import { Component } from '@angular/core';
import { Storage } from '@ionic/storage-angular';
import { StatusBar } from '@awesome-cordova-plugins/status-bar/ngx';
import { Uid } from '@ionic-native/uid/ngx';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';


@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent {
    constructor(private storage: Storage, private statusBar:StatusBar, private uid: Uid, private androidPermissions: AndroidPermissions) {

        this.statusBar.overlaysWebView(true);


    }
    openQRCode() {
        console.log("open QR Code");
    }
    async ngOnInit() {
        // If using a custom driver:
        // await this.storage.defineDriver(MyCustomDriver)
        await this.storage.create();
        //await this.getImei();
    }
    async getImei() {
        const { hasPermission } = await this.androidPermissions.checkPermission(
            this.androidPermissions.PERMISSION.READ_PHONE_STATE
        );
        console.log(hasPermission);

        if (!hasPermission) {
            const result = await this.androidPermissions.requestPermission(
                this.androidPermissions.PERMISSION.READ_PHONE_STATE
            );

            if (!result.hasPermission) {
                throw new Error('Permissions required');
            }

            // ok, a user gave us permission, we can get him identifiers after restart app
            return;
        }
        console.log("UUID");
        console.log(this.uid.UUID);
        await this.storage.set('uid', this.uid.UUID);
        return this.uid.IMEI
    }
}
