angular.module('virtoCommerce.catalogModule')
    .controller('virtoCommerce.catalogModule.productConfigurationDetailController', ['$scope', 'platformWebApp.bladeNavigationService', 'virtoCommerce.catalogModule.configurationsApi', 'platformWebApp.uiGridHelper',
        function ($scope, bladeNavigationService, configurationsApi, uiGridHelper) {
            var blade = $scope.blade;
            var updatePermission = 'catalog:configurations:update';

            blade.headIcon = 'fas fa-sliders';
            blade.title = 'catalog.blades.configuration-details.title';
            blade.formScope = null;

            blade.toolbarCommands = [
                {
                    name: "platform.commands.save",
                    icon: 'fas fa-save',
                    executeMethod: saveChanges,
                    canExecuteMethod: canSave,
                    permission: updatePermission
                },
                {
                    name: "platform.commands.reset",
                    icon: 'fa fa-undo',
                    executeMethod: function () { angular.copy(blade.origEntity, blade.currentEntity); },
                    canExecuteMethod: isDirty,
                    permission: updatePermission
                },
                {
                    name: "catalog.blades.configuration-details.commands.add",
                    icon: 'fas fa-plus',
                    executeMethod: function() { openSectionBlade({}); },
                    canExecuteMethod: function () { return true; },
                    permission: updatePermission
                },
                {
                    name: "platform.commands.delete",
                    icon: 'fas fa-trash-alt',
                    executeMethod: function () { deleteList($scope.gridApi.selection.getSelectedRows()); },
                    canExecuteMethod: isItemsChecked,
                    permission: 'catalog:configurations:delete'
                }
            ];

            blade.onClose = function (closeCallback) {
                bladeNavigationService.showConfirmationIfNeeded(isDirty(), canSave(), blade, saveChanges, closeCallback, "catalog.dialogs.configuration-save.title", "catalog.dialogs.configuration-save.message");
            };

            $scope.setForm = function (form) { blade.formScope = form; }

            $scope.setGridOptions = function (gridOptions) {
                uiGridHelper.initialize($scope, gridOptions, function (gridApi) {
                    $scope.gridApi = gridApi;

                    gridApi.draggableRows.on.rowFinishDrag($scope, function () {
                        for (var i = 0; i < blade.currentEntity.sections.length; i++) {
                            blade.currentEntity.sections[i].displayOrder = i;
                        }
                    });
                });
            };

            $scope.edit = function(item)
            {
                $scope.selectedNodeId = item.displayOrder;
                openSectionBlade(item);
            }

            $scope.delete = function (data) {
                deleteList([data]);
            };

            $scope.$watch("blade.currentEntity", function () {
                if (blade.currentEntity) {
                    // Configuration with any sections can be active
                    var canBeEnabled = _.some(blade.currentEntity.sections);

                    if (!canBeEnabled) {
                        blade.currentEntity.isActive = false;
                    }

                    $scope.canBeEnabled = canBeEnabled;
                }
            }, true);

            function deleteList(list) {
                bladeNavigationService.closeChildrenBlades(blade,
                    function () {
                        var deletedIds = _.map(list, 'id');
                        var undeletedEntries = _.difference(blade.currentEntity.sections, list);
                        _.each(undeletedEntries, function (section) {
                            if (section.dependsOnSectionId && _.contains(deletedIds, section.dependsOnSectionId)) {
                                section.dependsOnSectionId = null;
                            }
                        });
                        blade.currentEntity.sections = undeletedEntries;
                    });
            }

            function isItemsChecked() {
                return $scope.gridApi && _.some(blade.currentEntity.sections) && _.some($scope.gridApi.selection.getSelectedRows());
            }

            function isDirty() {
                return !angular.equals(blade.currentEntity, blade.origEntity);
            }

            function canSave() {
                return isDirty() && blade.formScope && blade.formScope.$valid;
            }

            function saveChanges() {
                blade.isLoading = true;

                configurationsApi.update({}, blade.currentEntity, function () {
                    refresh();
                });
            }

            function openSectionBlade(section) {
                var otherSections = _.filter(blade.currentEntity.sections, function(s) {
                    return s !== section && s.name;
                });

                var newBlade = {
                    id: "sectionDetail",
                    controller: 'virtoCommerce.catalogModule.configurationSectionDetailController',
                    template: 'Modules/$(VirtoCommerce.Catalog)/Scripts/blades/configurations/configuration-section-detail.tpl.html',
                    origEntity: section,
                    otherSections: otherSections,
                    onSaveNew: function (newSection) {
                        newSection.displayOrder = blade.currentEntity.sections.length;
                        blade.currentEntity.sections.push(newSection);
                    },
                };

                bladeNavigationService.showBlade(newBlade, blade);
            }

            function refresh() {
                blade.isLoading = true;

                configurationsApi.search({ productId: blade.productId }, function (result) {
                    blade.isLoading = false;

                    var data = {};

                    if (result.totalCount === 0 || result.results == null || result.results.length <= 0) {
                        data.productId = blade.productId;
                        data.isActive = false;
                    } else {
                        data = result.results[0];
                    }

                    if (data.sections == null) {
                        data.sections = [];
                    } else {
                        data.sections.sort((a, b) => a.displayOrder - b.displayOrder);
                    }

                    blade.currentEntity = angular.copy(data);
                    blade.origEntity = data;
                });
            }

            refresh();
        }]);
