import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AnimationController, ModalController } from '@ionic/angular';
import { LoginUserRespose } from 'src/app/models/user.model';
import { AuthService } from 'src/app/services/auth.service';
import { FingerprintPage } from 'src/app/shared/fingerprint/fingerprint.page';
import { PinPage } from 'src/app/shared/pin/pin.page';
import { LocalstoreService } from 'src/app/store/localstore.service';

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

  loggeduser: LoginUserRespose;
  userLoginPreference = ''

  constructor(private modalController:ModalController,
    private authService: AuthService,
    private animationController: AnimationController,
    private router: Router,
    private localstoreService: LocalstoreService
    ) { 

    this.loggeduser = authService.ValidatedUser;

    console.log(this.loggeduser.RoleDescription)

    this.checkState()
  }

  ngOnInit() {}

  close() {
    this.modalController.dismiss();
  }

  async addPin() {
    const enterAnimation = (baseEl: any) => {
      const backdropAnimation = this.animationController.create()
        .addElement(baseEl.querySelector('ion-backdrop')!)
        .fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

      const wrapperAnimation = this.animationController.create()
        .addElement(baseEl.querySelector('.modal-wrapper')!)
        .keyframes([
          { offset: 0, opacity: '1', right: '-100%' },
          { offset: 1, opacity: '1', right: '0px' }
        ]);

      return this.animationController.create()
        .addElement(baseEl)
        .easing('ease-out')
        .duration(500)
        .addAnimation([backdropAnimation, wrapperAnimation]);
    }

    const leaveAnimation = (baseEl: any) => {
      return enterAnimation(baseEl).direction('reverse');
    }
 

    const modal = await this.modalController.create({
      enterAnimation,
      leaveAnimation,
      component: PinPage,
      cssClass: 'model profile-modal',
      componentProps: {
      }
    });
    
    modal.present();
  }


  async addFingerprint() {
    const enterAnimation = (baseEl: any) => {
      const backdropAnimation = this.animationController.create()
        .addElement(baseEl.querySelector('ion-backdrop')!)
        .fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

      const wrapperAnimation = this.animationController.create()
        .addElement(baseEl.querySelector('.modal-wrapper')!)
        .keyframes([
          { offset: 0, opacity: '1', right: '-100%' },
          { offset: 1, opacity: '1', right: '0px' }
        ]);

      return this.animationController.create()
        .addElement(baseEl)
        .easing('ease-out')
        .duration(500)
        .addAnimation([backdropAnimation, wrapperAnimation]);
    }

    const leaveAnimation = (baseEl: any) => {
      return enterAnimation(baseEl).direction('reverse');
    }
 

    const modal = await this.modalController.create({
      enterAnimation,
      leaveAnimation,
      component: FingerprintPage,
      cssClass: 'model profile-modal',
      componentProps: {
      }
    });
    
    modal.present();
  }

  logout() {
    // clear local storage
    window.localStorage.clear();

    setTimeout(()=> {
      this.router.navigateByUrl('/', { replaceUrl: true });
    },100)

  }

  LoginPreferenceMethod(type: string) {
    
    let userData = this.localstoreService.get('UserData', {})

    if (userData.hasOwnProperty('loginPreference')) { 
      if (userData.loginPreference != type) {
        if (type) {
          userData.loginPreference = type
        }
      } else {
        userData.loginPreference = 'none'
      }
    
    } else {
      userData.loginPreference = 'none'
    }


    this.localstoreService.set('UserData', userData)
  }

  checkState() {

    let userData = this.localstoreService.get('UserData', {})
    
    if (userData.hasOwnProperty('loginPreference')) {
      this.userLoginPreference = userData.loginPreference
    } else {
      this.userLoginPreference =  ''
    }
    
  }

}
