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 { CheckUserAuthenticationRequest } from 'src/app/hmg-common/services/authentication/models/check-user-auth.request';
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 { InternationalMobileComponent } from 'src/app/hmg-common/ui/mobile-number/international-mobile/international-mobile.component';
import { LoginRequest } from 'src/app/hmg-common/services/authentication/models/login.request';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import { Password } from '../models/password';
import { LoginModel } from '../models/LoginModel';
import {LoginComponent} from 'src/app/authentication/login/login.component';
@Component({
  selector: 'app-check-user',
  templateUrl: './check-user.component.html',
  styleUrls: ['./check-user.component.scss'],
})
export class CheckUserComponent implements OnInit {
  public P_USER_NAME : string;
  private loginData = new LoginModel();
  
  @ViewChild(InternationalMobileComponent) internationlMobile: InternationalMobileComponent;
  constructor(public cs: CommonService,
    public authService: AuthenticationService,
    public router: Router,
    public alertController: AlertController,
    public ts: TranslatorService,
    public smsService: SmsReaderService,
    public sharedData: SharedDataService,
    public changeDetector: ChangeDetectorRef) { }

  ngOnInit() {}

  public onForgot() {
      this.sendSMSForForgotPassword();
  }

  public userCheck()
  {
    this.cs.startLoading();
    const request = new CheckUserAuthenticationRequest();
    request.P_USER_NAME = this.P_USER_NAME; 
    this.authService.checkUserAuthentication(
      request,
      () => {
            /* Write code for error */
            }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => {
                if (this.cs.validResponse(result)) {
                    this.checkUserResult = result;
                    this.cs.stopLoading();
                    this.sendSMSForForgotPassword();
                }
            });
  }

  private checkUserResult: CheckUserAuthenticationResponse;

    private sendSMSForForgotPassword() {
      this.cs.startLoading();
      let changePwdObj= new LoginRequest();
      changePwdObj.MobileNumber= this.checkUserResult.BasicMemberInformation.P_MOBILE_NUMBER;
      changePwdObj.P_USER_NAME=this.P_USER_NAME;
      changePwdObj.P_MOBILE_NUMBER=this.checkUserResult.BasicMemberInformation.P_MOBILE_NUMBER;
      
      this.authService.sendPublicSMS(
        changePwdObj,
        () => {
              //this.sendSMSForForgotPassword();
              /* Write code for error */
              }, this.ts.trPK('general', 'ok')).subscribe((result: CheckUserAuthenticationResponse) => {
              if (this.cs.validResponse(result)) {
                  this.checkUserResult = result;
                  console.log("2");
                  console.log(result);
                  this.loginData.LogInTokenID = result.LogInTokenID;
                  this.loginData.P_USER_NAME = this.P_USER_NAME;
                  this.sharedData.setSharedData(this.loginData, AuthenticationService.LOGIN_DATA);
                  this.sharedData.setSharedData(true,Password.IS_FORGET_PSW);
                  this.cs.stopLoading();
                  this.cs.openSMSPage();
              }});
    }
}