'use strict';

/** 
 * @controller DocumentSettingsController
 * @property scope 
 * @property route
 * @property location
 * @property filter
 * @property System
 * @property DocumentService
 * @property Resource
 */

angular.module('documents.controllers.settings', [
    'system'
])
    .controller('DocumentSettingsController', [
        '$scope',
        '$route',
        '$location',
        '$filter',
        'System',
        'DocumentService',
        function ($scope, $route, $location, $filter, System, DocumentService) {
            $(".tooltip").tooltip();

            /**
            * @method refresh
            * @description refreshes settings data
            */
            $scope.refresh = function() {
                DocumentService.getSettings()
                    .then(function(data) {
                        $scope.settings = data;
                    });
            };

            /**
            * @method save
            * @description updates Documents settings
            * 
            */
            $scope.save = function() {
                DocumentService.saveSettings($scope.settings)
                    .then(function (data) {
                        if (data.Errors) {
                            $scope.errors = data.Errors;
                            System.Message.error($scope.$resource("utility.changeNotSave"));
                        } else {
                            System.Message.success($scope.$resource('utility.settingsSaved'));
                            $location.url('/documents');
                        }
                    });
            };
            
            /**
            * @method reset
            * @description Resets Document settings
            * 
            */
            $scope.reset = function () {
                DocumentService.removeSettings($scope.settings)
                    .then(function (data) {
                        if (data.Errors) {
                            $scope.errors = data.Errors;
                            System.Message.error($scope.$resource("utility.changeNotSave"));
                        } else {
                            System.Message.success($scope.$resource('utility.settingsReset'));
                            $scope.refresh();
                        }
                    });
            };

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

            $scope.refresh();
        }
    ]);