(function () {
    global.dashModule
        .controller('documentsLinkCtrl', ['$scope', '$http', '$', '$translate', 'utilsService', documentsLinkController])
        .directive('documentsLink', [documentsLinkDirective]);

    function documentsLinkDirective() {
        return {
            restrict: 'E',
            templateUrl: 'documents/html/documentsLinkTemplate'
        };
    }

    function documentsLinkController($scope, $http, $, $translate, utilsService) {
        $scope.getDocumentsAndPackages = getDocumentsAndPackages;
        $scope.selectPackage = selectPackage;
        $scope.getPackageClass = getPackageClass;
        $scope.documentDragStart = documentDragStart;
        $scope.documentDragStop = documentDragStop;
        $scope.documentDropped = documentDropped;
        $scope.updatePackageDocuments = updatePackageDocuments;
        $scope.documents = [];
        $scope.packages = [];
        $scope.selectedPackage = {};

        function getDocumentsAndPackages() {
            $http({
                method: 'POST',
                url: 'documents/api/getDocumentsAndPackages'
            }).then(setDocumentsAndPackages, utilsService.onHttpError);
        }

        function setDocumentsAndPackages(response) {
            if (response.data && response.data.documents && response.data.packages) {
                $scope.documents = response.data.documents;
                $scope.packages = response.data.packages;
                $scope.documents.forEach(hideLinkedDocuments);
                $scope.selectedPackage = response.data.packages[0] || {};
            }
        }

        function hideLinkedDocuments(curentDocument) {
            let isDocumentInArray = false;
            if ($scope.selectedPackage.documents) {
                isDocumentInArray = $scope.selectedPackage.documents.find((doc) => {
                    return doc.idDocument === curentDocument.idDocument;
                });
            }

            curentDocument.isNotLinked = isDocumentInArray ? curentDocument.isNotLinked = false : curentDocument.isNotLinked = true;
        }

        function selectPackage(selected) {
            $scope.selectedPackage = selected;
            $scope.documents.forEach(hideLinkedDocuments);
        }

        function getPackageClass(curent) {
            return curent === $scope.selectedPackage ? 'selected-package' : '';
        }

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

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

        function documentDropped(event, ui, dropContainer) {
            const dropToSelector = $(ui.helper).attr('drop-to');
            const idDocument = $(ui.helper).attr('id-document');

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

            if (dropToSelector === 'linked-documents') {
                const dragedDocument = $scope.documents.find((doc) => {
                    return doc.idDocument === idDocument;
                });

                $scope.selectedPackage.documents.push(dragedDocument);
                $scope.documents.forEach(hideLinkedDocuments);
            } else if(dropToSelector === 'all-documents') {
                const dragedDocumentIndex = $scope.selectedPackage.documents.findIndex((doc) => {
                    return doc.idDocument === idDocument;
                });
                $scope.selectedPackage.documents.splice(dragedDocumentIndex, 1);
                $scope.documents.forEach(hideLinkedDocuments);
            }

        }

        function updatePackageDocuments() {
            const params = $.param({
                idPackage: $scope.selectedPackage.idPackage,
                documents: JSON.stringify($scope.selectedPackage.documents)
            });

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

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