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 'package:timeago/timeago.dart';
import 'store/store.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';

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

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

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

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

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

    /// 依赖注入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);
  }

  //启动图延时移除方法
  static initialization(BuildContext? context) async {
    //延迟3秒
    await Future.delayed(const Duration(seconds: 3));
    FlutterNativeSplash.remove();
  }
}

// my_custom_messages.dart
class MyCustomMessages implements LookupMessages {
  @override
  String prefixAgo() => '';
  @override
  String prefixFromNow() => '';
  @override
  String suffixAgo() => '';
  @override
  String suffixFromNow() => '';
  @override
  String lessThanOneMinute(int seconds) => '现在';
  @override
  String aboutAMinute(int minutes) => '$minutes分钟';
  @override
  String minutes(int minutes) => '$minutes分钟';
  @override
  String aboutAnHour(int minutes) => '$minutes分钟';
  @override
  String hours(int hours) => '$hours小时';
  @override
  String aDay(int hours) => '$hours小时';
  @override
  String days(int days) => '$days天';
  @override
  String aboutAMonth(int days) => '$days天';
  @override
  String months(int months) => '$months月';
  @override
  String aboutAYear(int year) => '$year年';
  @override
  String years(int years) => '$years年';
  @override
  String wordSeparator() => ' ';
}
