(function () {
    "use strict";

    WinJS.UI.Pages.define("pages/dataStorage/dataStorage.html", {
        ready: function (element, options) {
            Customers.preparePage();
        },

        unload: function () {
        },

        updateLayout: function (element, viewState, lastViewState) {
        }
    });
    WinJS.Namespace.define("Customers", {
        removeListeners: function (e) {
            window.removeEventListener("resize", Customers.handleLayoutChange);
        },

        preparePage: function () {
            WinJS.Binding.processAll(customerCount, ViewModel.CustomerData);
            WinJS.Binding.processAll(customerList, ViewModel.CustomerData);
            editCustomer.disabled = true;
            deleteCustomer.disabled = true;
            var listView = customerList.winControl;
            var currentItem = null;
            listView.addEventListener('selectionchanged', function (e) {
                if (this.winControl.selection.count() != 0) {
                    listView.selection.getItems().then(function (items) {
                        currentItem = items[0].data;
                    });
                    WinJS.Binding.processAll(customerDetail, currentItem);
                    editCustomer.disabled = false;
                    deleteCustomer.disabled = false;
                }
                else {
                    editCustomer.disabled = true;
                    deleteCustomer.disabled = false;
                    WinJS.Binding.processAll(customerDetail, null);
                }
            });
            loadAllCustomers.addEventListener('click', function (e) {
                ViewModel.CustomerData.customerList.length = 0;
                Samples.Data.getCustomers().done(function (results) {
                    ViewModel.addRecords(results);
                });
            })
            //saveAllCustomers.addEventListener('click', function (e) {
            //    //JSONHandler.saveCustomers();
            //});
            editCustomer.addEventListener('click', function (e) {
                editID.value = currentItem.id;
                editFirstName.value = currentItem.firstName;
                editLastName.value = currentItem.lastName;
                editAge.value = currentItem.age;
            });
            deleteCustomer.addEventListener('click', function (e) {
                deleteID.innerText = currentItem.id;
                deleteFirstName.innerText = currentItem.firstName;
                deleteLastName.innerText = currentItem.lastName;
                deleteAge.innerText = currentItem.age;
            });
            saveNewCustomer.addEventListener('click', function (e) {
                ViewModel.saveCustomer(newFirstName.value,
                                       newLastName.value,
                                       newAge.value,
                                       newID.value);
                var customer = ViewModel.addCustomer(
                    newFirstName.value,
                    newLastName.value,
                    newAge.value,
                    newID.value);
                Helpers.hideFlyout('addNewCustomerFlyout');
            });
            saveEditedCustomer.addEventListener('click', function (e) {
                currentItem.firstName = editFirstName.value;
                currentItem.lastName = editLastName.value;
                currentItem.age = editAge.value;
                Helpers.hideFlyout('editCustomerFlyout');
                Samples.Data.updateCustomer(currentItem);
            });
            confirmDeleteCustomer.addEventListener('click', function (e) {
                //Samples.Data.deleteCustomer(deleteID.innerText);
                Samples.Data.deleteCustomerObject(Model.Customer.returnSimpleCustomerInfo(currentItem));
                loadAllCustomers.click();
                Helpers.hideFlyout('deleteCustomerFlyout');
            });
            cancelNewCustomer.addEventListener('click', function (e) {
                Helpers.hideFlyout('addNewCustomerFlyout');
            });
            cancelEditCustomer.addEventListener('click', function (e) {
                Helpers.hideFlyout('editCustomerFlyout');
            });
            cancelDeleteCustomer.addEventListener('click', function (e) {
                Helpers.hideFlyout('deleteCustomerFlyout');
            });
        },
    });
})();