import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:naiyouwl/app/bean/proxie.dart';
import 'package:naiyouwl/app/controller/controllers.dart';
import 'package:naiyouwl/app/data/model/NodeMode.dart';
import 'package:naiyouwl/app/network/api_service.dart';
import 'package:naiyouwl/app/utils/logger.dart';
import 'package:naiyouwl/app/utils/system_proxy.dart';
import 'package:naiyouwl/app/utils/utils.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

class GlobalController extends GetxController {

  late BuildContext context;
  final List<String> modes = ['rule', 'global'];
  var nodeModes = <NodeMode>[].obs;
  var isLoading = false.obs;
  var errorMsg = ''.obs;
  var systemProxySwitchIng = false.obs;
  bool allowStatusUpdate = false;
  final selectedNode = Rx<NodeMode?>(null);
  // 策略组
  var proxieGroups = <ProxieProxiesItem>[].obs;
  // 代理集
  var proxieProviders = <ProxieProviderItem>[].obs;
  // 代理
  var proxieProxies = <ProxieProxiesItem>[].obs;
  // 所有节点
  var allProxies = <String, ProxieProxiesItem>{}.obs;

  final List<String> groupInternalTypes = ['DIRECT', 'REJECT', 'GLOBAL'];
  final List<String> groupTypes = [
    ProxieProxieType.selector,
    ProxieProxieType.urltest,
    ProxieProxieType.fallback,
    ProxieProxieType.loadbalance,
  ];
  Future<void> init(BuildContext context) async {
    this.context = context;
    watchExit();

    // init plugins
    await controllers.tray.initTray();
    controllers.window.initWindow();
    //controllers.protocol.initProtocol();

    // init config
    await controllers.config.initConfig();
    // final language = controllers.config.config.value.language.split('_');
    //
    // await applyLanguage(Locale(language[0], language[1]));

    await controllers.service.initConfig();
    // init service
    await controllers.service.startService();
    if (controllers.service.serviceStatus.value != RunningState.running) return;

    // init clash core

    // await controllers.service.startClashCore();
    // if (controllers.service.coreStatus.value != RunningState.running) return;
    // await controllers.core.updateVersion();
    // await controllers.pageProxie.updateDate();

    initRegularlyUpdate();
  }

  Future<void> fetchNodes() async {
    nodeModes.value = await ApiService().getNode("/api/client/v4/nodes?vless=1");
    await makeProxy();
    if(controllers.service.coreStatus.value == RunningState.stoped){
      await controllers.service.reloadClashCore();
    }
    if (controllers.service.coreStatus.value != RunningState.running) return;
    await controllers.core.updateVersion();
    await updateDate();

    NodeMode? targetNode;
    if (selectedNode.value == null) {
      targetNode = await findNodeWithMinUsers(nodeModes);
    } else {
      targetNode = selectedNode.value;
    }
    if (targetNode != null){
      selectNode(targetNode);
      ProxieProxiesItem? targetProxie = await findProxieByName(targetNode.name);
      if (targetProxie != null) {
        handleSetProxieGroup(targetProxie, targetNode.name);
      }
    }


  }
  // Future<ProxieProxiesItem> findProxieByName(String name) async {
  //   return proxieGroups.firstWhere((proxie) => proxie['name'] == name, orElse: () => null);
  // }

  Future<ProxieProxiesItem?> findProxieByName(String name) async {
    ProxieProxiesItem? result;
    try {
      result = proxieGroups.firstWhere((proxie) => proxie.all!.contains(name));
    } catch (e) {
      // 在这里可以处理异常,或者只是简单地返回 null
      return null;
    }
    return result;
  }



  Future<void> makeProxy() async {
     await controllers.config.makeClashConfig(nodeModes);
     // await controllers.service.reloadClashCore();

  }
  Future<NodeMode> findNodeWithMinUsers(List<NodeMode> nodes) async {
    return nodes
        .where((node) => node.countryCode == "hk")
        .reduce((a, b) => a.onlineUsers < b.onlineUsers ? a : b);
  }




  Future<SystemProxyConfig> getSysProxy() async{
     return  await SystemProxy.instance.get();
  }

  Future<void> systemProxySwitch(bool open) async {
    systemProxySwitchIng.value = true;

    await SystemProxy.instance.set(open ? controllers.core.proxyConfig : SystemProxyConfig());
    await controllers.config.setSystemProxy(open);
    systemProxySwitchIng.value = false;
  }

  Future<dynamic> _updateProxie() async {
    final proxie = await controllers.core.fetchProxie();
    final global = proxie.proxies["GLOBAL"]!;
    proxieGroups.value = global.all!
        .where((it) => !groupInternalTypes.contains(it) && groupTypes.contains(proxie.proxies[it]!.type))
        .map((it) => proxie.proxies[it]!)
        .toList();
    proxieProxies.value = global.all!
        .where((it) => !groupInternalTypes.contains(it) && !groupTypes.contains(proxie.proxies[it]!.type))
        .map((it) => proxie.proxies[it]!)
        .toList();
    if (controllers.core.config.value.mode == 'global') proxieGroups.insert(0, global);
  }

  Future<dynamic> _updateProxieProvider() async {
    proxieProviders.value = (await controllers.core.fetchProxieProvider()).providers.values.where((it) => it.vehicleType != 'Compatible').toList();
    for (final it in proxieProviders) {
      it.proxies.sort((a, b) {
        if (a.delay == 0) return 1;
        if (b.delay == 0) return -1;
        return a.delay - b.delay;
      });
    }
  }

  Future<void> updateDate() async {
    log.debug('controller.proxie.updateDate()');

    await controllers.core.updateConfig();
    await _updateProxie();
    await _updateProxieProvider();
    allProxies.clear();
    for (final provide in proxieProviders) {
      for (final it in provide.proxies) {
        allProxies[it.name] = it;
      }
    }
    for (final it in proxieProxies) {
      allProxies[it.name] = it;
    }
    for (final it in proxieGroups) {
      allProxies[it.name] = it;
    }
    proxieGroups.refresh();
    proxieProxies.refresh();
    proxieProviders.refresh();
    allProxies.refresh();
  }


  Future<void> handleSetProxieGroup(ProxieProxiesItem proxie, String value) async {
    if (proxie.now == value) return;
    await controllers.core.fetchSetProxieGroup(proxie.name, value);
    await updateDate();
    if (controllers.config.config.value.breakConnections) {
      final conn = await controllers.core.fetchConnection();
      for (final it in conn.connections) {
        if (it.chains.contains(proxie.name)) controllers.core.fetchCloseConnections(it.id);
      }
    }
  }

  Future<void> handleSetSelectProxieGroup(NodeMode proxie, String value) async {
    if (proxie.name == value) return;
    await controllers.core.fetchSetProxieGroup(proxie.name, value);
    await updateDate();
    final conn = await controllers.core.fetchConnection();
    for (final it in conn.connections) {
      if (it.chains.contains(proxie.name)) controllers.core.fetchCloseConnections(it.id);
    }
  }

  void watchExit() {
    // watch process kill
    // ref https://github.com/dart-lang/sdk/issues/12170
    if (Platform.isMacOS) {
      // windows not support https://github.com/dart-lang/sdk/issues/28603
      // for macos 任务管理器退出进程
      ProcessSignal.sigterm.watch().listen((_) {
        stdout.writeln('exit: sigterm');
        handleExit();
      });
    }
    // for macos, windows ctrl+c
    ProcessSignal.sigint.watch().listen((_) {
      stdout.writeln('exit: sigint');
      handleExit();
    });
  }
  void selectNode(NodeMode node) {
    controllers.global.selectedNode.value = node;
    _storeSelectedNode(node);
  }

  Future<void> _storeSelectedNode(NodeMode node) async {
    final prefs = await SharedPreferences.getInstance();
    // 为简化起见,我们只存储node的ID,但您可以根据需要存储更多信息
    prefs.setInt('selectedNodeId', node.id);
    await loadSelectedNode();
  }


  Future<void> loadSelectedNode() async {
    final prefs = await SharedPreferences.getInstance();
    final selectedNodeId = prefs.getInt('selectedNodeId');
    if (selectedNodeId != null) {
      //selectedIndex.value = nodeModes.indexWhere((item) => item.id == selectedNodeId);
      selectedNode.value = nodeModes.firstWhere((node) => node.id == selectedNodeId);
    }
  }


  void initRegularlyUpdate() {
    Future.delayed(const Duration(minutes: 5)).then((_) async {
      for (final it in controllers.config.config.value.subs) {
        try {
          if (it.url == null || it.url!.isEmpty) continue;
          if (((DateTime.now().millisecondsSinceEpoch ~/ 1000) - (it.updateTime ?? 0)) < controllers.config.config.value.updateInterval) continue;
          final chenged = await controllers.config.updateSub(it);
          if (!chenged) continue;
          if (it.name != controllers.config.config.value.selected) continue;
          // restart clash core
          await controllers.service.reloadClashCore();
          await Future.delayed(const Duration(seconds: 20));
        } catch (_) {}
      }
      initRegularlyUpdate();
    });
  }

  Future<void> handleExit() async {
    await controllers.service.stopService();
    await trayManager.destroy();
    await windowManager.destroy();
    // exit(0);
  }

  @override
  void dispose() {
    controllers.tray.dispose();
    controllers.window.dispose();
    //controllers.protocol.dispose();
    super.dispose();
  }
}