'use strict';

/** 
 * @controller BlogDetailsController
 * @property scope 
 * @property route
 * @property location
 * @property window
 * @property System
 * @property BlogService
 * @property Resource
 */

angular.module('blogs.controllers.details', [
    'system'
])
    .controller('BlogDetailsController', [
        '$scope',
        '$location',
        '$route',
        '$window',
        'System',
        'BlogService',
        '$sce',
        function ($scope, $location, $route, $window, System, BlogService, $sce) {
            var id = $route.current.params["BlogID"];
            $scope.query = $route.current.params || {};
            $scope.currentView = 'view';
            
            $(".tooltip").tooltip();

            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                BlogService.get(id).then(function (blog) {
                    $scope.blog = blog;
                    $scope.existblog = {
                        ID: blog.ID,
                        Title: blog.Title,
                        Summary: blog.Summary
                    };
                    System.Page.title($scope.existblog.Title);
                });
                
                BlogService.queryPosts($scope.query).then(function (data) {
                    $scope.posts = data.posts;
                    $scope.pager = System.Pager.create({ page: $scope.query.page, total: data.total });
                });
            };
            
            $scope.renderHtml = function (html_code) {
                return $sce.trustAsHtml(html_code);
            };
            /**
            * @action back returns to location /blogs
            */
            $scope.back = function () {
                $location.url('/blogs');
            };

            /**
            * @method isCurrentView
            * @description returns value that indicates if currentView is initialized
            */
            $scope.isCurrentView = function (name) {
                return $scope.currentView == name;
            };

            /**
            * @method show
            * @description shows current view
            */
            $scope.show = function (name) {
                $scope.currentView = name;
            };

        
            
            /**
            * @method removePost
            * @param id
            * @description Removes blog post by its id
            */
            $scope.removePost = function (post, index) {
                if ($window.confirm($scope.$resource('utility.areYouShure'))) {
                    BlogService.removePost({ id: post.ID }).then(function (response) {
                        if (response.Error) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            $scope.posts.splice(index, 1);
                            System.Message.success($scope.$resource('utility.deleted'));
                        }
                    });
                }
            };
            
            $scope.$watch("pager.page", function () {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

            $scope.refresh();
        }
    ]);