import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hmg_nurses/base/base_vm.dart';
import 'package:hmg_nurses/classes/enums.dart';
import 'package:hmg_nurses/classes/utils.dart';
import 'package:hmg_nurses/dialogs/otp_dialog.dart';
import 'package:hmg_nurses/generated/locale_keys.g.dart';
import 'package:local_auth/local_auth.dart';

class LoginViewModel extends BaseViewModel {
  bool isLogin = false;
  bool unverified = false;
  bool isFromLogin = false;
  APPSTATUS appStatus = APPSTATUS.loading;
  String localToken = "";

  String nurseProfile = "";

  final LocalAuthentication auth = LocalAuthentication();
  late List<BiometricType> _availableBiometrics;

  LoginViewModel();

  /// get type name based on id.
  getType(type) {
    switch (type) {
      case 1:
        return LocaleKeys.sms.tr();
      case 3:
        return LocaleKeys.fingerPrint.tr();
      case 4:
        return LocaleKeys.face.tr();
      case 2:
        return LocaleKeys.whatsapp.tr();
      default:
        return LocaleKeys.sms.tr();
    }
  }

  /// ask user to add his biometric
  showIOSAuthMessages() async {
    try {
      await auth.authenticate(
        localizedReason: 'Scan your fingerprint to authenticate',
      );
    } on PlatformException catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
  }

  /// check specific biometric if it available or not
  Future<bool> checkIfBiometricAvailable(BiometricType biometricType) async {
    bool isAvailable = false;
    await _getAvailableBiometrics();
    if (_availableBiometrics != null) {
      for (var i = 0; i < _availableBiometrics.length; i++) {
        if (biometricType == _availableBiometrics[i]) isAvailable = true;
      }
    }
    return isAvailable;
  }

  /// get all available biometric on the device for local Auth service
  Future<void> _getAvailableBiometrics() async {
    try {
      _availableBiometrics = await auth.getAvailableBiometrics();
    } on PlatformException catch (e) {
      if (kDebugMode) {
        print(e);
      }
    }
  }

  /// determine the status of the app
  APPSTATUS get status {
    if (state == ViewState.busy) {
      appStatus = APPSTATUS.loading;
    } else {
      if (nurseProfile != null) {
        appStatus = APPSTATUS.authenticated;
      } else if (unverified) {
        appStatus = APPSTATUS.unverified;
      } else if (isLogin) {
        appStatus = APPSTATUS.authenticated;
      } else {
        appStatus = APPSTATUS.unAuthenticated;
      }
    }
    return appStatus;
  }

  setAppStatus(APPSTATUS status) {
    appStatus = status;
    notifyListeners();
  }

  setUnverified(bool unverified, {bool isFromLogin = false}) {
    this.unverified = unverified;
    this.isFromLogin = isFromLogin;
    notifyListeners();
  }

  /// logout function
  logout({bool isFromLogin = false}) async {
    // localToken = "";
    // String lang = await sharedPref.getString(APP_Language);
    // await Utils.clearSharedPref();
    // doctorProfile = null;
    // sharedPref.setString(APP_Language, lang);
    // deleteUser();
    // await getDeviceInfoFromFirebase();
    // this.isFromLogin = isFromLogin;
    // appStatus = APPSTATUS.unAuthenticated;
    // setState(ViewState.Idle);
  }

  deleteUser() {
    // user = null;
    // unverified = false;
    // isLogin = false;
  }

  startSMSService(AuthMethodTypes type, {isSilentLogin = false, required BuildContext context}) {
    OtpDialog(
      type: 1,
      mobileNo: 0504278212,
      onSuccess: (String otpCode, TextEditingController pinPut) {
        Utils.showLoading();
        //TODO: API CALL
        // performDirectApiCall(_title, _icon, _flag, value);
      },
      onFailure: () => Navigator.pop(context),
      onResendCode: () {},
    ).displayDialog(context);
  }
}
