'use strict';

/** 
 * @controller ArticleMainController
 * @property scope 
 * @property location
 * @property route
 * @property System
 * @property ArticleService
 */

var ArticleMainController = angular.module('articles.controllers.main', [
    'system'
])
    .controller('ArticleMainController', [
        '$scope',
        '$location',
        '$route',
        'System',
        'ArticleService',
        'ClassificationService',
        function ($scope, $location, $route, System, ArticleService, ClassificationService) {
            System.Page.title("Vijesti");
            $scope.query = $route.current.params || {}, 
            $scope.query.Culture = Globalize.culture().name ;
            $scope.sorter = System.Sorter.create($scope.query);
            $scope.selection = {};
            $(".tooltip").tooltip();

            /**
            * @property sidebarView
            */
            $scope.sidebarView = 'basic';

            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                ArticleService.query($scope.query).then(function (data) {
                    $scope.articles = data.articles;
                    $scope.pager = System.Pager.create({ page: $scope.query.page, total: data.total });
                });
            };


            $scope.getClassifications = function () {
                ClassificationService.getByAlias('tag').then(function (response) {
                    $scope.tags = response;
                });
                ClassificationService.getByAlias('vijesti').then(function (response) {
                    $scope.categories = response;
                });

            };

            /**
            * @method isSearch
            * @description returns value indicates if search is running
            */
            $scope.isSearch = function () {
                return Object.keys($scope.query).length !== 0;
            };

            /**
            * @method reset
            * @description resets search to its initial value
            */
            $scope.reset = function () {
                $scope.query.Culture = Globalize.culture().name;
                $location.search(($scope.query = { }));
                $scope.refresh();
            };

            /**
           * @method search
           * @description searches articles 
           */
            $scope.search = function(q, reset) {
                reset ? $scope.query = q : angular.extend($scope.query, q);
                $location.search($scope.query);
                $scope.refresh();
            };

            $scope.switchSidebar = function(view) {
                $scope.sidebarView = view;
            };


            /**
           * @method reset
           * @description resets search to its initial value
           */
            $scope.reset = function () {
                $location.search(($scope.query = {}));
            };
            /**
          * @method sort
          * @description sorts articles
          */
            $scope.sort = function(by, dir) {
                $scope.sorter.sort(by, dir);
                $location.search($scope.query);
                $scope.refresh();
            };

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

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

            /**
            * @action edit redirects user to edit form
            * @param article
            */
            $scope.edit = function(article) {
                $location.url('/articles/' + article.ID + '/edit');
            };

            /**
           * @action edit redirects user to edit form
           * @param article
            */
            $scope.facebook = function (article) {
                $location.url('/facebook/' + article.ID);
            };
            /**
            * @action new redirects user to create form
            */
            $scope.new = function() {
                $location.url('/articles/create');
            };

            /**
            * @action select selects single article and redirects user to article details
            * @param article article instance to view details for
            */
            $scope.select = function(article) {
                $location.url('/articles/' + article.ID);
            };

            /**
            * @method remove
            * @param article
            * @param index
            * @description removes article by its id and removes it from local article list by its index
            */
            $scope.remove = function(article, index) {
                if (window.confirm($scope.$resource('utility.areYouShure'))) {
                    console.log(article);
                    ArticleService.remove({ id: article.ID})
                        .then(function (response) {
                            if (response.Error) {
                                $scope.errors = response.Errors;
                                System.Message.error($scope.$resource('utility.changeNotSave'));
                            } else {
                                $scope.articles.splice(index, 1);
                                $scope.refresh();
                                System.Message.success($scope.$resource('utility.deleted'));
                            }
                        });
                }
            };

            /**
            * @method link returns to location article.ExternalLink
            * @param article article instance to generate link for
            */
            $scope.link = function(article) {
                $location.url(article.ExternalLink);
            };

            $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();
            $scope.getClassifications();
        }
    ]);