(function () {
    global.dashModule
        .controller('documentsAddCtrl', ['$scope', '$http', '$', '$translate', 'Upload', 'utilsService', documentsAddController])
        .directive('documentsAdd', [documentsAddDirective]);

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

    function documentsAddController($scope, $http, $, $translate, Upload, utilsService) {
        $scope.getDocumentTypes = getDocumentTypes;
        $scope.setDocumentTypes = setDocumentTypes;
        $scope.uploadFile = uploadFile;
        $scope.selectType = selectType;
        $scope.getTypeRowClass = getTypeRowClass;
        $scope.addNewType = addNewType;
        $scope.hasInfo = hasInfo;
        $scope.documentTypes = [];
        $scope.documentName = '';
        $scope.documentNewType = '';
        $scope.selectedType = {};

        function getDocumentTypes() {
            $http({
                method: 'GET',
                url: 'documents/api/getDocumentTypes'
            }).then(setDocumentTypes, utilsService.onHttpError);
        }

        function setDocumentTypes(response) {
            if (response.data && response.data.length) {
                $scope.documentTypes = response.data;
            }
        }

        function uploadFile(file) {
            if (parseInt($scope.selectedType.isSpecialType) === 1) {
                displayMessage({data: {messages: [{code:'error', 'message': 'INVALID_DOC_TYPE'}]}});
            } else {
                Upload.upload({
                    url: 'documents/api/uploadNewDocument',
                    method: 'POST',
                    file: file,
                    data: {
                        idDocumentType: $scope.selectedType.idDocumentType || 0,
                        documentName: $scope.documentName
                    }
                }).then(displayMessage, utilsService.onHttpError);
            }
        }

        function displayMessage(response) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const translatedMessage = $translate.instant('documents.messages.' + messageObj.message);
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                    if (messageObj.code === 'success') {
                        getDocumentTypes();
                        $scope.documentName = '';
                        $scope.documentNewType = '';
                    }
                });
            }
        }

        function getTypeRowClass(docType) {
            let rowClass = parseInt(docType.isSpecialType) === 0 ? 'can-add-document' : 'special-document';
            rowClass += $scope.selectedType === docType ? ' selected-document' : '';
            return rowClass;
        }

        function selectType(docType) {
            $scope.selectedType = docType;
        }

        function addNewType() {
            const params = $.param({
                documentNewType: $scope.documentNewType
            });

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

    function hasInfo(selectedType) {
        return parseInt(selectedType.isSpecialType) === 1 || parseInt(selectedType.idDocumentType) === 1 || parseInt(selectedType.idDocumentType) === 6;
    }
})();
