import { Component, OnInit } from '@angular/core';
import { ModalController, 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';
import { OneSignal, OSNotificationPayload } from '@ionic-native/onesignal/ngx';

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

  phone_string: string = "phone";
  private loading;

  constructor(
    private modalController: ModalController,
    private authService: AuthService,
    private navCtrl: NavController,
    private alertService: AlertService,
    private oneSignal: OneSignal,
    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 => {
        console.log(data);
        this.authService.isVerified = true
        this.navCtrl.navigateRoot('/dashboard')
      },
      error => {
        console.log(error);
      }
    );
  }

  logout() {
    this.loadingCtrl.create().then((overlay) => {
      this.loading = overlay;
      this.loading.present();
    });
    this.authService.logout().subscribe(
      data => {
        this.alertService.presentToast("Sesión finalizada");
        this.oneSignal.sendTag("iChamba_ID", null);
      },
      error => {
        this.loading.dismiss();
        console.log(error);
      },
      () => {
        this.loading.dismiss();
        this.navCtrl.navigateRoot('/landing');
      }
    );
  }
}
