import { Component, OnInit, NgZone } from '@angular/core';
import { NFC } from "@ionic-native/nfc/ngx" 
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import {AttendScanService} from '../../hmg-common/services/attend-services/attend-scan.service';
import { attendanceSwipeScannerRequest } from 'src/app/home/models/attendanceSwipe.Request';
import { Device } from '@ionic-native/device/ngx';
import { AuthenticatedUser } from '../../hmg-common/services/authentication/models/authenticated-user';
import { AttendanceService } from 'src/app/home/services/attendance.services';
import { Response } from 'src/app/hmg-common/services/models/response';
import { ModalController } from '@ionic/angular';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';

@Component({
  selector: 'app-nfc-modal',
  templateUrl: './nfc-modal.component.html',
  styleUrls: ['./nfc-modal.component.scss'],
})
export class NfcModalComponent implements OnInit {
  public userData: any = {};
  public gifStatus = false;
  public nfcReader: any;
  public enableLocationNFC: boolean;
  public lat: number;
  public longt: number;

  constructor(
    private nfc: NFC,
    public common: CommonService,
    public attendScanService: AttendScanService,
    private device: Device,
    private attendance_service: AttendanceService,
    public modalController: ModalController,
    public ts: TranslatorService,
    public ngZone: NgZone
  ) { }

  ngOnInit() {
    this.userData = this.common.sharedService.getSharedData(AuthenticatedUser.SHARED_DATA, false);
    this.startAndroidNfc();
    console.log(this.enableLocationNFC);
  }

  ionViewDidLeave() {
    this.nfcReader.unsubscribe();
  }

  async startAndroidNfc() {
    let flags = this.nfc.FLAG_READER_NFC_A | this.nfc.FLAG_READER_NFC_V;
    this.nfcReader = this.nfc.readerMode(flags).subscribe((tag) => {
      const serialNumber = this.nfc.bytesToHexString(tag.id);
      this.swipeAttendanceNFC(serialNumber);
    }, (error) => {      
      this.modalController.dismiss();
      this.common.presentAlert(this.ts.trPK('general', 'nfc-error'));
      console.log('error =====>>>>>> + + + =' + error);
    });
  }​

  public swipeAttendanceNFC (nfcSerialCode: any) {
    this.nfcReader.unsubscribe();
    const enableFirestore = this.common.sharedService.getSharedData('enableFirestore', false);
    const request: attendanceSwipeScannerRequest = new attendanceSwipeScannerRequest();
    request.PointType = 2;
    request.Latitude = this.lat;
    request.Longitude = this.longt;
    request.QRValue = "";
    request.NFCValue = nfcSerialCode;
    request.UID = this.device.uuid;
    request.UserName = this.userData.EMPLOYEE_NUMBER;
    request.IsGpsRequired = this.enableLocationNFC;
    this.attendance_service.attendanceSwipeScanner(request, () => {
        console.log('Error inside in swipe attendance');
        this.modalController.dismiss();
      }).subscribe((result: Response) => {
        if (enableFirestore) {
          this.attendScanService.processFirebaseDocument('NFC-ANDROID', result);
        }
        if (this.common.validResponse(result)) {
          this.ngZone.run(() => {
            this.gifStatus = true;
          });
          setTimeout(() => {
            this.modalController.dismiss();
            // this.common.openHome();
            this.common.openAttenTrackingpage();
          }, 4000);
        }
      });
  }

  public closeModal() {
    this.modalController.dismiss();
  }

}
