import { Router } from '@angular/router';
import {AlertController, LoadingController, NavController} from '@ionic/angular';
import {Component, OnInit, ViewChild} from '@angular/core';
import {WrenchService} from "../../services/wrench.service";
import {SessionDataProviderService} from "../../store/session-data-provider.service";

@Component({
  selector: 'app-new-password',
  templateUrl: './new-password.page.html',
  styleUrls: ['./new-password.page.scss'],
})
export class NewPasswordPage implements OnInit {
@ViewChild('confirm_new_password')  confirm_new_password;
@ViewChild('new_password')  new_password;

  resetResult;any;
  reset_pin:string='';
  constructor(
    private navctr: NavController,
    private router: Router,
    private wrenchService: WrenchService,
    public sessionDataProviderService:SessionDataProviderService,
    private loadingCtrl: LoadingController,
    private alertController: AlertController
  ) {
    this.resetResult  =  this.router.getCurrentNavigation().extras.state.resetResult;
    this.reset_pin  =  this.router.getCurrentNavigation().extras.state.reset_pin;
   // debugger;

    if ( this.resetResult != null
        &&  this.resetResult.internal_return == 0 &&
        this.resetResult.lostpass_id > 0 &&
        this.resetResult.reset_uid !='' &&
        this.resetResult.m_uid != '' ) {
    }
    else
    {
      this.onLogin();
    }
  }

  ngOnInit() {
  }

  onBack() {
    this.navctr.back();
  }

  onLogin() {
    this.router.navigate(['login']);
  }

  postData: {
    action:number,
    step: number,
    m_uid: string,
    reset_uid: string,
    reset_pin:  string,
    sessionid: string,
    reset_link: string,
    channel:'MOBILE',
    newpass: string
  };
  completeResetResults:any;

  async onCompleteNewReset(){
    const loading = await this.loadingCtrl.create({
      message: 'Please wait...',
      duration: 6000,
    });

    if (this.new_password.length < 8 || (this.new_password != this.confirm_new_password) ){
      this.showAlert('Password Reset Error', 'Enter new password to continue, must be 8 or more alphanumeric');
      return;
    }
    this.postData = {
      action:730,
      step: 300,
      m_uid: this.resetResult.m_uid,
      reset_uid: this.resetResult.reset_uid,
      reset_pin:  this.reset_pin,
      sessionid: 'DUMMY-CANNOT_BE_EMPTY',
      reset_link: 'DUMMY-CANNOT_BE_EMPTY',
      channel:'MOBILE',
      newpass: this.new_password
    };
    loading.present();
    this.wrenchService.verifyPassReset(this.postData).subscribe(
        completeResetResults => {
          this.completeResetResults = completeResetResults;
          console.log("completeResetResults RETURN->", this.completeResetResults);
          this.showAlert('Completed', 'Login with new password now');
          this.router.navigate(['login']);
          loading.dismiss();
        }
    );
  }
  async showAlert(mtitle: string, amessage: string) {
    const alert = await this.alertController.create({
      header: 'WrenchBoard',
      subHeader: mtitle,
      message: amessage,
      buttons: ['OK'],
    });
    await alert.present();
  }
}
