'use strict';
/** 
 * @controller ClassificationMainController
 * @property scope 
 * @property route
 * @property location
 * @property System
 * @property ClassificationService
 */
var ClassificationMainController = angular.module('classifications.controllers.main', [
    'system'
])
    .controller('ClassificationMainController', [
        '$scope',
        '$route',
        '$window',
        '$location',
        'System',
        'ClassificationService',
        function($scope, $route, $window, $location, System, ClassificationService) {

            $scope.query = $route.current.params || { };
            $scope.newClassification = { ClassificationType: 0 };
            
            $(".tooltip").tooltip();

            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                ClassificationService.query($scope.query).then(function(data) {
                    $scope.classifications = data.classifications;
                });
            };
            
            /**
            * @method reset
            * @description resets search to its initial value
            */
            $scope.reset = function () {
                $location.search(($scope.query = {}));
                $scope.refresh();
            };
            
            /**
          * @method search
          * @description searches classifications
          */
            $scope.search = function (q, reset) {
                reset ? $scope.query = q : angular.extend($scope.query, q);
                $location.search($scope.query);
                $scope.refresh();
            };
            
            /**
            * @method isSearch
            * @description returns value indicates if search is running
            */
            $scope.isSearch = function () {
                return Object.keys($scope.query).length !== 0;
            };

            /**
            * @method select
            * @description redirects user to the list of labels for the current classification
            */
            $scope.select = function(classification) {
                $location.url('/classifications/' + classification.Alias);
            };

            /**
            * @method create
            * @description creates new classification
            */
            $scope.create = function() {
                ClassificationService.create($scope.newClassification)
                    .then(function (classification) {
                        if (classification.Errors) {
                            $scope.errors = classification.Errors;
                            System.Message.error($scope.$resource('utility.changeNotSave'));
                        } else {
                            $scope.classifications.push(classification);
                            $scope.newClassification = { ClassificationType: 0 };
                            System.Message.success($scope.$resource('classifications.ClassCreated'));
                        }
                    });
            };


            /**
            * @method remove
            * @param id
            * @description Removes classifications
            */
            $scope.remove = function(classification) {
                if ($window.confirm($scope.$resource('utility.areYouShure'))) {
                    if (classification.Labels.length > 0)
                        System.Message.error($scope.$resource('classifications.labels_first'));
                    else {
                        System.Message.success($scope.$resource('utility.deleted'));
                        ClassificationService.remove({ id: classification.ID })
                            .then(function() {
                                $scope.refresh();
                            });
                    }
                }
            };

            /**
            * @method edit
            * @description redirects user to label edit form.
            */
            $scope.edit = function (classification) {
                $location.url('/classifications/' + classification.ID + '/edit');
            };

            $scope.refresh();
        }
    ]);