import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/store/dict.store.dart';
import 'package:sk_base_mobile/util/loading_util.dart';
import 'package:timeago/timeago.dart' as timeago;
import 'package:sk_base_mobile/constants/cache_key.dart';
import 'package:sk_base_mobile/services/app_info.service.dart';
import 'package:sk_base_mobile/services/dio.service.dart';
import 'package:sk_base_mobile/services/storage.service.dart';
import 'store/store.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';

/// 全局注册
class Global {
  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();

    /// 设置初始化竖屏
    // await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

    /// 设置跟随系统屏幕旋转
    await SystemChrome.setPreferredOrientations(DeviceOrientation.values);

    /// 设置系统UI 因用户反馈可能存在bug先隐藏功能
    setSystemUi();

    /// 初始化 几天前 的时区国际化
    timeago.setLocaleMessages('en', timeago.EnMessages());

    /// 依赖注入Loading工具
    await Get.putAsync<LoadingUtil>(() => LoadingUtil().init());

    /// 依赖注入持久化
    await Get.putAsync<StorageService>(() => StorageService().init());

    /// 依赖注入Http
    await Get.putAsync<DioService>(() => DioService().init());

    /// 依赖注入app信息
    await Get.putAsync<AppInfoService>(() => AppInfoService().init());

    /// 依赖注入用户信息
    await Get.putAsync<AuthStore>(() => AuthStore().init());

    /// 依赖注入字典信息
    await Get.putAsync<DictService>(() => DictService().init());

    if (StorageService.to.getString(CacheKeys.token, isWithUser: false) !=
        null) {
      /// 这里做一些缓存初始化后的操作
    }
  }

  static void setSystemUi() {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}
