(function () {
    global.dashModule
        .controller('orderProjectsController', ['$scope', '$rootScope', '$http', '$compile', '$translate', '$', 'dataTableHelper', 'utilsService', orderProjectsController])
        .directive('orderProjects', [orderProjectsDirective]);
    function orderProjectsDirective() {
        return {
            restrict: 'E',
            templateUrl: 'orderProjects/html/orderProjectsTemplate'
        };
    }

    function orderProjectsController($scope, $rootScope, $http, $compile, $translate, $, dataTableHelper, utilsService) {
        $scope.getOrderProjectsHeaders = getOrderProjectsHeaders;
        const translationPath = 'orders.tables.headers.';

        function getOrderProjectsHeaders() {
            $http({
                method: 'POST',
                url: 'orderProjects/api/getOrderProjectsHeaders',
            }).then(showOrderProjects, utilsService.onHttpError);
        }

        function showOrderProjects(response) {
            if (response.data.headers.length > 0) {
                const params = {
                    selector: '#order-projects-table',
                    url: 'orderProjects/api/getOrderProjects?available=0',
                    hasEdit: true,
                    extraTableOptions: {
                        responsive: false,
                        order: [
                            [1, 'asc']
                        ]
                    }
                };
                dataTableHelper.generateColumns(response.data.headers, translationPath, dataTableHelper.showTable, params, formatOrderProjectsColumn)
                    .then((table) => {
                        addEditEvent(table, params.selector, 'order-projects-edit', 'orderProjectsEditCtrl', getOrderProjectsHeaders);
                    });
            }
        }

        function addEditEvent(table, containerSelector, directive, controller, callback) {
            $(containerSelector + ' tbody').off('click', 'td.info-edit');
            $(containerSelector + ' tbody').on('click', 'td.info-edit', function () {
                var tr = $(this).closest('tr');
                var row = table.row(tr);

                if (row.child.isShown()) {
                    row.child.hide();
                    tr.removeClass('shown');
                } else {
                    const newScope = $rootScope.$new();
                    newScope.data = $.extend(true, {}, row.data());
                    newScope.data.isAvailable = String(newScope.data.isAvailable);
                    const directiveHtml = '<' + directive + ' class="' + directive + '" ng-controller="' + controller + '"></' + directive + '>';
                    const layerId = newScope.data.idProject;
                    newScope.formAction = 'EDIT';
                    newScope.onUpdated = callback;
                    const layerSelector = 'edit-layer-' + layerId;
                    row.child('<div class="edit-tr" id="' + layerSelector + '"></div>').show();

                    const comp = $compile($(directiveHtml))(newScope);
                    newScope.$apply();

                    $('#' + layerSelector).append(comp);
                    tr.addClass('shown');
                }
            });
        }

        function formatOrderProjectsColumn(value, translations) {
            const columnObj = {
                data: value,
                title: translations[translationPath + value]
            };
            const renders = getOrderProjectsRenders();
            columnObj.visible = isColumnVisible(value);
            if (typeof renders[value] !== 'undefined') {
                columnObj.render = renders[value];
            }

            return columnObj;
        }

        function getOrderProjectsRenders(){
            return {
                isAvailable: isAvailableRender
            };

            function isAvailableRender(data){
                return data === 1 ? 'available' : 'unavailable';
            }
        }

        function isColumnVisible(value){
            const notVisibleFields = [];
            return notVisibleFields.indexOf(value) === -1;
        }
    }
})();
