import { Component, OnInit } from '@angular/core';
import { AnimationController, ModalController } from '@ionic/angular';
import { BadRequestPage } from '../popover/bad-request/bad-request.page';
import { SuccessMessagePage } from '../popover/success-message/success-message.page';
import crypto  from  'crypto-js'
import { ToastService } from 'src/app/services/toast.service';
import { LocalstoreService } from 'src/app/store/localstore.service';
@Component({
  selector: 'app-pin',
  templateUrl: './pin.page.html',
  styleUrls: ['./pin.page.scss'],
})
export class PinPage implements OnInit {

  code = []
  constructor( private modalController: ModalController,
    private animationController: AnimationController,
    private toastService: ToastService,
    private localstoreService: LocalstoreService) { }

  ngOnInit() {
  }

  setCode(code: string) {
    if(this.code.length < 4) {
      this.code.push(code)
    }

    if(this.code.length == 4) {
      this.save()
    }
    
  }

  clearCode() {
    this.code =[]
  }

  close() {
    /* console.log(this.isEventEdited); */
    this.modalController.dismiss();
  }

  async save() {
    if(this.code.length == 4) {

      this.close()
      //this.toastService.successMessage()

      const code = this.code.join('')
      const encrypted = crypto.SHA1(code)

      localStorage.setItem('PIN', encrypted)
      this.localstoreService.set('PIN', encrypted)

    } else {
      this.toastService.badRequest()
    }

  }

 



}