/// <reference path="../../includes.ts" />

module acctMgmtLoginDetailsCtrl {

	import IUser = services.IUser;

    interface IAcctMgmtLoginDetailsController {
		passwordStrengthMeter(): any;
		saveLoginDetails(): any;
	}

	interface IAcctMgmtLoginDetailsScope extends angular.IScope {
		myForm: angular.IFormController
	}

	class AcctMgmtLoginDetailsController implements IAcctMgmtLoginDetailsController{

		static $inject = [
			'passwordStrengthSrvc',
			'$rootScope',
			'appMemorySrvc',
			'apiSrvc',
			'mydnaApis',
			'errorHandlerSrvc',
			'$scope',
			'scrollerSrvc',
			'authenSrvc',
			'userService'
		];

		frm: any;
		tabIndexValue: any;
		showErrors: any;
		strengthClassName: string;
		strengthUserText: string;
		savedSuccesfully: boolean;
		currentPasswordFailed: boolean;

		constructor(
			private passwordStrengthSrvc: passwordStrengthSrvc.IPasswordStrengthService,
			private $rootScope: angular.IRootScopeService,
			private appMemorySrvc: appMemorySrvc.IAppMemoryService,
			private apiSrvc: apiSrvc.IApiService,
			private mydnaApis: ImyDNAApis,
			private errorHandlerSrvc: errorHandlerSrvc.IErrorHandlerService,
			private $scope: IAcctMgmtLoginDetailsScope,
			private scrollerSrvc: scrollerSrvc.IScrollerService,
			private authenSrvc: authenSrvc.IAuthenService,
			private userService: services.IUserService
			){
			var vm = this;
			vm.frm = {};

			vm.frm.NewPassword = "";
			vm.frm.ConfirmPassword = "";
			vm.savedSuccesfully = false;
			vm.showErrors = false;
			vm.strengthClassName = "";
			vm.strengthUserText = "";
			vm.currentPasswordFailed = false;

            userService.user().then((user: IUser) => {
                vm.frm.Username = user.Username;
			});

			$rootScope.$on("resetTabIndexValue", (event, message) => {
				vm.tabIndexValue = "0";
			});

			$rootScope.$on("setTabIndexValue", (event, message) => {
				vm.tabIndexValue = "-1";
			});

		}

		passwordStrengthMeter() {
			let strength = this.passwordStrengthSrvc.checkStrength(this.frm.Password);
			this.strengthClassName = strength.className;
			this.strengthUserText = strength.userText;
		}

		saveLoginDetails(){
			
			this.scrollerSrvc.TopScroll();
			this.showErrors = this.showErrors == true ? false : "";
			if (this.$scope.myForm.$valid) {
				this.$scope.$emit("disable-on-save-on");
				this.apiSrvc.post_request(this.mydnaApis.LoginDetails, this.frm).then(data => {
						this.savedSuccesfully = true;
						this.currentPasswordFailed = false;
						this.frm.CurrentPassword = this.frm.NewPassword = this.frm.ConfirmPassword = '';
						this.strengthClassName = "";
						this.strengthUserText = "";
						this.$scope.$emit("disable-on-save-off");

					}, err => {
						if(err.data.Message == 'Incorrect password.' && err.status == 400){
							this.currentPasswordFailed = true;
						}
						else{
							this.errorHandlerSrvc.errorHandler(err, 'debug', '');
						}
						this.$scope.$emit("disable-on-save-off");
					});
			}
			else {
				this.showErrors = true;
			}
		}

	}

	angular
		.module('app')
		.controller('acctMgmtLoginDetailsCtrl', AcctMgmtLoginDetailsController);
}