import 'dart:async';
import 'dart:io';

import 'package:app_links/app_links.dart';
import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/main.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/launcher_url_util.dart';
import 'package:electronic_assistant/utils/mmkv_util.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:url_launcher/url_launcher.dart';
import '../dialog/desktop_shortcut_dialog.dart';
import 'android_shortcut.dart';

class DesktopShortcutUtils {
  DesktopShortcutUtils._();

  static const String routePath = 'route_path';

  static const String _showMaxTimeTag = 'showMaxTime';
  static const int _showMaxFrequency = 1;

  static Map<String, dynamic>? intentMap;

  static StreamSubscription<Uri>? linkSubscription;

  static AppLinks appLinks = AppLinks();

  static Future<void> registerDesktopShortcut() async {
    if (Platform.isAndroid) {
      androidShortCut.register();
    } else if (Platform.isIOS) {
      //TODO IOS
      registerIosDeeplink();
    }
  }

  static Future<void> registerIosDeeplink() async {
    // Handle links
    linkSubscription = appLinks.uriLinkStream.listen((uri) {
      debugPrint('onAppLink: $uri');
      if (uri.scheme == "xiaoting") {
        DesktopShortcutUtils.setLaunchAction(LaunchAction.recordAudioAction);
        DesktopShortcutUtils.setRouteAction(LaunchAction.recordAudioAction);
      }
    });
  }

  static void isShowTipsDialog(void Function() nextCallback) {
    if (!_isShow()) {
      nextCallback();
      return;
    }
    showAddDesktopShortcutDialog(onConfirm: () {
      if (Platform.isAndroid) {
        showAddDesktopShortcutTipsDialog(onConfirm: () {
          androidShortCut.addRecordShortcut();
        }, onDismiss: () {
          nextCallback();
        });
      } else if (Platform.isIOS) {
        _launchUrl();
        nextCallback();
      }
    }, onCancel: () {
      nextCallback();
    });
    _setShowOnce();
  }

  static Future<void> _launchUrl() async {
    LauncherUrlUtil.launchHttpUrl('https://xiaoting.atmob.com/record',
        mode: LaunchMode.externalApplication);
  }

  static bool _isShow() {
    return KVUtil.getInt(_showMaxTimeTag, 0) < _showMaxFrequency;
  }

  static void _setShowOnce() {
    int maxFrequency = KVUtil.getInt(_showMaxTimeTag, 0);
    maxFrequency++;
    KVUtil.putInt(_showMaxTimeTag, maxFrequency);
  }

  static void requestAddDesktopShortcut() {
    if (Platform.isAndroid) {
      showAddDesktopShortcutTipsDialog(
          onConfirm: () {
            androidShortCut.addRecordShortcut();
          },
          onDismiss: () {});
    } else if (Platform.isIOS) {
      //TODO IOS
      _launchUrl();
    }
    _setShowOnce();
  }

  static void setLaunchAction(String action) {
    intentMap ??= {};
    intentMap?[LaunchAction.key] = action;
  }

  static Map<String, dynamic>? getRouteMap() {
    return intentMap;
  }

  static void clearRouteMap() {
    intentMap = null;
  }

  static void setRouteAction(String recordAudioAction) {
    if (recordAudioAction == LaunchAction.recordAudioAction) {
      Get.toNamed(RoutePath.record);
    }
  }
}

class LaunchAction {
  static const String key = 'launchAction';
  static const String recordAudioAction = 'record_audio';
}
