import 'dart:io';

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/utils/utils.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:window_manager/window_manager.dart';


class TrayController extends GetxController with TrayListener {
  late Menu trayMenu;
  var disabledTun = false.obs;
  var show = false.obs;
  var isSHow = false;
  var isShowConsole = false.obs;

  Future<void> initTray() async {
    await trayManager.setIcon('assets/images/logo/logo.ico');
     // await trayManager.setTitle('jsq');
    updateTray();
    trayManager.addListener(this);
  }

  Future<void> updateTray() async {
    final visible = await windowManager.isVisible();

    if(Platform.isWindows){
      isSHow = true;
    }

    final disabled = controllers.global.connectStatus.value == false;

    var disabledSerivce = controllers.global.connectStatus.value;
    if(Platform.isWindows){
      disabledSerivce = true;
    }

    var serivceName = controllers.cc_service.installStatus.value == true? 'setting_service_uninstall'.tr : 'setting_service_install'.tr,


    trayMenu = Menu(items: [
      MenuItem.checkbox(label: 'tray_show'.tr, checked: visible, onClick: handleClickShow),
      MenuItem.separator(),
      // MenuItem.submenu(
      //   label: 'proxie_group_title'.tr,
      //   disabled: disabled,
      //   submenu: Menu(
      //     items: controllers.global.nodeModes.map((it) =>
      //         MenuItem.checkbox(
      //           label: it.name,
      //           checked: it.id == nodeId.value,
      //           onClicked: () {
      //             handleClickProxieItem(it)
      //           },
      //         )
      //     ).toList(),
      //   ),
      // ),
      MenuItem.submenu(
        label: 'proxie_group_title'.tr,
        disabled: false,
        submenu: Menu(
          items: controllers.global.nodeModes.map((it) =>
              MenuItem.checkbox(
                label: it.name,
                checked: it.id == controllers.global.nodeId.value,
                onClick: (m) => handleClickProxieItem(it, m),
              )
          ).toList(),
        ),
      ),
      // MenuItem(
      //   label: 'tray_restart_clash_core'.tr,
      //   disabled: !controllers.service.isCanOperationCore,
      //   onClick: handleClickRestartClashCore,
      // ),
      MenuItem.checkbox(
        label: 'setting_set_as_system_proxy'.tr,
        checked: controllers.global.systemProxy.value,
        disabled: false, //disabled || controllers.global.systemProxySwitchIng.value
        onClick: handleClickSetAsSystemProxy,
      ),
      MenuItem.checkbox(
        label: 'route_tun_title'.tr,
        checked: disabledTun.value,
        disabled: disabledSerivce,
        onClick: handleClickSetAsTunProxy,
      ),
      MenuItem.checkbox(
        label: serivceName,
        checked: controllers.cc_service.installStatus.value,
        disabled: disabledSerivce,
        onClick: handleClickServiceModeSwitch,
      ),
      MenuItem.submenu(
          label: 'tray_copy_command_line_proxy'.tr,
          disabled: disabled,
          submenu: Menu(items: [
            MenuItem(label: 'bash', onClick: handleClickCopyCommandLineProxy),
            MenuItem(label: 'cmd', onClick: handleClickCopyCommandLineProxy),
            MenuItem(label: 'powershell', onClick: handleClickCopyCommandLineProxy),
          ])),
      MenuItem.separator(),

      MenuItem(label: '服务端口:${controllers.config.servicePort.value}', disabled: true,),


      isSHow == true ?  MenuItem(label: "显示控制台", onClick: handleClickConsoleShow,checked: isShowConsole.value)
          : MenuItem(disabled: true,label: "显示控制台", onClick: handleClickConsoleShow),


      MenuItem(label: 'tray_exit'.tr, onClick: handleClickExit),
    ]);


    await trayManager.setContextMenu(trayMenu);
  }

  @override
  void onTrayIconMouseDown() async {
    await controllers.window.showWindow();
  }

  @override
  void onTrayIconRightMouseDown() async {
    show.value = true;
    await updateTray();
    await trayManager.popUpContextMenu();
    show.value = false;
  }
  Future<void> handleClickConsoleShow(MenuItem menuItem) async {
    isSHow = true;
    if (menuItem.checked == true) {
      isShowConsole.value = false;
      controllers.global.hideConsole();
    } else {
      isShowConsole.value = true;
      controllers.global.showConsole();
    }
  }
  Future<void> handleClickShow(MenuItem menuItem) async {
    show.value = false;
    if (menuItem.checked == true) {
      await controllers.window.hideWindow();
    } else {
      await controllers.window.showWindow();
    }
  }
  Future<void> handleClickProxieItem(NodeMode proxie, MenuItem menuItem) async {
    controllers.global.selectNode(proxie);
    await controllers.global.swift(proxie.name ?? "");
  }
  // Future<void> handleClickProxieItem(ProxieProxiesItem proxie, MenuItem menuItem) async {
  //   await controllers.global.handleSetProxieGroup(proxie, menuItem.label!);
  // }

  Future<void> handleClickSetAsSystemProxy(MenuItem menuItem) async {
    await controllers.global.systemProxySwitch(menuItem.checked != true);
  }

  Future<void> handleClickSetAsTunProxy(MenuItem menuItem) async {
    disabledTun.value = menuItem.checked != true;
    await controllers.global.TunProxySwitch(menuItem.checked != true);
  }

  Future<void> handleClickCopyCommandLineProxy(MenuItem menuItem) async {
    final title = menuItem.label!;
    final proxyConfig = controllers.core.proxyConfig;
    await copyCommandLineProxy(title, http: proxyConfig.http, https: proxyConfig.https);
  }

  Future<void> handleClickAbout(MenuItem menuItem) async {
    //await launchUrl(Uri.parse('https://github.com/csj8520/clash_for_flutter'));
  }

  Future<void> handleClickExit(MenuItem menuItem) async {
    await controllers.global.handleExit();
  }

  Future<void> handleClickRestartClashCore(MenuItem menuItem) async {
    controllers.global.allowStatusUpdate = true;
    await controllers.service.reloadClashCore();
  }

  Future<void> handleClickServiceModeSwitch(MenuItem menuItem) async {
    await controllers.cc_service.serviceModeSwitch(menuItem.checked != true);
  }

  @override
  void dispose() {
    trayManager.removeListener(this);
    super.dispose();
  }
}
