'use strict';
app.controller('userCreateController', ['$scope', '$http', '$modalInstance', '$translate', 'blockUI', 'lib', function ($scope, $http, $modalInstance, $translate, blockUI, lib) {

	$scope.username = '';
	$scope.password = '';
	$scope.passwordConfirm = '';

	var init = function () {
	};

	$scope.submit = function () {
		
		if ($scope.username === '') {
			$translate(['message', 'ucFillUsername']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.ucFillUsername);
			});
			return;
		}
		var typedPassword = $scope.password !== '' || $scope.passwordConfirm !== '';
		if (typedPassword && $scope.password !== $scope.passwordConfirm) {
			$translate(['message', 'ucPasswordsDoNotMatch']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.ucPasswordsDoNotMatch);
			});
			return;
		}

		$http.post('/Api/Users/', {
			username: $scope.username,
			password: $scope.password
		}).then(function (response) {
			$translate(['message', 'ucSuccess']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.ucSuccess, function (alertDialog) {
					alertDialog.close();
					$modalInstance.close();
				});
			});
		}, lib.handleRestError);

	};

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

	init();
}]);
