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

	$scope.oldPassword = '';
	$scope.newPassword = '';
	$scope.passwordConfirm = '';

	$scope.submit = function () {
		
		if ($scope.oldPassword === '') {
			$translate(['message', 'cpFillOldPass']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.cpFillOldPass);
			});
			return;
		}
		if ($scope.newPassword === '') {
			$translate(['message', 'cpFillNewPass']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.cpFillNewPass);
			});
			return;
		}
		if ($scope.passwordConfirm === '') {
			$translate(['message', 'cpFillPasswordConfirm']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.cpFillPasswordConfirm);
			});
			return;
		}
		if ($scope.newPassword !== $scope.passwordConfirm) {
			$translate(['message', 'passwordsDoNotMatch']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.passwordsDoNotMatch);
			});
			return;
		}

		blockUI.start();
		$http.post('/Api/Account/ChangePassword', {
			oldPassword: $scope.oldPassword,
			newPassword: $scope.newPassword,
			confirmPassword: $scope.newPassword // we already validated that newPassword and passwordConfirm match
		}).then(function (response) {
			blockUI.stop();
			$translate(['message', 'cpSuccess']).then(function (msgs) {
				lib.showAlert(msgs.message, msgs.cpSuccess, function (alertDialog) {
					alertDialog.close();
					$modalInstance.close();
				});
			});
		}, lib.handleIdentityError);
	};

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