import 'dart:io';
import 'dart:ui';


import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:kommon/tool/sp_util.dart';
import 'package:proxy_manager/proxy_manager.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

import 'app/routes/app_pages.dart';
import 'app/service/clash_service.dart';
import 'main.mapper.g.dart' show initializeJsonMapper;

final proxyManager = ProxyManager();
final isDesktop = GetPlatform.isDesktop;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (isDesktop) {
    await Future.wait([
      Future.microtask(() async {
        await windowManager.ensureInitialized();
        await windowManager.setPreventClose(true);
      })
    ]);
  }
  initializeJsonMapper();
  JsonMapper().useAdapter(JsonMapperAdapter(valueDecorators: {
    typeOf<Map<String, String>>(): (value) {
      return Map.castFrom<dynamic, dynamic, String, String>(value);
    },
  }));

  const Set<PointerDeviceKind> kTouchLikeDeviceTypes = <PointerDeviceKind>{
    PointerDeviceKind.touch,
    PointerDeviceKind.mouse,
    PointerDeviceKind.stylus,
    PointerDeviceKind.invertedStylus,
    PointerDeviceKind.unknown
  };

  await initAppService();

  runApp(
    GetMaterialApp(
      scrollBehavior: const MaterialScrollBehavior().copyWith(
          scrollbars: true,
          dragDevices: kTouchLikeDeviceTypes
      ),
      title: "Application",
      initialRoute: AppPages.INITIAL,
      getPages: AppPages.routes,

    ),
  );

  if (isDesktop) {
    initWindow();
  }

}

Future<void> initWindow() async {
  const width = 375.0;
  const height = 736.0;

  WindowOptions opts = const WindowOptions(
    minimumSize: Size(width, height),
    maximumSize: Size(width, height - kToolbarHeight),
    size: Size(width, height),
    center: true,
    backgroundColor: Colors.transparent,
    skipTaskbar: true,
    titleBarStyle: TitleBarStyle.hidden,
  );
  windowManager.waitUntilReadyToShow(opts, () {
    // hide window when start
    // if (Get.find<ClashService>().isHideWindowWhenStart() && kReleaseMode) {
    //   windowManager.hide();
    // }
    //windowManager.show();
    // windowManager.focus();
  });
}


Future<void> initAppService() async {
  await SpUtil.getInstance();
  await Get.putAsync(() => ClashService().init());
  if (isDesktop) {
    // await Get.putAsync(() => AutostartService().init());
  }
  //Get.put(ThemeController());
}

Future<void> initAppTray(
    {List<MenuItem>? details, bool isUpdate = false}) async {
  await trayManager.setIcon(Platform.isWindows
      ? 'assets/images/logo/logo.ico'
      : 'assets/images/logo/logo.png');

  List<MenuItem> items = [
    MenuItem(
      key: 'show',
      label: 'Show Fclash'.tr,
    ),
    MenuItem.separator(),
    MenuItem(
      key: 'exit',
      label: 'Exit Fclash'.tr,
    ),
  ];
  if (details != null) {
    items.insertAll(0, details);
  }
  await trayManager.setContextMenu(Menu(items: items));
}