(function () {
    global.dashModule
        .directive('linkCustomers', linkCustomersDirective)
        .controller('linkCustomersCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', linkCustomersCtrl]);

    function linkCustomersDirective() {
        return {
            restrict: 'E',
            templateUrl: 'users/html/linkCustomersTemplate'
        };
    }

    function linkCustomersCtrl($scope, $http, $, $translate, utilsService) {
        $scope.getCustomersAndCl = getCustomersAndCl;
        $scope.getUserClass = getUserClass;
        $scope.customerDragStart = customerDragStart;
        $scope.customerDragStop = customerDragStop;
        $scope.customerDropped = customerDropped;
        $scope.selectCommercialLead = selectCommercialLead;
        $scope.updateLinkedCustomers = updateLinkedCustomers;
        $scope.customers = [];
        $scope.commercialLeads = [];
        $scope.selectedCommercialLead = {};

        function getCustomersAndCl(){
            $http({
                method: 'GET',
                url: 'users/api/getCustomersAndCl'
            }).then(showCustomersAndCl, utilsService.onHttpError);
        }

        function showCustomersAndCl(response){
            if(response.data){
                $scope.customers = response.data.customers;
                $scope.commercialLeads = response.data.commercialLeads;
                $scope.selectedCommercialLead = response.data.commercialLeads[Object.keys(response.data.commercialLeads)[0]] || {};
                $scope.customers.forEach(hideLinkedCustomers);
            }
        }

        function selectCommercialLead(selected) {
            $scope.selectedCommercialLead = selected;
            $scope.customers.forEach(hideLinkedCustomers);
        }

        function hideLinkedCustomers(curentCustomer) {
            let isCustomerInArray = false;
            if ($scope.selectedCommercialLead.linkedCustomers) {
                isCustomerInArray = $scope.selectedCommercialLead.linkedCustomers.find((customer) => {
                    return customer.id === curentCustomer.id;
                });
            }

            curentCustomer.isNotLinked = isCustomerInArray ? false : true;
        }

        function getUserClass(curent) {
            return curent === $scope.selectedCommercialLead ? 'selected-user' : '';
        }

        function customerDragStart(event, ui, selector) {
            $('#'+selector).css({
                overflow: 'visible',
            });
        }

        function customerDragStop(event, ui, selector) {
            $('#'+ selector).css({
                'overflow': 'auto'
            });
        }

        function customerDropped(event, ui, dropContainer) {
            const dropToSelector = $(ui.helper).attr('drop-to');
            const idCustomer = $(ui.helper).attr('id-customer');

            if(dropContainer !== dropToSelector){
                return;
            }

            if (dropToSelector === 'linked-customers') {
                const dragedCustomer = $scope.customers.find((customer) => {
                    return customer.id === idCustomer;
                });

                $scope.selectedCommercialLead.linkedCustomers.push(dragedCustomer);
            } else {
                const dragedCustomerIndex = $scope.selectedCommercialLead.linkedCustomers.findIndex((customer) => {
                    return customer.id === idCustomer;
                });
                $scope.selectedCommercialLead.linkedCustomers.splice(dragedCustomerIndex, 1);
            }
            $scope.customers.forEach(hideLinkedCustomers);
        }

        function updateLinkedCustomers(){
            const params = $.param({
                idCommercialLead: $scope.selectedCommercialLead.id,
                customers: JSON.stringify($scope.selectedCommercialLead.linkedCustomers)
            });

            $http({
                method: 'POST',
                url: 'users/api/updateLinkedCustomers',
                data: params
            }).then(updateMessage, utilsService.onHttpError);
        }

        function updateMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    let translatedMessage = $translate.instant('users.messages.' + messageObj.message);
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                });
            }
        }
    }
})();
