'use strict';

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

            $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 });
                });
                System.Page.title("Portfolio");
            };

     

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

            $scope.isSearch = function () {
                return Object.keys($scope.query).length !== 0;
            };

            $scope.reset = function () {
                $scope.query.Culture = Globalize.culture().name;
                $location.search(($scope.query = {}));
                $scope.refresh();
            };

            $scope.selectLibrary = function (tip) {
                if (tip != null)
                    $location.url('/documents/' + tip);
                else
                    $location.url('/documentst/');
            };


            $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;
            };


            $scope.reset = function () {
                $location.search(($scope.query = {}));
            };

            $scope.sort = function (by, dir) {
                $scope.sorter.sort(by, dir);
                $location.search($scope.query);
                $scope.refresh();
            };

            $scope.back = function () {
                $location.url('/articles');
            };

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