'use strict';

/** 
 * @controller ArticleEditController
 * @property scope 
 * @property route
 * @property location
 * @property filter
 * @property System
 * @property ArticleService
 * @property Resource
 */

var ArticleEditController = angular.module('articles.controllers.edit', [
    'system'
])
    .controller('ArticleEditController', [
        '$scope',
        '$route',
        '$location',
        'System',
        'ArticleService',
        'ClassificationService',
        'ImageService',
        'DocumentService',
        function ($scope, $route, $location, System, ArticleService, ClassificationService, ImageService) {

            $scope.Id = $route.current.params["id"];

            $(".tooltip").tooltip();

            $scope.ImageSelectorOpiton = {
                multipleSeletcion: false
            };
            
            $scope.LibrarySelectorOpiton = {
                multipleSeletcion: false
            };

            $scope.DocumentSelectorOpiton = {
                multipleSeletcion: false
            };

            $scope.KartaSelectorOpiton = {
                multipleSeletcion: false
            };

            $scope.classifications = { };
            
            /**
            * @method get
            * @param Id
            * @description ArticleService gets Article data by its id
            */
            ArticleService.getEdit($scope.Id)
                .then(function(data) {
                    $scope.article = data;
                     System.Page.title("Articles - " + $scope.article.Title);
                    
                    for (var i = 0; i < $scope.article.Labels.length; i++) {
                        if ($scope.classifications[data.Labels[i].Classification] == undefined) {
                            $scope.classifications[data.Labels[i].Classification] = new Array();
                        }
                        $scope.classifications[data.Labels[i].Classification].push(data.Labels[i].ID);
                    }
                });

            ClassificationService.getByAlias('tag').then(function (response) {
                $scope.tags = response;
            });
            
            ClassificationService.getByAlias('vijesti').then(function (response) {
                $scope.categories = qusion.list.jsTree(response.Labels, "ID", "Name");
            });
            
            ClassificationService.getByAlias('lokacije').then(function (response) {
                $scope.zcategories = qusion.list.jsTree(response.Labels, "ID", "Name");
            });

            /**
            * @method update
            * @description ArticleService updates Article data after successful edit
            */
            $scope.update = function () {
                $scope.article.Labels = [];
                var keys = (function (obj) {
                    var k = [], x;
                    for (x in obj) {
                        k.push(x);
                    }
                    return k;
                })($scope.classifications);

                for (var i = 0; i < keys.length; i++)
                {
                    for (var j = 0; j < $scope.classifications[keys[i]].length; j++) {
                        $scope.article.Labels.push($scope.classifications[keys[i]][j]);
                    }
                    
                }
                
                
                ArticleService.update($scope.article)
                    .then(function (response) {
                        if (response.Errors) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource("utility.changeNotSave"));
                        } else {
                            $location.url('/articles');
                            System.Message.success($scope.$resource("articles.ArticleUpdated"));
                        }
                    });
            };
            
            $scope.treeOptions = {
                selectMode: 2,
                dialogTitle: $scope.$resource("articles.select_dialog_categories")
            };

            /**
            * @action back returns to location /articles
            */
            $scope.back = function() {
                $location.url('/articles');
            };
          
        }
    ]);