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

        $scope.instance = undefined;
        $scope.documents = [];
        $scope.sofa = "";
        $scope.processes = [];
        $scope.response = {};
        $http.get('/processes/list/').then(function (response) {
            $scope.processes = response.data
        });

        $scope.StartProcess = function (code) {
            $scope.sofa = "";
            var ff = [];
            for (var i = 0; i < $scope.documents.length; i++) {
                ff.push($scope.documents[i].ID)
            }
            $scope.instance = undefined;
            $http.post('/instances/start/', {
                files: ff,
                process: code
            }).then(function (response) {

                $scope.response = response;

                if (response.data.success) {
                    $scope.instance = response.data.instance_id
                    $('#smartwizard').smartWizard("next");
                    $scope.fight();
                }
                else{
                    $scope.stopFight();
                }
            });
        }
        var stop;
        $scope.fight = function () {
            // Don't start a new fight if we are already fighting
            if (angular.isDefined(stop)) return;

            stop = $interval(function () {
                $http.post('/instances/check/' + $scope.instance).then(function (response) {
                    if (response.data.status != "InProgress" && response.data.status != "Approved") {
                        $http.post('/instances/result/' + $scope.instance).then(function (response2) {
                            $scope.sofa = response2.data.result;
                            $scope.stopFight();
                        });
                    }
                });

            }, 500);
        };

        $scope.stopFight = function () {
            if (angular.isDefined(stop)) {
                $interval.cancel(stop);
                stop = undefined;
            }
        };

        $scope.uploadFiles = function (files, errFiles) {
            if (files.length == 0)
                return;
            $('#smartwizard').smartWizard("reset");
            $scope.instance = undefined;
            $scope.documents = [];
            $scope.sofa = "";
            $scope.files = files;
            $scope.errFiles = errFiles;
            angular.forEach(files, function (file) {
                file.upload = Upload.upload({
                    url: '/files/add/',
                    data: { file: file }
                });

                file.upload.then(function (response) {
                    $scope.documents.push(response.data)
                    $scope.files = []
                    $scope.invalidFiles = []
                }, function (response) {
                    if (response.status > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 *
                        evt.loaded / evt.total));
                });
            });
            $('#smartwizard').smartWizard("next");
        }
    }]);