angular
    .module('universal')
    .controller('AdminController', ['$scope', '$http', '$window', 'Upload', '$timeout', '$interval', function ($scope, $http, $window, Upload, $timeout, $interval) {

        $scope.VIEW = {};
        $scope.VIEW.errorMsg = '';
        $scope.VIEW.successMsg = '';

        $scope.data = {};

        $scope.isProcesses = false;
        $scope.isConfigs = false;
        $scope.isSources = false;
        $scope.isProblems = false;

        $scope.processes = [];
        $scope.processesExec = [];

        $scope.setProcessesExec = function ($index) {

            if (!$scope.processesExec.find(o => o === $scope.processes[$index].Code)) {
                $scope.processesExec.push($scope.processes[$index].Code);
                $scope.processes[$index].Class = "btn btn-info btn-sm";
            }
            else {
                for (var i in $scope.processesExec) {
                    if ($scope.processesExec[i] === $scope.processes[$index].Code) {
                        $scope.processesExec.splice(i, 1);
                        break;
                    }
                }
                $scope.processes[$index].Class = "btn btn-default btn-sm";
            }
        }

        $scope.unload = function () {

            $scope.data = [];
            if ($scope.isProblems) $scope.VIEW.problemsLoading = true;
            if ($scope.isSources) $scope.VIEW.sourcesLoading = true;

            for (var i in $scope.processes) {
                for (var y in $scope.processesExec) {
                    if ($scope.processes[i].Code === $scope.processesExec[y]) {
                        $scope.processes[i].Loading = true;
                    }
                }
            }

            var data = {
                processes: $scope.isProcesses ? $scope.processesExec : [],
                isSources: $scope.isSources,
                isProblems: $scope.isProblems
            };
            $.post('/admin/unload',
                data,
                function (response) {

                    $scope.VIEW.successMsg = "Выгрузка завершена. Нажмите кнопки <Скачать файл>";

                    for (var i in $scope.processes) {
                        $scope.processes[i].Loading = false;
                    }
                    $scope.VIEW.problemsLoading = false;
                    $scope.VIEW.sourcesLoading = false;

                    $scope.$apply(function () {
                        $scope.data = response;
                    })
                }
            );
        }

        $http.get('/processes/list/').then(function (response) {
            $scope.processes = response.data;
            var array = response.data.map(function (p) {
                var newp = { Class: 'btn btn-default btn-sm', Loading: false };
                angular.extend(newp, p);
                return newp;
            });

            $scope.processes = array;
        });

        $scope.archive = function () {
            $.post('/admin/archive',
                {},
                function (response) {
                    alert("архивы созданы")
                }
            );
        }

        $scope.upload = function (files, errFiles, type) {
            $scope.VIEW.errorMsg = '';
            $scope.VIEW.successMsg = '';

            if (type == 'process')
                $scope.VIEW.processUpLoading = true;
            if (type === 'problems')
                $scope.VIEW.problemsUpLoading = true;
            if (type === 'sources')
                $scope.VIEW.sourcesUpLoading = true;

            $scope.errFiles = errFiles;
            angular.forEach(files, function (file) {
                file.upload = Upload.upload({
                    url: '/admin/upload/',
                    data: {
                        file: file,
                        type: type
                    }
                });

                file.upload.then(function (response) {
                    if (response.data.result === 'error') {
                        $scope.VIEW.errorMsg = response.data.message;
                    }
                    else {
                        $scope.VIEW.successMsg = response.data.message;
                        $scope.VIEW.sourcesUpLoading = false;
                        $scope.VIEW.problemsUpLoading = false;
                        $scope.VIEW.processUpLoading = false;
                    }
                }, function (response) {
                    if (response.status > 0)
                        $scope.VIEW.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 *
                        evt.loaded / evt.total));
                });
            });
        }
    }]);