'use strict';

var VideoSettingsController = angular.module('videos.controllers.settings', [
    'system'
])
    .controller('VideoSettingsController', [
        '$scope',
        '$route',
        '$location',
        '$filter',
        'System',
        'VideoService',
        function ($scope, $route, $location, $filter, System, VideoService) {
            $(".tooltip").tooltip();

            $scope.refresh = function() {
                VideoService.getSettings()
                    .then(function(data) {
                        $scope.settings = data;
                    });
            };

            $scope.save = function() {
                VideoService.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('/videos');
                        }
                        
                    });
            };
            
            $scope.reset = function () {
                VideoService.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();
                        }
                    });
            };

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

            $scope.refresh();
        }
    ]);