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

@Component({
  selector: 'app-forgot',
  templateUrl: './forgot.page.html',
  styleUrls: ['./forgot.page.scss'],
})
export class ForgotPage implements OnInit {
  @ViewChild('username') username;
  @ViewChild('password') password;
  @ViewChild('reset_pin') reset_pin;
  tab = 1
  reset_uid:string = '';
  m_uid:string = '';
  password_reset_id:number = 0;

  constructor(
      private router: Router,
      private wrenchService: WrenchService,
      public sessionDataProviderService:SessionDataProviderService,
      public blogDataService:BlogDataService,
      public alertController: AlertController
  ) {

  }

  ngOnInit() {
  }


  reqData: {
    'action':11013, email: string, sessionid: string, channel:'MOBILE'
  };
  resetResult:any;
  onEnterEmail(){
    this.reset_uid = '';
    this.m_uid = '';
    if (this.username == null || this.username == '' || this.validateEmail(this.username) == false) {
      this.showAlert('Error', 'Enter username(email) to continue');
      return;
    }

    this.reqData = { 'action':11013,email: this.username,  sessionid: 'DUMMY-APP-SESSION', channel:'MOBILE' }
    this.wrenchService.startPassReset(this.reqData).subscribe(
        resetResult => {
          this.resetResult = resetResult;
          console.log("INTERNAL RETURN->" + this.resetResult);
          /*
          'm_uid' => string '43c766d9-403d-4125-9983-63c21960b187' (length=36)
  'password_reset_id' => string '109' (length=3)
  'reset_uid' => string '48dc78ef-c5c4-4e3a-937c-05f79eeb3f0a' (length=36)
  'result' => string 'YES I GET TO BACK END' (length=21)
  'server_name' => string 'https://dev-users.wrenchboard.com' (length=33)
  'internal_return' => int 0
           */
          if (resetResult != null
              && resetResult.internal_return == 0 &&
               resetResult.password_reset_id > 0 &&
               resetResult.reset_uid !='' &&
               resetResult.m_uid != '' ) {
               this.reset_uid = resetResult.reset_uid;
               this.m_uid = resetResult.m_uid;
               this.password_reset_id = resetResult.password_reset_id;
               this.tab =3;
          }
          else
          {
              this.showAlert("Password Reset Error","We are unable to complete the process, please contact support");
             // alert("SOME WENT WROMNG");
          }

        }
    );


  }
  // onPassword() {
  //   this.router.navigate(['new-password']);
  // }

  verifyData: {
    'action':730,
     email: string,
     sessionid: string,
     reset_uid: string,
     m_uid: string,
     reset_pin: string,
     step:200,
     channel:'MOBILE'
  };
/*
   'sessionid' => string 'DUMMY-CANNOT_BE_EMPTY' (length=21)
  'reset_link' => string '36dfec6e557768511ffc87bdc861fac4543ae5758f933f8a7c92a7a70960fa3f' (length=64)
  'newpass' => string 'your-new-password' (length=17)
  'step' => int 300
  'action' => int 730

    this.reset_uid = resetResult.reset_uid;
               this.m_uid = resetResult.m_uid;
               this.password_reset_id = resetResult.password_reset_id;

 */
  onCompleteReset(){

    if (this.username == null || this.username == '' || this.validateEmail(this.username) == false) {
      this.showAlert('Error', 'Enter username(email) to continue');
      return;
    }

      this.verifyData= {
          'action':730,
              email: this.username,
              sessionid: 'START-SESSION',
              reset_uid: this.reset_uid,
              m_uid: this.m_uid,
              reset_pin: this.reset_pin,
              step:200,
              channel:'MOBILE'
      };


      this.wrenchService.verifyPassReset(this.verifyData).subscribe(
          resetResult => {
              this.resetResult = resetResult;
              console.log("verifyPassReset ==>  ==>INTERNAL RETURN->" + this.resetResult);
              /*
              'm_uid' => string '43c766d9-403d-4125-9983-63c21960b187' (length=36)
      'password_reset_id' => string '109' (length=3)
      'reset_uid' => string '48dc78ef-c5c4-4e3a-937c-05f79eeb3f0a' (length=36)
      'result' => string 'YES I GET TO BACK END' (length=21)
      'server_name' => string 'https://dev-users.wrenchboard.com' (length=33)
      'internal_return' => int 0
               */
              if (resetResult != null
                  && resetResult.internal_return == 0 &&
                  resetResult.lostpass_id > 0 &&
                  resetResult.reset_uid !='' &&
                  resetResult.m_uid != '' ) {

                  var newPassParam= {
                      resetResult: resetResult,
                      reset_pin: this.reset_pin
                  };
              //     this.reset_uid = resetResult.reset_uid;
              //     this.m_uid = resetResult.m_uid;
              //     this.password_reset_id = resetResult.password_reset_id;
                  this.router.navigate(['new-password'],{state:newPassParam});
              }
              else
              {
                  this.showAlert("Reset Error","Some parts of this process went wrong, and it is better to start allover or contact support.");
              }

          }
      );

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

  validateEmail(email) {
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
  }

    async showAlert(mtitle: string, amessage: string) {

        const alert = await this.alertController.create({
            header: 'WrenchBoard',
            subHeader: mtitle,
            message: amessage,
            buttons: ['OK'],
        });

        await alert.present();
        //
        //    let alert = this.alertCtrl.create({
        //      title: mtitle,
        //      subTitle: amessage,
        //      buttons: ['OK']
        //    });
        //    alert.present();
        // //  alert(amessage);
    }
}
