import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/index.dart';
import 'package:vnoteapp/rpc.dart';
import 'package:vnoteapp/store/store.dart';
import 'package:fis_common/logger/logger.dart';

abstract class Global {
  static Future<void> init() async {
    logger.i("Global init start...");

    // 锁定横屏
    await SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);

    // SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);

    SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
    SystemChrome.setSystemUIChangeCallback((systemOverlaysAreVisible) async {
      await Future.delayed(const Duration(seconds: 1));
      SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
    });
    SystemChrome.setSystemUIOverlayStyle(
      const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.light,
      ),
    );
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

    // 日志
    if (kReleaseMode) {
      // 生产环境
      Get.isLogEnable = false;
    } else {
      // debug/profile 环境
      logger.setMinimumLevel(LogLevel.Degbug);
    }
    await logger.init();

    // Loading工具
    PromptBox.init();

    // 状态
    await Store.init();

    _initRpc();

    /// 全局管理器中心
    ManagerCenter.load();

    logger.i("Global init finish...");
  }

  static void _initRpc() {
    _updateRpcHostPath("http://192.168.6.80:8400");
  }

  static void _updateRpcHostPath(String hostPath) {
    final uri = Uri.parse(hostPath);
    rpc.setServerHost("${uri.host}:${uri.port}", uri.scheme == 'https');
    rpc.clearCache();
  }
}
