import { Component, OnInit } from '@angular/core';
import { NavController, LoadingController } from '@ionic/angular';
import { NgForm } from '@angular/forms';
import { EnvService } from 'src/app/services/env.service';
import { AuthService } from 'src/app/services/auth.service';
import { IchambaService } from 'src/app/services/ichamba.service';
import { AlertService } from 'src/app/services/alert.service';

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

  private loading: HTMLIonLoadingElement | null = null;
  pin: number | null = null;

  constructor(
    private authService: AuthService,
    private alertService: AlertService,
    private ichambaService: IchambaService,
    private navCtrl: NavController,
    private loadingCtrl: LoadingController,
    private env: EnvService,
  ) { }

  ngOnInit() {
  }

  otpController(event: any, next: any, prev: any){
    if (event.target.value.length > 1){
      event.target.value = event.target.value.slice(-1);
      next.setFocus();
    }
     if(event.target.value.length < 1 && prev) {
       prev.setFocus()
     }
     else if(next && event.target.value.length > 0) {
       next.setFocus();
     } else {
      return 0;
     }
  }

  startservice(form: NgForm) {
    this.pin = Number(form.value.otp1.toString() + form.value.otp2.toString() + form.value.otp3.toString() + form.value.otp4.toString() + form.value.otp5.toString() + form.value.otp6.toString())
    this.loadingCtrl.create().then((overlay) => {
      this.loading = overlay;
      this.loading.present();
    });
    this.ichambaService.startContract(this.pin!).subscribe(
      (data: any) => {
        if (data['message'] == "No service") {
          if (this.loading) this.loading.dismiss();
          this.alertService.presentToast("No hay ningún servicio agendado a esta hora");
        } else {
          if (this.loading) this.loading.dismiss();
          this.alertService.presentToast(data['message']);
          this.navCtrl.navigateRoot('/dashboard');
        }
      }, (error: any) => {
        if (this.loading) this.loading.dismiss();
        this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
      });
    console.log(form.value.otp1 + form.value.otp2 + form.value.otp3 + form.value.otp4 + form.value.otp5 + form.value.otp6)
  }

  nohome() {
    this.loadingCtrl.create().then((overlay) => {
      this.loading = overlay;
      this.loading.present();
    });
    this.ichambaService.noHomeCheck().subscribe(
      (data: any) => {
        if (data['message'] == "wait") {
          if (this.loading) this.loading.dismiss();
          this.alertService.presentToast("Por favor espere a los 10 minutos de tolerancia de la hora acordada");
        } else if (data['message'] == "no-contract") {
          if (this.loading) this.loading.dismiss();
          this.alertService.presentToast("No hay contratos citados a esta hora");
        } else {
          if (this.loading) this.loading.dismiss();
          this.navCtrl.navigateForward(['/nohome/', data['id']]);
        }
      }, (error: any) => {
        if (this.loading) this.loading.dismiss();
        this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
      }
    );
  }
}
