import 'package:core/presentation/extensions/either_view_state_extension.dart';
import 'package:core/presentation/utils/theme_utils.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/string_convert.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:tmail_ui_user/features/base/mixin/message_dialog_action_manager.dart';
import 'package:tmail_ui_user/features/home/domain/state/auto_sign_in_via_deep_link_state.dart';
import 'package:tmail_ui_user/features/home/domain/usecases/auto_sign_in_via_deep_link_interactor.dart';
import 'package:tmail_ui_user/main/deep_links/deep_link_callback_action_define.dart';
import 'package:tmail_ui_user/main/deep_links/open_app_deep_link_data.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';

mixin OpenAppDeepLinkHandlerMixin {
  OpenAppDeepLinkData? parseOpenAppDeepLink(Uri uri) {
    try {
      final accessToken = uri.queryParameters['access_token'] ?? '';
      final refreshToken = uri.queryParameters['refresh_token'];
      final idToken = uri.queryParameters['id_token'];
      final expiresInStr = uri.queryParameters['expires_in'];
      final username = uri.queryParameters['username'];
      final registrationUrl = uri.queryParameters['registrationUrl'] ?? '';
      final jmapUrl = uri.queryParameters['jmapUrl'] ?? '';

      final expiresIn = expiresInStr != null
          ? int.tryParse(expiresInStr)
          : null;

      final usernameDecoded = username?.isNotEmpty == true
          ? StringConvert.decodeBase64ToString(username!)
          : username ?? '';

      return OpenAppDeepLinkData(
        registrationUrl: registrationUrl,
        jmapUrl: jmapUrl,
        accessToken: accessToken,
        refreshToken: refreshToken,
        idToken: idToken,
        expiresIn: expiresIn,
        username: usernameDecoded,
      );
    } catch (e) {
      logWarning('DeepLinksManager::parseOpenAppDeepLink:Exception = $e');
      return null;
    }
  }

  void handleOpenAppDeepLinks({
    required OpenAppDeepLinkData openAppDeepLinkData,
    OnDeepLinkFailureCallback? onFailureCallback,
    OnAutoSignInViaDeepLinkSuccessCallback? onAutoSignInSuccessCallback,
    OnDeepLinkConfirmLogoutCallback<OpenAppDeepLinkData>? onConfirmLogoutCallback,
    UserName? username,
    bool isSignedIn = true,
  }) {
    if (!openAppDeepLinkData.isValidAuthentication()) {
      onFailureCallback?.call();
      return;
    }

    if (isSignedIn) {
      if (currentContext == null || username == null) {
        onFailureCallback?.call();
        return;
      }

      if (openAppDeepLinkData.isLoggedInWith(username.value)) {
        onFailureCallback?.call();
        return;
      }

      _showConfirmDialogSwitchAccount(
        context: currentContext!,
        currentUsername: username.value,
        newUsername: openAppDeepLinkData.username,
        onConfirmAction: () => onConfirmLogoutCallback?.call(openAppDeepLinkData),
        onCancelAction: () => onFailureCallback?.call()
      );
    } else {
      autoSignInViaDeepLink(
        openAppDeepLinkData: openAppDeepLinkData,
        onAutoSignInSuccessCallback: (viewState) => onAutoSignInSuccessCallback?.call(viewState),
        onFailureCallback: () => onFailureCallback?.call(),
      );
    }
  }

  Future<void> autoSignInViaDeepLink({
    required OpenAppDeepLinkData openAppDeepLinkData,
    required OnAutoSignInViaDeepLinkSuccessCallback onAutoSignInSuccessCallback,
    required OnDeepLinkFailureCallback onFailureCallback,
  }) async {
    try {
      final autoSignInViaDeepLinkInteractor = Get.find<AutoSignInViaDeepLinkInteractor>();

      final autoSignInViewState = await autoSignInViaDeepLinkInteractor.execute(
        baseUri: openAppDeepLinkData.baseUri,
        tokenOIDC: openAppDeepLinkData.tokenOIDC,
        oidcConfiguration: openAppDeepLinkData.oidcConfiguration,
      ).last;

      autoSignInViewState.foldSuccess<AutoSignInViaDeepLinkSuccess>(
        onSuccess: onAutoSignInSuccessCallback,
        onFailure: (failure) => onFailureCallback.call(),
      );
    } catch (e) {
      logWarning('DeepLinksManager::_autoSignInViaDeepLink:Exception = $e');
      onFailureCallback.call();
    }
  }

  void _showConfirmDialogSwitchAccount({
    required BuildContext context,
    required String currentUsername,
    required String newUsername,
    required Function onConfirmAction,
    required Function onCancelAction,
  }) {
    final appLocalizations = AppLocalizations.of(context);

    MessageDialogActionManager().showConfirmDialogAction(
      context,
      '',
      appLocalizations.yes,
      title: appLocalizations.switchAccountConfirmation,
      alignCenter: true,
      outsideDismissible: false,
      listTextSpan: [
        TextSpan(text: appLocalizations.youAreCurrentlyLoggedInWith),
        TextSpan(
          text: ' $currentUsername',
          style: ThemeUtils.textStyleM3BodyMedium1.copyWith(
            fontWeight: FontWeight.bold,
          ),
        ),
        const TextSpan(text: '. '),
        TextSpan(text: appLocalizations.doYouWantToLogOutAndSwitchTo),
        TextSpan(
          text: ' $newUsername',
          style: ThemeUtils.textStyleM3BodyMedium1.copyWith(
            fontWeight: FontWeight.bold,
          ),
        ),
        const TextSpan(text: '?'),
      ],
      onConfirmAction: onConfirmAction,
      onCancelAction: onCancelAction,
    );
  }
}
