import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import '../app/components/ix_snackbar.dart';
import '../app/constants/configs.dart';
import '../app/data/sp/ix_sp.dart';
import '../app/routes/app_pages.dart';
import 'package:flutter_custom_tabs/flutter_custom_tabs.dart' as ct;

import '../config/translations/strings_enum.dart';

class SystemHelper {
  // static Future<void> openEmail(String email) async {
  //   final Uri emailUri = Uri(
  //     scheme: 'mailto',
  //     path: email,
  //   );
  //   try {
  //     await launchUrl(
  //       emailUri,
  //       mode: LaunchMode.externalApplication,
  //     );
  //   } catch (e) {
  //     CustomSnackBar.showCustomErrorSnackBar(
  //         title: Strings.error.tr, message: Strings.eUtilOpenEmail.tr);
  //   }
  // }

  static Future<void> openGooglePlay(String packageName) async {
    final String url =
        'https://play.google.com/store/apps/details?id=$packageName';
    try {
      // 直接尝试打开 Google Play 应用
      await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
    } catch (e) {
      IXSnackBar.showIXErrorSnackBar(
        title: Strings.error.tr,
        message: Strings.eUtilOpenGooglePlay.tr,
      );
    }
  }

  static Future<void> openGooglePlayUrl(String url) async {
    try {
      // 直接尝试打开 Google Play 应用
      await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
    } catch (e) {
      IXSnackBar.showIXErrorSnackBar(
        title: Strings.error.tr,
        message: Strings.eUtilOpenGooglePlay.tr,
      );
    }
  }

  static Future<void> openGoogleChrome(String url) async {
    try {
      await ct.launchUrl(
        Uri.parse(url),
        customTabsOptions: ct.CustomTabsOptions.partial(
          configuration: ct.PartialCustomTabsConfiguration.adaptiveSheet(
            initialHeight: Get.height * 0.7,
            initialWidth: Get.width * 0.4,
            activitySideSheetMaximizationEnabled: true,
            activitySideSheetDecorationType:
                ct.CustomTabsActivitySideSheetDecorationType.shadow,
            activitySideSheetRoundedCornersPosition:
                ct.CustomTabsActivitySideSheetRoundedCornersPosition.top,
            cornerRadius: 16,
          ),
        ),
        safariVCOptions: const ct.SafariViewControllerOptions.pageSheet(
          configuration: ct.SheetPresentationControllerConfiguration(
            detents: {
              ct.SheetPresentationControllerDetent.large,
              ct.SheetPresentationControllerDetent.medium,
            },
            prefersScrollingExpandsWhenScrolledToEdge: true,
            prefersGrabberVisible: true,
            prefersEdgeAttachedInCompactHeight: true,
          ),
          dismissButtonStyle: ct.SafariViewControllerDismissButtonStyle.close,
        ),
      );
    } catch (e) {
      try {
        await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
      } catch (e) {
        IXSnackBar.showIXErrorSnackBar(
          title: Strings.error.tr,
          message: Strings.eUtilOpenBrowser.tr,
        );
      }
    }
  }

  static Future<void> closeGoogleChrome() async {
    await ct.closeCustomTabs();
  }

  static Future<void> openWebPage(String url) async {
    try {
      final Uri webUri = Uri.parse(url);
      await launchUrl(webUri, mode: LaunchMode.externalApplication);
    } catch (e) {
      IXSnackBar.showIXErrorSnackBar(
        title: Strings.error.tr,
        message: Strings.eUtilOpenBrowser.tr,
      );
    }
  }

  static Future<void> openInAppWebPage(String url, String title) async {
    Get.toNamed(Routes.WEB, arguments: {'url': url, 'title': title});
  }

  static Future<void> handleDeepLink(String deepLink) async {
    try {
      final Uri uri = Uri.parse(deepLink);
      if (await canLaunchUrl(uri)) {
        await launchUrl(uri, mode: LaunchMode.externalApplication);
      } else {
        IXSnackBar.showIXErrorSnackBar(
          title: Strings.error.tr,
          message: Strings.unknownError.tr,
        );
      }
    } catch (e) {
      IXSnackBar.showIXErrorSnackBar(
        title: Strings.error.tr,
        message: Strings.unknownError.tr,
      );
    }
  }

  static void openPrivacyTerms() {
    final launch = IXSP.getLaunch();
    if (launch?.appConfig?.privacyAgreement != null &&
        launch?.appConfig?.privacyAgreement?.privacyTermsUrl != null) {
      String url = launch?.appConfig?.privacyAgreement?.privacyTermsUrl ?? '';
      if (url.indexOf("http") != 0) {
        url = '${Configs.websiteUrl}/$url';
      }
      Get.toNamed(
        Routes.WEB,
        arguments: {'url': url, 'title': Strings.privacyPolicy.tr},
      );
    } else {
      Get.toNamed(
        Routes.MARKDOWN,
        arguments: {'title': Strings.privacyPolicy.tr, 'filename': 'privacy'},
      );
    }
  }

  static void openTermsOfService() {
    final launch = IXSP.getLaunch();
    if (launch?.appConfig?.privacyAgreement != null &&
        launch?.appConfig?.privacyAgreement?.termsOfServiceUrl != null) {
      String url = launch?.appConfig?.privacyAgreement?.termsOfServiceUrl ?? '';
      if (url.indexOf("http") != 0) {
        url = '${Configs.websiteUrl}/$url';
      }
      Get.toNamed(
        Routes.WEB,
        arguments: {'url': url, 'title': Strings.termsOfService.tr},
      );
    } else {
      Get.toNamed(
        Routes.MARKDOWN,
        arguments: {'title': Strings.termsOfService.tr, 'filename': 'terms'},
      );
    }
  }
}
