/// <reference path="jquery-1.8.2.js" />
/// <reference path="kendo.web.min.js" />

var serverList = [];
var server = {} || server;
server.getServerList = function () {
    return [];
}

var count=0;
server.create = function () {
    var id = count++;
    return {
        serverCustomId: id,
        serverName: id,
        serverPartItems: serverPart.getServerPartItems(),
        serverTemplate: ""
    }
}

server.getServer = function (serverCustomId) {
    var server;
    if (serverList.length > 0) {
        for (var i = 0; i < serverList.length; i += 1) {
            if (serverList[i].serverCustomId === serverCustomId)
                return {
                    serverObj: serverList[i],
                    index:i
                }
        }
    }
}

server.addServer = function () {
    var serverObj = server.create();
    serverList.push(serverObj);
    server.loadTemplate(serverObj);
}

server.deleteServer = function (serverCustomId) {
    if (serverList.length > 0) {
        var data = server.getServer(serverCustomId);
        if (data.serverObj.serverTemplate) {
            data.serverObj.serverTemplate.remove();
        }
        serverList.splice(data.index, 1);
    }
}

server.updataServer = function (serverCustomId) {
}

server.loadTemplate = function (serverObj) {
    var req = $.ajax({
        url: "Server/GetServerTemplate",
        type: "post",
        dataType: "html"
    });
    req.done(function (msg) {
        var template = $(msg);
        template.find("[name='serverName']").val(serverObj.serverName);
        template.find(".deleteServerBtn").click(function () { server.deleteServer(serverObj.serverCustomId) });
        var grid = template.find(".serverPartGrid");
//        grid.kendoGrid({
//            dataSource: {
//                data: new kendo.data.DataSource({
//                    data: serverObj.serverPartItems,
//                    autoSync: true,
//                    pageSize:10,
//                    schema: {
//                        model: {
//                            id: "serverPartItemId",
//                            fields: {
//                                serverPartItemId: { editable: false },
//                                name:{validation:true},
//                                value: { validation: true }
//                            }
//                        }
//                    }
//                })
//            },
//            toolbar: ["create"],
//            editable: "inline",
//            pageable: true,
//            columns: [
//                { field: "name", title: "Name" },
//                { field: "value", title: "Value" },
//                { command: ["edit", "destroy"], title: "&nbsp;", width: "210px" }
//            ]
        //        })

        var crudServiceBaseUrl = "http://demos.kendoui.com/service",
                        dataSource = new kendo.data.DataSource({
                            transport: {
                                read: {
                                    url: crudServiceBaseUrl + "/Products",
                                    dataType: "jsonp"
                                },
                                update: {
                                    url: crudServiceBaseUrl + "/Products/Update",
                                    dataType: "jsonp"
                                },
                                destroy: {
                                    url: crudServiceBaseUrl + "/Products/Destroy",
                                    dataType: "jsonp"
                                },
                                create: {
                                    url: crudServiceBaseUrl + "/Products/Create",
                                    dataType: "jsonp"
                                },
                                parameterMap: function (options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return { models: kendo.stringify(options.models) };
                                    }
                                }
                            },
                            batch: true,
                            pageSize: 30,
                            schema: {
                                model: {
                                    id: "ProductID",
                                    fields: {
                                        ProductID: { editable: false, nullable: true },
                                        ProductName: { validation: { required: true} },
                                        UnitPrice: { type: "number", validation: { required: true, min: 1} },
                                        Discontinued: { type: "boolean" },
                                        UnitsInStock: { type: "number", validation: { min: 0, required: true} }
                                    }
                                }
                            }
                        });

                        grid.kendoGrid({
                            dataSource: dataSource,
                            pageable: true,
                            height: 400,
                            toolbar: ["create"],
                            columns: [
                            "ProductName",
                            { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "150px" },
                            { field: "UnitsInStock", title: "Units In Stock", width: "150px" },
                            { field: "Discontinued", width: "100px" },
                            { command: ["edit", "destroy"], title: "&nbsp;", width: "210px"}],
                            editable: "inline"
                        });
                        serverObj.serverTemplate = template;
        $(".serverList").append(template);
    })
    req.fail(function (jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    });
}