import { Component, OnInit } from '@angular/core';
import { LoadingController, NavController } from '@ionic/angular';
import { AuthService } from 'src/app/services/auth.service';
import { NgForm } from '@angular/forms';
import { AlertService } from 'src/app/services/alert.service';
// TODO: Install and configure OneSignal Capacitor plugin for push notifications

@Component({
  selector: 'app-verify',
  templateUrl: 'verify.page.html',
  styleUrls: ['verify.page.scss'],
  standalone: false
})
export class VerifyPage implements OnInit {

  phone_string: string = "phone";
  private loading: any;

  constructor(
    private authService: AuthService,
    private navCtrl: NavController,
    private alertService: AlertService,
    private loadingCtrl: LoadingController,
  ) { }
  ngOnInit() {
  }
  // VerifyMe abre la API de AccountKit, despues de verificar el numero, envia el numero
  // de telefono al backend con la funcion this.authService.verifyUser, que se encuentra en
  // auth.service.ts
  verifyMe(form: NgForm) {
    this.authService.verifyUser(this.phone_string).subscribe(
      (data: any) => {
        console.log(data);
        this.authService.isVerified = true
        this.navCtrl.navigateRoot('/dashboard')
      },
      (error: any) => {
        console.log(error);
      }
    );
  }

  async logout() {
    this.loading = await this.loadingCtrl.create();
    await this.loading.present();

    this.authService.logout().subscribe(
      (data: any) => {
        this.alertService.presentToast("Sesión finalizada");
        // TODO: Clear OneSignal tag when Capacitor plugin is configured
      },
      (error: any) => {
        if (this.loading) this.loading.dismiss();
        console.log(error);
      },
      () => {
        if (this.loading) this.loading.dismiss();
        this.navCtrl.navigateRoot('/landing');
      }
    );
  }
}
