import { Component, OnInit } from '@angular/core';
import { ModalController, NavController, LoadingController } 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 { TranslateService } from '@ngx-translate/core';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';

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

  loading = null;
  terms = false;

  constructor(
    private modalController: ModalController,
    private authService: AuthService,
    private translateService: TranslateService,
    private navCtrl: NavController,
    private loadingCtrl: LoadingController,
    private iab: InAppBrowser,
    private alertService: AlertService
  ) { }
  ngOnInit() {
  }
  // Dismiss Register Modal
  dismissRegister() {
    this.navCtrl.navigateRoot('/landing');
  }
  // On Login button tap, dismiss Register modal and open login Modal
  loginModal() {
    this.navCtrl.navigateForward('/login');
  }

  updateChecked () {
    if (this.terms == true) {
      this.terms = false;
      console.log(this.terms);
    } else {
        this.terms = true;
        console.log(this.terms);
    }
  }

  register(form: NgForm) {

    if (this.terms == true) {
      if (form.value.name && form.value.email && form.value.phone && form.value.password && form.value.password1) {
        if (form.value.password == form.value.password1) {
          this.loadingCtrl.create().then((overlay) => {
            this.loading = overlay;
            this.loading.present();
          });
          this.authService.register(form.value.name, form.value.email, form.value.phone, form.value.password).subscribe(
            data => {
                  this.loading.dismiss();
                  this.alertService.presentToast(this.translateService.instant('alerts.signup'));
            },
            error => {
              if (JSON.stringify(error['status']) == '422') {
                this.loading.dismiss();
                this.alertService.presentToast(this.translateService.instant('alerts.signup_error'));
              }
              else {
                this.loading.dismiss();
                console.log(error);
                this.alertService.presentToast(this.translateService.instant('alerts.error') + error['message']);
              }
            },
            () => {

            }
          );
        } else {
          this.alertService.presentToast(this.translateService.instant('alerts.signup_error_1'));
        }
      } else {
        this.alertService.presentToast(this.translateService.instant('alerts.signup_error_2'));
      }
    } else {
      this.alertService.presentToast(this.translateService.instant('alerts.signup_error_3'));
    }

  }

  openTerms() {
    this.iab.create('https://jobheroapp.com/terminos-y-condiciones', '_system')
  }
}
