import { MermsServiceProviderService } from './../providers/merms-service-provider.service';
import { Component, ViewChild, OnInit } from '@angular/core';
import { MermsSessionService } from './../merms-session.service';
//import { Device } from '@ionic-native/device/ngx'
import { Router, NavigationExtras } from '@angular/router'


import {
  NavController,
  AlertController,
  LoadingController,
} from '@ionic/angular'

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

  @ViewChild('username', { static: false }) username
  @ViewChild('password', { static: false }) password

  loginData: {
    username: string
    password: string
    sessionid: string
  }

  StoredloginData: {
    username: string
    password: string
  }

  loaded = false
  loginResult: any
  public platform_is_ios: boolean = false

  constructor(
     public navCtrl: NavController,
     private alertCtrl: AlertController,
     private loadingCtrl: LoadingController,
   /*  private mermsServiceProviderService :MermsServiceProviderService,*/
     private mermsSessionService :MermsSessionService ,
     private router: Router
     ) { }

  ngOnInit() {
  }


  async loginAccount() {
    
        console.log('MERMSLoginPage::loginAccount()')
        this.mermsSessionService.account_token = ''
        const loader = await this.loadingCtrl.create({
          message: 'Please wait...',
          duration: 3000,
        })
      

    /*
    220       spinner: null,
    221       duration: 5000,
    222       message: 'Click the backdrop to dismiss early...',
    223       translucent: true,
    224       cssClass: 'custom-class custom-loading',
    225       backdropDismiss: true
    */

    if (
      this.username == null ||
      this.username === '' ||
      this.mermsSessionService.validateEmail(this.username) === false ||
      this.password == null ||
      this.password === ''
    ) {
      this.showAlert(
        'Invalid Login',
        'Enter username(email) and password to login'
      )
    } else {
      loader.present()
      this.loginData = {
        username: this.username,
        password: this.password,
        sessionid: 'DUMMYSESSION',
      }
      this.StoredloginData = {
        username: this.username,
        password: this.password,
      }




    }
    this.completeLogin();
  }

  completeLogin() {
    console.log('MERMSLoginPage::completeLogin()')
    // alert(100);
    this.router.navigateByUrl('/merms-dash')
  }

  async showAlert(mtitle: string, amessage: string) {
    console.log('MERMSLoginPage::showAlert()')
    const alert = await this.alertCtrl.create({
      header: mtitle,
      message: amessage,
      buttons: ['OK'],
    })
    alert.present()
  }

  lostPassword() {
    console.log('MERMSLoginPage::lostPassword()')
    // this.navCtrl.push(LostpasswordPage);
    this.router.navigateByUrl('/MERMS-lostpassword')
  }

  showSignUp() {
    console.log('MERMSLoginPage::showSignUp()')
    // this.navCtrl.push(UserSignupPage);
    // this.navCtrl.push(HomePage,{jump_to_slide:5});
    const navigationExtras: NavigationExtras = {
      state: {
        jump_to_slide: 5,
      },
    }
    this.router.navigate(['/MERMS-start'], navigationExtras)
  }

  goBack() {
    console.log('MERMSLoginPage::goBack()')
    this.navCtrl.pop()
  }

  appleSignup() {
    console.log('MERMSStartPage:appleSignup()')

  
  }
  googleSignup() {
    console.log('MERMSLoginPage::googleSignup()')
    console.log('----- Google Signup -----')
    // this.signup_method = 1;

  }

}
