import { Component, OnInit, NgZone, OnDestroy } from "@angular/core";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { AuthenticationService } from "src/app/hmg-common/services/authentication/authentication.service";
import { Router } from "@angular/router";
import { AlertController } from "@ionic/angular";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { CheckUserAuthenticationResponse } from "src/app/hmg-common/services/authentication/models/check-user-auth.response";
import { SmsReaderService } from "src/app/hmg-common/services/sms/sms-reader.service";
import { FingerprintAIO } from "@ionic-native/fingerprint-aio/ngx";
import { Device } from "@ionic-native/device/ngx";
import { SplashScreen } from "@ionic-native/splash-screen/ngx";
import { SharedDataService } from "src/app/hmg-common/services/shared-data-service/shared-data.service";
import { LoginModel } from "../models/LoginModel";
import { LoginRequest } from "src/app/hmg-common/services/authentication/models/login.request";

@Component({
    selector: "login",
    templateUrl: "./login.component.html",
    styleUrls: ["./login.component.scss"]
})
export class LoginComponent implements OnInit, OnDestroy {
    appLang: number = 1;
    isExpired: boolean = false;
    isSupportAr: boolean = false;
    isAppleStore: boolean = false;
    memberLogin: any = {};
    private password: string;
    private language: string;
    private username: string;
    private currentLang: any = 1;
    private patientOutSA: boolean;
    private loginTokenID: string;
    private isMobileFingerPrint: boolean;
    private FingerPrintPatientIdentificationID: string;
    private loginData = new LoginModel();
    constructor(
        public cs: CommonService,
        public authService: AuthenticationService,
        public router: Router,
        public alertController: AlertController,
        public ts: TranslatorService,
        public smsService: SmsReaderService,
        private faio: FingerprintAIO,
        public ngZone: NgZone,
        public device: Device,
        public splash: SplashScreen,
        public sharedData: SharedDataService
    ) { }

    ngOnInit() { }

    ngOnDestroy(): void {
        this.backClicked();
    }
    public changeLanguage() {
        this.ts.switchLanguage();
        if (TranslatorService.CURRENT_LANGUAGE == TranslatorService.EN) {
            this.language = "US";
        } else {
            this.language = "AR";
        }
        this.currentLang = TranslatorService.getCurrentLanguageCode();
    }
    private forgetPasswordPage() {
        this.cs.openUserForgot();
    }
    private hideSplashScreen(stopLoading = false) {
        // this.splash.hide();
        if (stopLoading) {
            this.cs.stopLoading();
        }
    }



    public onLogin() {
        this.checkUserAuthentication();
    }

    private checkUserAuthentication() {
        this.cs.startLoading();
        const request = new LoginRequest();
        request.P_USER_NAME = this.username;
        request.P_LANGUAGE = this.language;
        request.P_PASSWORD = this.password;
        console.log(request);
        this.authService
            .login(request, () => { }, this.ts.trPK("general", "ok"))
            .subscribe((result: CheckUserAuthenticationResponse) => {
                if (this.cs.validResponse(result)) {
                    this.loginData.LogInTokenID = result.LogInTokenID;
                    this.loginData.MobileNumber = result.MemberLoginList.P_MOBILE_NUMBER;
                    this.sharedData.setSharedData(this.loginData, "logindata");
                    this.cs.stopLoading();
                    this.cs.openSMSPage();
                }
            });
    }

    public backClicked() {
        this.smsService.stopSMSMonitoring();
    }

    public openForgotID() {
        this.cs.openUserForgot();
    }

}
