import { Component, OnInit } from "@angular/core";
import { ElementRef } from "@angular/core";
import { NavController } from "@ionic/angular";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { SharedDataService } from "src/app/hmg-common/services/shared-data-service/shared-data.service";
import { SMSCheckRequest } from "src/app/hmg-common/services/authentication/models/smscheck.request";
import { LoginModel } from "../models/LoginModel";
import { AuthenticationService } from "src/app/hmg-common/services/authentication/authentication.service";
import { SMSCheckResponse } from "src/app/hmg-common/services/authentication/models/smscheck.response";
import { Password } from "../models/password";

@Component({
  selector: "app-sms-page",
  templateUrl: "./sms-page.page.html",
  styleUrls: ["./sms-page.page.scss"]
})
export class SmsPagePage implements OnInit {
  public static LOGIN_DATA = "LOGIN_DATA";
  Channel: number = 0;
  activationCode: string;
  P_SESSION_ID: number;
  timeInSeconds: any;
  time: any;
  runTimer: any;
  hasStarted: any;
  hasFinished: any;
  remainingTime: any;
  displayTime: any;
  loginTokenID: string;
  public isForgetPwd: boolean = false;
  public isExpiredPwd: boolean = false;
  public count: number = 0;
  private loginData = new LoginModel();

  constructor(
    public navCtrl: NavController,
    public translate: TranslatorService,
    public common: CommonService,
    private elementRef: ElementRef,
    public authService: AuthenticationService,
    public sharedData: SharedDataService
  ) {}

  ngOnInit() {
    this.count = 0;
    this.initTimer();
    this.startTimer();
    this.isForgetPwd =
      this.sharedData.getSharedData(Password.IS_FORGET_PSW) || false;
  }

  initTimer() {
    // Pomodoro is usually for 25 minutes
    if (!this.timeInSeconds) {
      this.timeInSeconds = 600;
    }

    this.time = this.timeInSeconds;
    this.runTimer = false;
    this.hasStarted = false;
    this.hasFinished = false;
    this.remainingTime = this.timeInSeconds;

    this.displayTime = this.common.getSecondsAsDigitalClock(this.remainingTime);
  }

  startTimer() {
    this.runTimer = true;
    this.hasStarted = true;
    this.timerTick();
  }

  pauseTimer() {
    this.runTimer = false;
  }

  resumeTimer() {
    this.startTimer();
  }

  timerTick() {
    setTimeout(() => {
      if (!this.runTimer) {
        return;
      }
      this.remainingTime--;
      this.displayTime = this.common.getSecondsAsDigitalClock(
        this.remainingTime
      );
      if (this.remainingTime > 0) {
        this.timerTick();
      } else {
        this.hasFinished = true;
        this.pauseTimer();
        this.navCtrl.pop();
      }
    }, 1000);
  }

  ionViewWillLeave() {
    this.pauseTimer();
  }

  checkVerificationCode() {
    if (this.count < 3) {
      if (
        this.activationCode == undefined ||
        this.activationCode == null ||
        this.activationCode == ""
      ) {
        //  this.common.showAlert(this.translate.translate('verificationcode.emptyCode'));
      } else {
        this.count = this.count + 1;
        if (this.isForgetPwd || this.isExpiredPwd) {
          this.checkForgetPwdSMS();
        } else {
          this.checkSMS();
        }
      }
    } else {
      let msg: string = this.translate.trPK("general", "noOfTriesLogin");
      this.common.JustAlertDialog(this.translate.trPK("general", "ok"), msg);
    }
  }

  public checkSMS() {
    const data = this.sharedData.getSharedData("logindata", false);
    const request = new SMSCheckRequest();

    (request.LogInTokenID = data.LogInTokenID),
      (request.activationCode = this.activationCode);
    request.MobileNumber = data.MobileNumber;
    this.authService
      .checkSMS(request, () => {}, this.translate.trPK("general", "ok"))
      .subscribe((result: SMSCheckResponse) => {
        console.log(result);
        if (this.common.validResponse(result)) {
          this.authService.setAuthenticatedUser(result).subscribe(() => {
            this.common.openHome();
          });
        }
      });
  }

  public checkForgetPwdSMS() {
    const data = this.sharedData.getSharedData("logindata", false);
    const request = new SMSCheckRequest();

    (request.LogInTokenID = data.LogInTokenID),
      (request.activationCode = this.activationCode);
    console.log(data.LogInTokenID);

    this.authService
      .checkForgetSMS(request, () => {}, this.translate.trPK("general", "ok"))
      .subscribe((result: SMSCheckResponse) => {
        console.log(result);
        if (this.common.validResponse(result)) {
          console.log(result);
          this.common.openForgotPassword();
        }
      });
  }
}
