'use strict';

var EventMainController = angular.module('events.controllers.main', [
    'system'
])
    .controller('EventMainController', [
        '$scope',
        '$location',
        '$filter',
        '$route',
        '$window',
        'System',
        'EventService',
        'ClassificationService',
        function ($scope, $location, $filter, $route, $window, System, EventService, ClassificationService) {
            $scope.query = $route.current.params || {};
            $(".tooltip").tooltip();
            $scope.sorter = System.Sorter.create($scope.query);
            $scope.sidebarView = 'classifications';
            $scope.refresh = function() {
                EventService.query($scope.query).then(function (data) {
                    $scope.events = data.events;
                    $scope.pager = System.Pager.create({ page: $scope.query.page, total: data.total });
                });

                ClassificationService.query($scope.query).then(function(data) {
                    $scope.classifications = data.classifications;
                });
            };
            
            $scope.edit = function(event) {
                $location.path('/events/' + event.ID + '/edit');
            };

            $scope.new = function() {
                $location.path('/events/create');
            };

            $scope.select = function (event) {
                $location.url('/events/' + event.ID);
            };
            
            /**
            * @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 () {
                $location.search(($scope.query = {}));
                $scope.refresh();
            };

            /**
            * @method search
            * @description searches events 
            */
            $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 sort
            * @description sorts events
            */
            $scope.sort = function (by, dir) {
                $scope.sorter.sort(by, dir);
                $location.search($scope.query);
                $scope.refresh();
            };

            /**
            * @method remove
            * @param id
            * @description Removes blog by its id
            */
            $scope.remove = function (event, index) {
                if ($window.confirm($scope.$resource('utility.areYouShure'))) {
                    EventService.remove({ id: event.ID }).then(function (response) {
                        if (response.Error) {
                            $scope.errors = response.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            $scope.events.splice(index, 1);
                            System.Message.success($scope.$resource('utility.deleted'));
                        }
                    });
                }
            };

            $scope.link = function(event) {
                $location.url(event.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();

        }
    ]);