'use strict';
app.controller('userDetailsController', ['$scope', '$http', '$modalInstance', '$timeout', '$modal', '$translate', '$location', 'blockUI', 'lib', 'notifyService', 'userId', function ($scope, $http, $modalInstance, $timeout, $modal, $translate, $location, blockUI, lib, notifyService, userId) {

	$scope.model = null;

	var init = function () {
		blockUI.start();
		$http.get('/Api/Users/' + userId).then(function (response) {
			$scope.model = response.data;
			blockUI.stop();
		}, function (response) {
			lib.showRestError(response, function () {
				$modalInstance.close();
			});
		});
	};

	$scope.resetPassword = function () {
		$modal.open({
			templateUrl: views.resetPasswordDialog,
			controller: 'resetPasswordDialogController',
			//size: 'lg',
			resolve: {
				userId: function () {
					return userId;
				}
			}
		}).result.then(init);
	};

	$scope.clearPassword = function () {
		$translate('udConfirmClearPassword', { username: $scope.model.username }).then(function (confirmMsg) {
			$translate(['areYouSure', 'yesClear', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.yesClear] = {
					css: 'btn-danger',
					click: function (modalInstance) {
						modalInstance.close();
						doClearPassword();
					}
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.dismiss();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	var doClearPassword = function () {
		blockUI.start();
		$http.delete('/Api/Users/' + userId + '/Password').then(function () {
			$scope.model.hasPassword = false;
			blockUI.stop();
			$translate('udPasswordCleared').then(function (msg) {
				notifyService.success(msg);
			});
		}, lib.handleError);
	};

	$scope.grantAdmin = function () {
		$translate('udConfirmGrantAdmin', { username: $scope.model.username }).then(function (confirmMsg) {
			$translate(['areYouSure', 'udYesGrant', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.udYesGrant] = {
					css: 'btn-warning',
					click: function (modalInstance) {
						modalInstance.close();
						setIsAdmin(true);
					}
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.close();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	$scope.revokeAdmin = function () {
		$translate('udConfirmRevokeAdmin', { username: $scope.model.username }).then(function (confirmMsg) {
			$translate(['areYouSure', 'udYesRevoke', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.udYesRevoke] = {
					css: 'btn-danger',
					click: function (modalInstance) {
						modalInstance.close();
						setIsAdmin(false);
					}
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.close();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	var setIsAdmin = function (isAdmin) {
		blockUI.start();
		var model = $.extend(true, {}, $scope.model);
		model.isAdmin = isAdmin;
		$http.put('/Api/Users/' + userId, model).then(function (response) {
			$scope.model = response.data;
			blockUI.stop();
		}, lib.showRestError);
	};

	$scope.remove = function () {
		$translate('udConfirmRemove', { username: $scope.model.username }).then(function (confirmMsg) {
			$translate(['areYouSure', 'yesRemove', 'cancel']).then(function (msgs) {
				var buttons = {};
				buttons[msgs.yesRemove] = {
					css: 'btn-danger',
					click: removeUser
				};
				buttons[msgs.cancel] = {
					css: 'btn-default',
					click: function (modalInstance) {
						modalInstance.close();
					}
				};
				lib.showMessageBox(confirmMsg, msgs.areYouSure, buttons);
			});
		});
	};

	var removeUser = function (confirmModal) {
		blockUI.start();
		var url = '/Api/Users/' + userId;
		$http.delete(url).then(function (response) {
			blockUI.stop();
			confirmModal.close();
			$scope.close();
		}, lib.handleRestError);
	};

	$scope.generateToken = function () {
		blockUI.start();
		$http.get('/Api/Users/' + userId + '/LongTermToken').then(function (response) {
			blockUI.stop();
			$modal.open({
				templateUrl: views.tokenDialog,
				controller: 'tokenDialogController',
				resolve: {
					token: function () { return response.data.accessToken; }
				}
			});
		}, lib.handleError);
	};

	$scope.showEvents = function () {
		$location.path('/events').search('userId', userId);
		$modalInstance.close();
	};

	$scope.showTransactions = function () {
		$location.path('/transactions').search('userId', userId);
		$modalInstance.close();
	};

	$scope.close = function () {
		$modalInstance.close();
	};

	init();
}]);
