angular
    .module('universal')
    .controller('FilesController', ['$scope', '$http', '$window', 'Upload', '$timeout', '$interval', function ($scope, $http, $window, Upload, $timeout, $interval) {
        $scope.isLoading = false;
        $scope.processes = [];
        $http.get('/processes/list/').then(function (response) {
            $scope.processes = response.data
        });

        $scope.dirs = [];
        $http.get('/files/dirs/').then(function (response) {
            $scope.dirs = response.data
        });

        $scope.start = function (id, $event) {
            $($event.currentTarget).prop('disabled', true);
            angular.forEach($scope.dirs, function (value, key) {
                if (value.ID.replace(/-/g, "") == id) {

                    var ff = [];
                    for (var i = 0; i < value.Files.length; i++) {
                        ff.push(value.Files[i].ID)
                    }

                    $http.post('/instances/start/', {
                        files: ff,
                        process: $scope["proccess" + id]
                    }).then(function (response) {

                        window.location.href = "/instances/show/" + response.data.instance_id

                    });
                } else {
                    if (console && console.log) console.log(value.ID);
                }
            });


        }

        $scope.AddDirectory = function () {
            /*$http.post('/files/AddDirectory/', {
                name:$scope.directoryName
            }).then(function (response) {
                window.location.reload();
            });*/

            $.post('/files/AddDirectory/', { name: $scope.directoryName },
                function (res) {
                    window.location.reload();
                }
            );
        }

        $scope.uploadToExcel = function () {
            $scope.isLoading = true;
            var directories = [];
            for (var i = 0; i < $('.directory').length; i++) {
                var input = $($('.directory')[i]).find("input[type='checkbox']");
                if ($(input).is(":checked")) {
                    var id = $($('.directory')[i]).attr('id');
                    directories.push(id);
                }
            }

            $http.post('/files/OnPostExport/',
                {
                    directories: directories
                }).then(function (response) {
                    $scope.isLoading = false;
                    window.location.href = "/" + response.data.url;
                });
        };

        $scope.uploadFiles = function (files, errFiles, dir, $event) {
            if (files.length == 0)
                return;
            $($event.currentTarget).prop('disabled', true);

            $scope.documents = [];

            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);

                    var ff = [];
                    for (var i = 0; i < $scope.documents.length; i++) {
                        ff.push($scope.documents[i].ID)
                    }

                    var data = {
                        id: dir,
                        ids: ff
                    };

                    $http.post('/files/addToDirecotry/' + data.id, data.ids).then(function (response2) {
                        window.location.reload()
                    });

                    $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));
                });
            });
        }

        $scope.SDKuploadFiles = function (files, errFiles, $event) {
            if (files.length == 0)
                return;
            $($event.currentTarget).prop('disabled', true);

            $scope.documents = [];

            angular.forEach(files, function (file) {

                file.upload = Upload.upload({
                    url: '/files/SDKadd/',
                    data: { file: file }
                });

                file.upload.then(function (response) {

                    window.location.reload()

                }, 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));
                });
            });
        }

        $scope.installSDK = function (id) {

            $.post('/files/installSDK/',
                {
                    id: id
                },function (response) {
                    window.location.reload();
                });
        }
    }]);