'use strict';

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

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

            $scope.post = {
                BlogID: id,
                BlogTitle: {}
            };

            $scope.refresh = function() {
                BlogService.get(id).then(function(blog) {
                    $scope.post.BlogTitle = blog.Title;
                });

                ClassificationService.getByAlias('vijesti').then(function (response) {
                    $scope.categories = qusion.list.jsTree(response.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 creates new 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.createPost($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.PostCreated'));
                    }
                });
            };

            $scope.Save = function() {

            };

            $scope.refresh();
        }
    ]);