﻿angular.module('SubServiceDescriptionOperations')
    .controller('SubServiceDescriptionOperationsController', ['$scope', '$stateParams', 'SubServiceDescriptionOperationsService', '$rootScope', '$window',
        SubServiceDescriptionOperationsController]);


function SubServiceDescriptionOperationsController($scope, $stateParams, SubServiceDescriptionOperationsService, $rootScope, $window) {

    $scope.subServiceDescriptions = {};
    $scope.description = {};
    $scope.selectedDescription = {}; 
    $scope.translate = {
        "Active": false,
        "Description": {}
    };

    $scope.AccordionStates =
        {
            "Name": false,
            "MainDescription": false,
            "Suppliers": false,
            "Media": false
        };

    $scope.ExecuteGetSubServiceDescription = function (subServiceId) {

        SubServiceDescriptionOperationsService.ExecuteGetDescription(subServiceId).then(function (data) {

            $scope.subServiceDescriptions = data;
            $scope.description = $scope.subServiceDescriptions.Descriptions[0];
            $scope.selectedDescription = $scope.description.Overview[0];
            $scope.translate.Description = $scope.description.Overview[0];
        });
    }
    $scope.ExecuteGetSubServiceDescription($stateParams.subServiceId);

    $scope.ExecuteSetDescription = function () {
        if (!SubServiceDescriptionOperationsService.CheckIfAppliedToAllSuppliedExistOnce($scope.subServiceDescriptions.Descriptions))
            $window.alert("Applied to all Suppliers more than once in 2 descriptions is not allowed action!");
        else {
            SubServiceDescriptionOperationsService.ExecuteSetDescription($scope.subServiceDescriptions.SubServiceId, $scope.subServiceDescriptions).then(function () {
                $rootScope.$broadcast('DescriptionSubmitted', {});
            });
        }
    }

    $scope.setSelectedSubServiceDescription = function (newD) {

        $scope.description = newD;
        $scope.selectedDescription = $scope.description.Overview[0];
        $scope.translate.Description = $scope.description.Overview[0];
    }

    $scope.addNewDescription = function (Overview) {

        $scope.subServiceDescriptions.Descriptions.push(SubServiceDescriptionOperationsService.CreateNewSubServiceDescription(Overview, $scope.description.Name));
        $scope.description = $scope.subServiceDescriptions.Descriptions[$scope.subServiceDescriptions.Descriptions.length - 1];
        $scope.selectedDescription = $scope.description.Overview[0];
        $scope.translate.Description = $scope.description.Overview[0];
        $scope.AccordionStates.Name = true;
    }

    $scope.removeDescription = function (description) {
        $scope.subServiceDescriptions.Descriptions.splice($scope.subServiceDescriptions.Descriptions.indexOf(description), 1);
    }

    $scope.addSpecificSupplierToDescription = function (supplier) {
        if (supplier && (!SubServiceDescriptionOperationsService.CheckIfSpecificSupplierIsApplied($scope.subServiceDescriptions.Descriptions, supplier)))
            $scope.description.SpecificSuppliers.push(supplier);
        else if (supplier == null)
            $scope.description.SpecificSuppliers = [];
        else
            $window.alert("Incorrect supplier applied. This supplier already has been applied");
    }

    $scope.removeSpecificSupplierToDescription = function (supplier) {
        $scope.description.SpecificSuppliers.splice($scope.description.SpecificSuppliers.indexOf(supplier), 1);
    }
}

