'use strict';
app.controller('systemStatusController', ['$scope', '$http', '$modal', '$translate', 'lib', 'authService', 'blockUI', '$q', function ($scope, $http, $modal, $translate, lib, authService, blockUI, $q) {

	$scope.dateFormats = ['Standard', 'Long', 'Short', 'LongDateShortTime', 'ShortDateLongTime'];
	$scope.storageServices = ['Disabled', 'Local', 'Azure', 'AmazonS3'];
	$scope.cacheConfigurations = ['Disabled', 'InMemory'];
	$scope.bStamperConfigurations = ['Disabled', 'Enabled'];

	$scope.systemStatus = null;
	$scope.regionAndTime = false;
	$scope.settings = null;
	$scope.model = { fee: null };

	var init = function () {
		if (!lib.checkIsAdmin()) {
			return;
		}
		$http.get('/Api/SystemStatus').then(function (response) {
			$scope.systemStatus = response.data;
			$scope.storageServiceAvailable = response.data.storageState === 'Ready' || response.data.storageState === 'ReadyAndEmpty';
		}, lib.handleRestError);
		$http.get('/Api/Admin/RegionAndTime').then(function (response) {
			$scope.regionAndTime = response.data;
		}, lib.handleRestError);
		$http.get('/Api/Settings').then(function (response) {
			$scope.settings = {};
			angular.forEach(response.data, function (e) {
				$scope.settings[e.key] = e.value;
			});
		}, lib.handleRestError);
		$http.get('/Api/TaskStatus').then(function (response) {
			$scope.tasksStatus = response.data;
		}, lib.handleRestError);
	};

	$scope.editSetting = function (key) {
		return $q.when({ key: key, value: $scope.settings[key] });
	};

	$scope.editSensitiveSetting = function (key) {
		return $q.when({ key: key, value: '' });
	};

	$scope.saveSetting = function (key, value) {
		blockUI.start();
		return $http.put('/Api/Settings/' + key, { value: value }).then(function (response) {
			$scope.settings[key] = response.data.value;
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.saveStorageSetting = function (key, value) {
		blockUI.start();
		return $http.put('/Api/Settings/' + key, { value: value }).then(function (response) {
			$scope.settings[key] = response.data.value;
			blockUI.stop();
			init();
		}, lib.handleRestError);
	};

	$scope.saveRegionAndTime = function (key, value) {
		var data = {};
		data[key] = value;
		blockUI.start();
		return $http.put('/Api/Admin/RegionAndTime/', data).then(function (response) {
			$scope.regionAndTime = response.data;
			blockUI.stop();
		}, lib.handleRestError);
	};

	$scope.editDateFormat = function () {
		return $q.when({ key: 'dateFormat', value: $scope.regionAndTime.dateFormat });
	};

	$scope.editTimeZone = function () {
		var promise = $q.when({ key: 'timeZoneId', value: $scope.regionAndTime.timeZone.id });
		if (!$scope.timezones) {
			blockUI.start();
			return $http.get('/Api/System/TimeZones').then(function (response) {
				blockUI.stop();
				$scope.timezones = response.data;
				return promise;
			}, lib.handleRestError);
		}
		return promise;
	};

	$scope.editCulture = function () {
		var promise = $q.when({ key: 'cultureId', value: $scope.regionAndTime.culture.id });
		if (!$scope.cultures) {
			blockUI.start();
			return $http.get('/Api/System/Cultures').then(function (response) {
				blockUI.stop();
				$scope.cultures = response.data;
				return promise;
			}, lib.handleRestError);
		}
		return promise;
	};

	$scope.toggleIsOnPremises = function () {
		$http.put('/Api/Settings/IsPrivateInstance', {
			value: (!$scope.systemStatus.isOnPremises).toString()
		}).then(function (response) {
			$scope.systemStatus.isOnPremises = !$scope.systemStatus.isOnPremises;
			blockUI.stop();
			$translate(['message', 'ssConfigChanged']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.ssConfigChanged);
			});
		}, lib.handleRestError);
	};

	$scope.testLog = function () {
		$modal.open({
			templateUrl: views.logTestDialog,
			controller: 'logTestController'
		});
	};

	$scope.changeSdkLicense = function () {
		$modal.open({
			templateUrl: views.sdkLicenseConfigDialog,
			controller: 'sdkLicenseConfigController'
		}).result.then(init, init);
	};

	$scope.editStorageService = function () {
		var editData = { key: 'StorageService', value: $scope.settings['StorageService'] };
		if ($scope.systemStatus.storageState !== 'Ready') {
			return $q.when(editData);
		}
		return $translate('ssStorageNotEmpty').then(function (confirmMsg) {
			return $translate(['areYouSure', 'yes', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.yes] = {
					css: 'btn-danger',
					click: function (modalInstance) {
						modalInstance.close();
					}
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.dismiss();
					}
				};
				return lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons).result.then(function () {
					return editData;
				});
			});
		});
	};

	init();
}]);
