import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/device_utils.dart';
import 'package:shared/utils/log_utils.dart';
import 'package:shared/utils/screen_util.dart';
import '../global_provider_container.dart';
import 'app_config.dart';

part 'app_config_service.g.dart';

@Riverpod(keepAlive: true)
class AppConfigService extends _$AppConfigService {
  /// 静态方法获取当前单例对象
  static AppConfigService getInstance() {
    return globalContainer.read(appConfigServiceProvider.notifier);
  }

  static AppConfig getState() {
    return globalContainer.read(appConfigServiceProvider);
  }

  @override
  AppConfig build() {
    return AppConfig(
      whRatio: 1,
      isFullScreenDevice: false,
      safeAreaPaddingTop: 0,
      safeAreaPaddingBottom: 0,
      density: 0,
    );
  }

  Future initAsync() async {
    final startTime = DateTime.now();
    Log.d('----AppConfigService------initAsync() --  start ---');

    // package_info 插件获取到App的包信息
    final packageInfo = await PackageInfo.fromPlatform();
    var configState = state.copyWith(packageInfo: packageInfo);

    // device_info_plus插件获取设备信息
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if (DeviceUtils.isIOS) {
      //赋值iOS信息
      configState.iosDeviceInfo = await deviceInfoPlugin.iosInfo;
    } else if (DeviceUtils.isAndroid) {
      //赋值Android信息
      configState.androidDeviceInfo = await deviceInfoPlugin.androidInfo;
    }

    state = configState;

    Log.d('----AppConfigService------initAsync() --  end 任务耗时:${DateTime.now().difference(startTime).inMilliseconds} ms ---');
  }

  void initSize(BuildContext context) {
    if (state.mSize == null) {
      //屏幕大小
      final size = MediaQuery.of(context).size;
      //密度
      final ratio = MediaQuery.of(context).devicePixelRatio;
      //设备像素 px
      final width = size.width * ratio;
      final height = size.height * ratio;

      var configState = state.copyWith(
        mSize: size,
        mRatio: ratio,
        width: width,
        height: height,
        whRatio: height / width,
        isFullScreenDevice: (height / width) > 1.88,
        topPadding: MediaQuery.of(context).padding.top,
        bottomPadding: MediaQuery.of(context).padding.bottom,
        textScaleFactor: MediaQuery.of(context).textScaleFactor,
        platformBrightness: MediaQuery.of(context).platformBrightness,
        viewInsets: MediaQuery.of(context).viewInsets,
        padding: MediaQuery.of(context).padding,
        alwaysUse24HourFormat: MediaQuery.of(context).alwaysUse24HourFormat,
        accessibleNavigation: MediaQuery.of(context).accessibleNavigation,
        invertColors: MediaQuery.of(context).invertColors,
        disableAnimations: MediaQuery.of(context).disableAnimations,
        boldText: MediaQuery.of(context).boldText,
        orientation: MediaQuery.of(context).orientation,
        density: MediaQuery.devicePixelRatioOf(context),
      );

      state = configState;

      //初始化Flustarts工具类中 ScreenUtils 工具，可以设置设计稿的大小，可以选择性的使用 getAdapterSizeCtx 适配大小屏不同尺寸的展示
      setDesignWHD(375, 667, density: configState.density);

      printAppConfig();
    }
  }

  /// 测试并打印数据
  void printAppConfig() {
    Log.d('AppConfig 是否是Release环境 ${state.isRelease}');
    Log.d('AppConfig 插件获获取到的app【appName】${state.appName}');
    Log.d('AppConfig 插件获获取到的app【packageName】${state.packageName}');
    Log.d('AppConfig 插件获获取到的app【version】${state.version}');
    Log.d('AppConfig 插件获获取到的app【buildNumber】${state.buildNumber}');
    Log.d('AppConfig 插件获取到设备信息【iosDeviceInfo】${state.iosDeviceInfo}');
    Log.d('AppConfig 插件获取到设备信息【androidDeviceInfo】${state.androidDeviceInfo}');
    Log.d('AppConfig---设备像素比dpr(物理像素/逻辑像素)【mRatio】:${state.mRatio}');
    Log.d('AppConfig--- ScreenUtil像素比: ${state.density}');
    Log.d('AppConfig---逻辑像素pt【mSize.width * mSize.height】:${state.mSize!.width} x ${state.mSize!.height}');
    Log.d('AppConfig---屏幕分辨率px(物理像素)【mSize.width * mRatio】 X【mSize.height * mRatio】:${state.width} x ${state.height} 屏幕的宽高比：${state.whRatio}');
    Log.d('AppConfig---上边刘海(状态栏)【MediaQuery.of(context).padding.top】: ${state.topPadding}');
    Log.d('AppConfig---下边导航【MediaQuery.of(context).padding.bottom】:  ${state.bottomPadding}');
    Log.d('AppConfig---textScaleFactor【MediaQuery.of(context).textScaleFactor】: ${state.textScaleFactor}');
    Log.d('AppConfig---platformBrightness【MediaQuery.of(context).platformBrightness】: ${state.platformBrightness}');
    Log.d('AppConfig---viewInsets【MediaQuery.of(context).viewInsets】: ${state.viewInsets}');
    Log.d('AppConfig---padding【MediaQuery.of(context).padding】: ${state.padding}');
    Log.d('AppConfig---alwaysUse24HourFormat: ${state.alwaysUse24HourFormat}');
    Log.d('AppConfig---accessibleNavigation: ${state.accessibleNavigation}');
    Log.d('AppConfig---invertColors: ${state.invertColors}');
    Log.d('AppConfig---disableAnimations: ${state.disableAnimations}');
    Log.d('AppConfig---boldText: ${state.boldText}');
    Log.d('AppConfig---orientation:${state.orientation}');
    Log.d('AppConfig---宽高比:${(state.mSize!.height / state.mSize!.width)} isFullScreenDevice：${state.isFullScreenDevice}');
  }
}
