angular
    .module('universal')
    .controller('ResultController', function ($scope, $http) {

        $scope.VIEW = {};
        $scope.VIEW.isLoading = false;
        $scope.VIEW.Data = "";
        $scope.sofa = "";
        $scope.rulesErrors = "";
        $scope.specifications = {};

        $scope.getResult = function (id) {

            $http.post('/instances/result/' + id).then(function (response2) {
                $scope.sofa = response2.data.result;
                $scope.rulesErrors = response2.data.result.Log
                    .filter(function (x) {
                        return x.Item2.hasOwnProperty('type') && x.Item2['type'] === 'rule-error';
                    });
            });
        }

        $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);
            var ff = [];
            for (var i = 0; i < $scope.sofa.Documents.length; i++) {
                ff.push($scope.sofa.Documents[i].ID)
            }
            $http.post('/instances/start/', {
                files: ff,
                process: $scope.proccess
            }).then(function (response) {

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

            });
        }

        $scope.addToDirecotry = function ($event) {

            var ff = [];
            for (var i = 0; i < $scope.sofa.Documents.length; i++) {
                ff.push($scope.sofa.Documents[i].ID)
            }
            $($event.currentTarget).prop('disabled', true);
            $http.post('/files/addToDirecotry/' + $scope.dir, ff).then(function (response2) {
                $($event.currentTarget).prop('disabled', false);
                //   window.location.reload()
            });
        }

        $scope.repeat = function (id, $event) {

            $($event.currentTarget).prop('disabled', true);
            $http.post('/instances/repeat/' + id).then(function (response2) {
                $($event.currentTarget).prop('disabled', false);
                window.location.reload()
            });
        }
        $scope.fshow = function (id) {
            $scope.options = []
            $scope.showfile = id
            angular.forEach($scope.sofa.Documents, function (value, key) {
                if (value.ID == id) {
                    $scope["text" + id.replace(/-/g, "")] = value.PlainText;

                    angular.forEach(value.Attributes, function (value2, key2) {
                        $scope.options.push(value2.Typ)
                    });
                }
            });
        }

        $scope.getSpecifications = function (fileId, processId) {

            $http.post('/instances/result/' + processId).then(function (response2) {

                var sofa = response2.data.result;

                for (doc in sofa.Documents) {

                    var docEntities = [];
                    var childEntities = [];
                    var parentEntities = [];

                    for (var ent in sofa.Entities) {
                        if (sofa.Entities[ent].DocumentID == sofa.Documents[doc].ID && sofa.Entities[ent].Tag == "SPECIFICATION") docEntities.push(sofa.Entities[ent]);
                    }

                    for (var ent in docEntities) {
                        if (docEntities[ent].ParentID != null) childEntities.push(docEntities[ent]);
                    }

                    for (var ent in docEntities) {
                        if (docEntities[ent].ParentID == null) {

                            var currentChildEntities = [];
                            for (var child in childEntities) {
                                if (childEntities[child].ParentID == docEntities[ent].ID) currentChildEntities.push(childEntities[child]);
                            }

                            docEntities[ent].Childs = currentChildEntities;
                            parentEntities.push(docEntities[ent]);
                        }
                    }

                    if (parentEntities.length > 0)
                        $scope.specifications[sofa.Documents[doc].ID] = parentEntities;
                    else
                        $scope.specifications[sofa.Documents[doc].ID] = 'Нет данных';
                }
            });
        }
    });