import 'dart:math' as math;
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/service/logging.dart' as logging;
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

class FirebaseApi with logging.Logging {
  bool appleSignInAvailable = false;

  static final FirebaseAuth auth = FirebaseAuth.instance;

  static const String SIGN_IN_OK = "OK";
  static const String SIGN_IN_NOT_FOUND = "user-not-found";
  static const String SIGN_IN_WRONG_PWD = "wrong-password";
  static const String REGISTER_WEAK_PWD = "weak-password";
  static const String REGISTER_EMAIL_IN_USE = "email-already-in-use";

  late UserCredential userCredential;
  String? firebaseRegToken;

  factory FirebaseApi() => FirebaseApi._internal();

  FirebaseApi._internal();

  // Define an async function to initialize FlutterFire
  Future<void> initializeFlutterFire() async {
    try {
      // Wait for Firebase to initialize and set `_initialized` state to true
      await Firebase.initializeApp();

      this.appleSignInAvailable = await SignInWithApple.isAvailable();

      /* AwesomeNotifications().initialize(
          // set the icon to null if you want to use the default app icon
          null,
          [
            NotificationChannel(
                channelKey: 'basic_channel',
                channelName: 'Basic notifications',
                channelDescription: 'Notification channel for basic tests',
                defaultColor: Color(0xFF9D50DD),
                ledColor: Colors.white)
          ]);

      AwesomeNotifications().isNotificationAllowed().then((isAllowed) {
        if (!isAllowed) {
          // Insert here your friendly dialog box before call the request method
          // This is very important to not harm the user experience
          AwesomeNotifications().requestPermissionToSendNotifications();
        }
      }); */

      await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
        alert: true, // Required to display a heads up notification
        badge: true,
        sound: true,
      );
      this.firebaseRegToken = await FirebaseMessaging.instance.getToken();      
      Cache().firebaseMessageToken = firebaseRegToken;
      log("FirebaseMessaging token $firebaseRegToken");
      
      FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        print('Got a message whilst in the foreground!');
        print('Message data: ${message.data}');

        if (message.notification != null) {
          print('Message also contained a notification: ${message.notification}');
        }
      });
      
    } catch (e) {
      // Set `_error` state to true if Firebase initialization fails
      log("Error initializing Firebase");
    }
  }

  Future<String> signInEmail(String? email, String? password) async {
    if (email == null) {
      throw Exception("Please type an email address");
    }
    if (password == null) {
      throw Exception("Password too short");
    }
    String rc = SIGN_IN_OK;
    try {
      userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password);
      Cache().firebaseUid = userCredential.user!.uid;
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-not-found') {
        log('No user found for that email.');
        rc = SIGN_IN_NOT_FOUND;
      } else if (e.code == 'wrong-password') {
        log('Wrong password provided for that user.');
        rc = SIGN_IN_WRONG_PWD;
        throw Exception("Customer does not exist or the password is wrong");
      }
      return e.code;
    }
    return rc;
  }

  Future<String> registerEmail(String email, String password) async {
    String rc = SIGN_IN_OK;
    try {
      userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password);
      Cache().firebaseUid = userCredential.user!.uid;
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        log('The password provided is too weak.');
        rc = REGISTER_WEAK_PWD;
        throw Exception("Password too short");
      } else if (e.code == 'email-already-in-use') {
        log('The account already exists for that email.');
        rc = REGISTER_EMAIL_IN_USE;
        throw Exception("The email address has been registered already");
      }
    } catch (e) {
      log(e.toString());
      throw Exception(e.toString());
    }
    return rc;
  }

  String generateNonce([int length = 32]) {
    final charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._';
    final random = math.Random.secure();
    return List.generate(length, (_) => charset[random.nextInt(charset.length)]).join();
  }

  /// Returns the sha256 hash of [input] in hex notation.
  String sha256ofString(String input) {
    final bytes = utf8.encode(input);
    final digest = sha256.convert(bytes);
    return digest.toString();
  }

  Future<Map<String, dynamic>> signInWithApple() async {
    Map<String, dynamic> userData = Map();

    /* final apple.AuthorizationResult result = await SignInWithApple.performRequests([
      apple.AppleIdRequest(requestedScopes: [apple.Scope.email, apple.Scope.fullName])
    ]);
    switch (result.status) {
      case apple.AuthorizationStatus.authorized:
        print('User authorized');
        break;
      case apple.AuthorizationStatus.error:
        print('User error');
        throw Exception("Apple Sign-In failed");
      case apple.AuthorizationStatus.cancelled:
        print('User cancelled');
        throw Exception("Apple Sign-In cancelled");
    }

    // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider("apple.com").credential(
        idToken: String.fromCharCodes(result.credential.identityToken),
        accessToken: String.fromCharCodes(result.credential.authorizationCode!));
 */
    // To prevent replay attacks with the credential returned from Apple, we
    // include a nonce in the credential request. When signing in with
    // Firebase, the nonce in the id token returned by Apple, is expected to
    // match the sha256 hash of `rawNonce`.
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);

    // Request credential for the currently signed in Apple account.
    final appleCredential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      nonce: nonce,
    );

    // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider("apple.com").credential(
      idToken: appleCredential.identityToken,
      rawNonce: rawNonce,
    );
    // Sign in the user with Firebase. If the nonce we generated earlier does
    // not match the nonce in `appleCredential.identityToken`, sign in will fail.
    UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential);
    Cache().firebaseUid = userCredential.user!.uid;
    log("userCredential: " + userCredential.toString());

    log("Apple Credentials: ${appleCredential.userIdentifier} state ${appleCredential.state} email ${userCredential.user!.email!}");
    userData['email'] = userCredential.user!.email;

    return userData;
  }

  Future<Map<String, dynamic>> registerWithApple() async {
    Map<String, dynamic> userData = Map();
    /*    final apple.AuthorizationResult result = await apple.TheAppleSignIn.performRequests([
      apple.AppleIdRequest(requestedScopes: [apple.Scope.email, apple.Scope.fullName])
    ]);
    switch (result.status) {
      case apple.AuthorizationStatus.authorized:
        print('Apple User authorized');
        break;
      case apple.AuthorizationStatus.error:
        print('Apple User error');
        throw Exception("Apple Sign-In failed");
      case apple.AuthorizationStatus.cancelled:
        print('User cancelled');
        throw Exception("Apple Sign-In cancelled");
    }
 */
    final rawNonce = generateNonce();
    final nonce = sha256ofString(rawNonce);

    // Request credential for the currently signed in Apple account.
    final appleCredential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      nonce: nonce,
    );

    final oauthCredential = OAuthProvider("apple.com").credential(
      idToken: appleCredential.identityToken,
      rawNonce: rawNonce,
    );

    /*  // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider("apple.com").credential(
        idToken: String.fromCharCodes(result.credential.identityToken),
        accessToken: String.fromCharCodes(result.credential.authorizationCode));
 */
    // Sign in the user with Firebase. If the nonce we generated earlier does
    // not match the nonce in `appleCredential.identityToken`, sign in will fail.
    UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential);

    Cache().firebaseUid = userCredential.user!.uid;

    userData['email'] = userCredential.user!.email;

    return userData;
  }

  Future<Map<String, dynamic>> signInWithGoogle() async {
    Map<String, dynamic> userData = Map();

    // Trigger the authentication flow
    GoogleSignIn _googleSignIn = GoogleSignIn(
      scopes: [
        'email',
        'https://www.googleapis.com/auth/contacts.readonly',
      ],
    );
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();

    if (googleUser == null) {
      throw Exception("Google Sign In failed");
    }

    // Obtain the auth details from the request
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    // Create a new credential
    final OAuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential);
    Cache().firebaseUid = userCredential.user!.uid;

    log("GoogleUser: " + googleUser.toString());
    userData['email'] = googleUser.email;
    userData['id'] = googleUser.id;
    userData['name'] = googleUser.displayName;

    return userData;
  }

  Future<Map<String, dynamic>> registerWithGoogle() async {
    Map<String, dynamic> userData = Map();

    // Trigger the authentication flow
    GoogleSignIn _googleSignIn = GoogleSignIn(
      scopes: [
        'email',
        'https://www.googleapis.com/auth/contacts.readonly',
      ],
    );
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();

    if (googleUser == null) {
      throw Exception("Google Sign In failed");
    }

    // Obtain the auth details from the request
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    // Create a new credential
    final OAuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    final userCredential = await FirebaseAuth.instance.signInWithCredential(credential);

    log("Google credentials: " + credential.toString() + " GoogleUser: " + googleUser.toString());
    Cache().firebaseUid = userCredential.user!.uid;

    userData['email'] = googleUser.email;

    return userData;
  }

  Future<Map<String, dynamic>> signInWithFacebook() async {
    Map<String, dynamic> userData;

    // by default the login method has the next permissions ['email','public_profile']
    final LoginResult result = await FacebookAuth.instance.login();
    if (result.status == LoginStatus.success) {
      final AccessToken accessToken = result.accessToken!;
      log(accessToken.toJson().toString());
      Cache().accessTokenFacebook = accessToken;
      // get the user data
      userData = await FacebookAuth.instance.getUserData();
      Cache().firebaseUid = userData['id'];
      log(userData.toString());
    } else {
      throw Exception("Facebook login was not successful");
    }

    return userData;
  }

  Future<Map<String, dynamic>> registerWithFacebook() async {
    Map<String, dynamic> userData;

    // by default the login method has the next permissions ['email','public_profile']
    final LoginResult result = await FacebookAuth.instance.login();
    if (result.status == LoginStatus.success) {
      final AccessToken accessToken = result.accessToken!;
      Cache().accessTokenFacebook = accessToken;
      // get the user data
      userData = await FacebookAuth.instance.getUserData();
      log("FB user data: " + userData.toString());

      // Create a credential from the access token
      final OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(accessToken.token);

      // Once signed in, return the UserCredential
      final userCredential = await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
      log("Email by FB: " + userData['email'] + " FB credential: " + userCredential.toString());

      Cache().firebaseUid = userCredential.user!.uid;
    } else {
      throw Exception("Facebook login was not successful");
    }

    return userData;
  }

  Future<void> logOutFacebook() async {
    await FacebookAuth.instance.logOut();
    Cache().accessTokenFacebook = null;
  }

  Future<void> signOut() async {
    await FirebaseAuth.instance.signOut();
  }

  Future<void> resetPassword(String email) async {
    await FirebaseAuth.instance.sendPasswordResetEmail(email: email);
  }

  Future<void> setupRemoteConfig() async {
    //initializeFlutterFire();
    RemoteConfig? remoteConfig;
    try {
      remoteConfig = RemoteConfig.instance;
      await remoteConfig.setConfigSettings(RemoteConfigSettings(
        fetchTimeout: const Duration(seconds: 10),
        minimumFetchInterval: const Duration(seconds: 1),
      ));

      RemoteConfigValue(null, ValueSource.valueStatic);
      Cache().setRemoteConfig(remoteConfig);
    } on Exception catch (e) {
      print('Unable to fetch remote config. Cached or default values will be used: $e');
      if (remoteConfig != null) {
        await remoteConfig.setDefaults(<String, dynamic>{
          'sales_page_text_a': '',
          'product_set_2': '',
          'registration_skip_color': '',
          'email_checkbox': '',
        });
        Cache().setRemoteConfig(remoteConfig);
      }
    }
  }
}

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // If you're going to use other Firebase services in the background, such as Firestore,
  // make sure you call `initializeApp` before using other Firebase services.
  print('Handling a background message ${message.messageId}');
}

/* Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print('Handling a background message: ${message.messageId}');

  if (!StringUtils.isNullOrEmpty(message.notification?.title, considerWhiteSpaceAsEmpty: true) ||
      !StringUtils.isNullOrEmpty(message.notification?.body, considerWhiteSpaceAsEmpty: true)) {
    print('message also contained a notification: ${message.notification}');

    String? imageUrl;
    imageUrl ??= message.notification!.android?.imageUrl;
    imageUrl ??= message.notification!.apple?.imageUrl;

    Map<String, dynamic> notificationAdapter = {
      NOTIFICATION_CHANNEL_KEY: 'basic_channel',
      NOTIFICATION_ID: message.data[NOTIFICATION_CONTENT]?[NOTIFICATION_ID] ?? message.messageId ?? math.Random().nextInt(2147483647),
      NOTIFICATION_TITLE: message.data[NOTIFICATION_CONTENT]?[NOTIFICATION_TITLE] ?? message.notification?.title,
      NOTIFICATION_BODY: message.data[NOTIFICATION_CONTENT]?[NOTIFICATION_BODY] ?? message.notification?.body,
      NOTIFICATION_LAYOUT: StringUtils.isNullOrEmpty(imageUrl) ? 'Default' : 'BigPicture',
      NOTIFICATION_BIG_PICTURE: imageUrl
    };

    AwesomeNotifications().createNotificationFromJsonData(notificationAdapter);
  } else {
    AwesomeNotifications().createNotificationFromJsonData(message.data);
  }
} */