(function () {
    global.dashModule
        .controller('scheduleMeetingCtrl', ['$scope', '$', '$http', '$translate', 'utilsService', scheduleMeetingCtrl])
        .directive('scheduleMeeting', [scheduleMeetingDirective]);

    function scheduleMeetingDirective() {
        return {
            restrict: 'E',
            templateUrl: 'orders/html/scheduleMeetingTemplate'
        };
    }

    function scheduleMeetingCtrl($scope, $, $http, $translate, utilsService) {
        $scope.updateScheduledDates = updateScheduledDates;
        $scope.getScheduledDates = getScheduledDates;
        $scope.addNewSchedule = addNewSchedule;
        $scope.getIcon = getIcon;
        $scope.canNotEditDate = canNotEditDate;
        $scope.changeScheduleStatus = changeScheduleStatus;
        $scope.parentStep = $scope.$parent.step;
        const stepInfo = {
            isNextButtonDisabled: true,
            idOrder: $scope.$parent.step.idOrder,
            idPackage: $scope.$parent.step.idPackage,
            idProcess: $scope.$parent.step.idProcess
        };
        utilsService.executeRegisteredFunction('isNextBtnDisabled', stepInfo);

        function getScheduledDates(step) {
            const params = $.param({
                idOrder: step.idOrder,
                idPackage: step.idPackage,
                idProcessStep: step.idProcessStep || 0
            });

            $http({
                method: 'POST',
                url: 'orders/api/getScheduledDates',
                data: params
            }).then((response) => {
                setScheduleDates(response, step);
            }, utilsService.onHttpError);
        }

        function setScheduleDates(response, step) {
            if (response.data && response.data.length) {
                step.scheduledDates = response.data;
            } else {
                addNewSchedule(step);
            }
            checkIfDateConfirmed(step);
        }

        function addNewSchedule(step) {
            if (!step.scheduledDates) {
                step.scheduledDates = [];
            }

            step.scheduledDates.push((() => {
                return {
                    idSchedule: 0,
                    isDateConfirmed : 0,
                    scheduledDate: '',
                    idPackage: step.idPackage,
                    idProcessStep: step.idProcessStep
                };
            })());
        }

        function updateScheduledDates(newDate, data) {
            const params = $.param({
                idOrder: data.step.idOrder,
                idPackage: data.step.idPackage,
                idProcess: data.step.idProcess,
                idProcessStep: data.step.idProcessStep || 0,
                idSchedule: data.scheduleDate.idSchedule,
                newDate: newDate,
            });

            $http({
                method: 'POST',
                url: 'orders/api/updateScheduledDates',
                data: params
            }).then((response) => {
                showConfirmationMessage(response, data.step);
            }, utilsService.onHttpError);
        }

        function showConfirmationMessage(response, step) {
            if (typeof response.data.messages !== 'undefined') {
                response.data.messages.forEach((messageObj) => {
                    const key = messageObj.key ? $translate.instant('orders.messages.' + messageObj.key) : '';
                    let translatedMessage = $translate.instant('orders.messages.' + messageObj.message);
                    translatedMessage = key !== '' ? key + ': ' + translatedMessage : translatedMessage;
                    utilsService.displayMessage(messageObj.code, translatedMessage);
                    if (messageObj.code === 'success') {
                        getScheduledDates(step);
                    }
                });
            }
        }

        function getIcon(status){
            const statusesIcons = {
                pending: 'time',
                accepted: 'ok',
                declined: 'ban-circle'
            };

            return statusesIcons[status];
        }

        function canNotEditDate(scheduleDate){
            return parseInt(scheduleDate.isDateConfirmed) !== 0;
        }

        function changeScheduleStatus(scheduleDate, status, step){
            const params = $.param({
                idSchedule : scheduleDate.idSchedule,
                idOrder: step.idOrder,
                idPackage: step.idPackage,
                actionCode: step.actionCode,
                status
            });

            $http({
                method: 'POST',
                url: 'orders/api/updateScheduleDateStatus',
                data: params
            }).then((response) => {
                showConfirmationMessage(response, step);
            }, utilsService.onHttpError);
        }

        function checkIfDateConfirmed(step){
            let isConfirmed = false;
            step.scheduledDates.forEach((date) => {
                if(date.isDateConfirmed && parseInt(date.isDateConfirmed) === 1){
                    isConfirmed = true;
                }
            });

            stepInfo.isNextButtonDisabled = !isConfirmed;
            utilsService.executeRegisteredFunction('isNextBtnDisabled', stepInfo);
        }
    }
})();
