import { Component, OnInit, ViewChild, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service';
import { Router } from '@angular/router';
import { AlertController } from '@ionic/angular';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { CheckUserAuthenticationResponse } from 'src/app/hmg-common/services/authentication/models/check-user-auth.response';
import { SmsReaderService } from 'src/app/hmg-common/services/sms/sms-reader.service';
import { ForgetPassword } from '../models/forget.password';
import { LoginModel } from '../models/LoginModel';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import {LoginComponent} from "src/app/authentication/login/login.component";
import { Password } from '../models/password';
import { AuthenticatedUser } from "src/app/hmg-common/services/authentication/models/authenticated-user";

@Component({
  selector: 'app-change-password',
  templateUrl: './change-password.component.html',
  styleUrls: ['./change-password.component.scss'],
})
export class ChangePasswordComponent implements OnInit {
  private loginData = new LoginModel();
  public P_NEW_PASSWORD: string;
  public P_Confirm_NEW_PASSWORD : string;
  public P_OLD_PASSWORD : string;
  public P_USER_NAME : string;
  public logo = "assets/icon/login/lock.png";

  userData: any = {};
  public isExpiredPwd: boolean = false;
  constructor(
    public cs: CommonService,
    public authService: AuthenticationService,
    public router: Router,
    public alertController: AlertController,
    public ts: TranslatorService,
    public smsService: SmsReaderService,
    public changeDetector: ChangeDetectorRef,
    public sharedData: SharedDataService) { 
      console.log("change password constructor");
    }

  ngOnInit() {
    console.log("change password oninit");
    this.isExpiredPwd =
        this.sharedData.getSharedData(Password.IS_EXPIRED_PSW) || false;
    const data = this.sharedData.getSharedData(AuthenticationService.LOGIN_DATA, false);
    this.P_USER_NAME =data.P_USER_NAME;
    console.log(this.isExpiredPwd);
    console.log(this.P_USER_NAME);
    // this.authService
    //   .loadAuthenticatedUser()
    //   .subscribe((user: AuthenticatedUser) => {
    //     if (user) {
    //       this.userData = user;
    //       this.P_USER_NAME = user.EMPLOYEE_NUMBER; 
    //       console.log(user);
    //     } else {
    //       console.log(user);
    //     }
    //   });
  }


  private checkUserResult: CheckUserAuthenticationResponse;


  submit(){
    console.log(this.isExpiredPwd);
    if(this.isExpiredPwd){
      this.expiredPassword();
    }else{
      this.changePassword();
    }
  }

  public expiredPassword(){
    const data = this.sharedData.getSharedData(AuthenticationService.LOGIN_DATA, false);
    //this.loginData.P_USER_NAME = this.P_USER_NAME;
    let request:Password = new Password();
    request.P_OLD_PASSWORD=this.P_OLD_PASSWORD;
    request.P_Confirm_NEW_PASSWORD=this.P_NEW_PASSWORD;
    request.P_NEW_PASSWORD=this.P_Confirm_NEW_PASSWORD;
    request.P_USER_NAME = this.P_USER_NAME;

    this.authService.submitExpiredPassword(
        request,
        () => {
            //this.sendSMSForForgotPassword();
        }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => {
            if (this.cs.validResponse(result)) {
                this.checkUserResult = result;
                console.log(result);
                this.cs.toastPK("changePassword","successChange");
                this.sharedData.setSharedData(false,Password.IS_EXPIRED_PSW);
                this.cs.openLogin();
            }
        });
  }
  public changePassword(){
    const data = this.sharedData.getSharedData(AuthenticationService.LOGIN_DATA, false);
    //this.loginData.P_USER_NAME = this.P_USER_NAME;
    let request:Password = new Password();
    request.P_OLD_PASSWORD=this.P_OLD_PASSWORD;
    request.P_Confirm_NEW_PASSWORD=this.P_NEW_PASSWORD;
    request.P_NEW_PASSWORD=this.P_Confirm_NEW_PASSWORD;
    request.P_USER_NAME = this.P_USER_NAME;

    this.authService.submitChangePassword(
        request,
        () => {
            //this.sendSMSForForgotPassword();
        }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => {
            if (this.cs.validResponse(result)) {
                this.checkUserResult = result;
                console.log(result);
                this.cs.toastPK("changePassword","successChange");
                this.cs.openHome();
            }
        });
  }

  
}
