import { Component, OnInit } from '@angular/core';
import { ServicesService } from '../../services.service';
import { LoginflowService } from '../../data/loginflow.service';
import { AuthserviceService } from 'src/app/authservice.service';
import { Router } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';

@Component({
  selector: 'app-twofasetting',
  templateUrl: './twofasetting.component.html',
  styleUrls: ['./twofasetting.component.scss'],
})
export class TwofasettingComponent implements OnInit {
  data: any;
  code: any;
  dataqr: any;
  checkTrue = false;
  elementType: 'url';
  value: any = '';
  newdata: any;
  email: any;
  password: any;
  constructor(
    public LoginflowService: LoginflowService,
    public service: ServicesService,
    public auth: AuthserviceService,
    public router: Router,
    public spinner: NgxSpinnerService,

  ) {
    var setlogin = this.auth.isLoggedIn();
    if (!setlogin) {
      this.router.navigate(['/login']);
    }
  }

  async ngOnInit() {
    try {
      this.spinner.show();
      var data = window.localStorage.getItem('userdetails');
      if (data) {
        var jsonData = JSON.parse(<string>data);
        var password = window.localStorage.getItem('domaindata');
        var set = atob(password)
        var passwordset = JSON.parse(<string>set);
        if (jsonData.email && passwordset) {
          this.email = jsonData.email;
          this.password = passwordset

          await this.auth.Getqrcode(jsonData.email, passwordset).then(dataset => {
            if (dataset && dataset[0].StatusCode == 1) {
              this.newdata = dataset;
              this.value = dataset[0].code;
              this.dataqr = dataset[0].secretCode;
              this.spinner.hide();
            }
            else {
              this.spinner.hide();
              this.router.navigate(['/login']);
            }

          })
        }
        else {
          this.spinner.hide();

          this.router.navigate(['/login']);

        }

      }
      else {
        this.spinner.hide();

        this.router.navigate(['/login']);

      }
    } catch {
      this.spinner.hide();

    }



  }
  thankyou() {
    if (this.code) {
      var data = window.localStorage.getItem('userdetails');
      var jsonData = JSON.parse(<string>data);

      this.LoginflowService.verify(this.code, jsonData.email).then((data) => {
        if (data[0].status == 200) {
          var list = JSON.parse(<string>data[0].json);
          if (list.data.success == 'true') {
            this.service.ShowSuccess('Authentication successfully');
          } else {
            this.service.ShowError('Invalid code');
          }
        }
      });
    } else {
      this.service.ShowError('Please enter code');
    }
  }
  reset() {
    this.code = ""
  }

  checkauth() {
    this.checkTrue = !this.checkTrue;
  }
  async verify() {
    if (this.code.trim() && this.code.length == 6) {
      await this.spinner.show()
      await this.auth.verify(this.email, this.password, this.code, this.newdata[0].data).then(async (set) => {
        if (set[0].StatusCode == 1) {
          this.code = "";
          this.checkTrue = true;
          await this.spinner.hide()

          this.service.ShowSuccess("Your Authentication is successfully.")
        }
        else {
          await this.spinner.hide()

          this.service.ShowError("You have entered Invalid code. Please try again with valid code.")
        }
      })

    }
    else {
      await this.spinner.hide()

      this.service.ShowError("Please enter the 6-digit one-time password that has been generated by your app")
    }
    await this.spinner.hide()

  }
}
