import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
import 'package:local_auth_android/local_auth_android.dart';
import 'package:local_auth_darwin/types/auth_messages_ios.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/main.dart';
import 'package:easy_localization/easy_localization.dart';

abstract class CommonAuthServices {
  Future<bool> authenticate();

  Future<List<BiometricType>> getAvailBio();
}

class CommonAuthImp implements CommonAuthServices {
  final LocalAuthentication localAuth = LocalAuthentication();

  Future<bool> authenticate() async {
    try {
      final availableBiometrics = await localAuth.getAvailableBiometrics();
      if (availableBiometrics == null || availableBiometrics.isEmpty) {
        return false;
      }

      final didAuthenticate = await localAuth.authenticate(
        localizedReason: "Scan your fingerprint or face ID to authenticate",
        options: const AuthenticationOptions(
          stickyAuth: false,
          biometricOnly: true,
          sensitiveTransaction: true,
          useErrorDialogs: true,
        ),
        authMessages: [
          AndroidAuthMessages(
            cancelButton: LocaleKeys.cancel.tr(),
            biometricHint: LocaleKeys.useFingerPrintFaceID.tr(),
            goToSettingsButton: LocaleKeys.settings.tr(),
            goToSettingsDescription: LocaleKeys.setupTouchID.tr(),
          ),
          IOSAuthMessages(
            cancelButton: LocaleKeys.cancel.tr(),
            goToSettingsButton: LocaleKeys.settings.tr(),
            goToSettingsDescription: LocaleKeys.setupTouchID.tr(),
            lockOut: LocaleKeys.reenableTouchID.tr(),
          ),
        ],
      );

      return didAuthenticate;
    } on PlatformException catch (e) {
      print('Error during authentication: $e');
      return false;
    }
  }

  Future<List<BiometricType>> getAvailBio() async {
    return await localAuth.getAvailableBiometrics();
  }

  Future<void> getHuaweiAuth() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    // if (androidInfo.brand == "HUAWEI") {
    //   huawei.canAuth();
    // }
  }
}
