'use strict';

/** 
 * @controller PostEditController
 * @property scope 
 * @property route
 * @property location
 * @property System
 * @property BlogService
 * @property Resource
 */

var PostEditController = angular.module('blogs.controllers.postedit', [
    'system'
])
    .controller('PostEditController', [
        '$scope',
        '$location',
        '$route',
        'System',
        'BlogService',
        'ClassificationService',
        function ($scope, $location, $route, System, BlogService, ClassificationService) {
            
            var id = $route.current.params["id"];
            $scope.query = { };
            $scope.classifications = {};

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

         
            BlogService.getForEdit(id).then(function(data) {
                    $scope.post = data;

                    for (var i = 0; i < $scope.post.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('vijesti').then(function (response) {
                    $scope.categories = qusion.list.jsTree(response.Labels, "ID", "Name");
                });

                ClassificationService.getByAlias("vijesti")
                        .then(function (data) {
                            $scope.classification = {
                                Alias: data.Alias,
                                team: []
                            };
                            $scope.classification = qusion.list.jsTree(data.Labels, "ID", "Name");
                        });
            
            $scope.treeOptions = {
                selectMode: 2,
                dialogTitle: $scope.$resource("blogs.select_dialog_labels")
            };

         


            /**
            * @action back returns to location /blogs/BlogID
            */
            $scope.back = function () {
                $location.url('/blogs/' + $scope.post.BlogID);
            };

            /**
            * @method SaveAndPublish
            * @description updates existing blog post
            */
            $scope.SaveAndPublish = function () {
                $scope.post.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.post.Labels.push($scope.classifications[keys[i]][j]);
                    }

                }

                $scope.post.status = 2;
                BlogService.updatePost($scope.post).then(function (response) {
                    if (response.Errors) {
                        $scope.errors = response.Errors;
                        System.Message.error($scope.$resource('blogs.PostCreatedError'));
                    } else {
                        $location.url('/blogs/' + $scope.post.BlogID);
                        System.Message.success($scope.$resource('blogs.PostUpdated'));
                    }
                });
            };

          
        }
    ]);