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

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

class Device {
  static Locale? locale;

  final Directory baseDir;

  Device(this.context, this.baseDir);

  AndroidDeviceInfo? androidDeviceInfo;

  /// 获取平台性能
  int get androidSdkInt => androidDeviceInfo != null ? androidDeviceInfo!.version.sdkInt : 100;
  bool get isOldAndroid => androidSdkInt < 26; // 安卓8以下
  bool get isLowRamDevice => androidDeviceInfo != null ? androidDeviceInfo!.isLowRamDevice : false;
  bool get isLowEndDevice => Platform.numberOfProcessors <= 1 || isOldAndroid || isLowRamDevice;

  static double get devPixelRatio => PlatformDispatcher.instance.views.first.devicePixelRatio;
  static Size get physicalSize => PlatformDispatcher.instance.views.first.physicalSize;
  static Size get logicalSize => physicalSize / devPixelRatio;

  ///final Size screenSize;
  final BuildContext context;

  final aspectRatio = 3.0 / 2.0; // 图片宽高比按照 2 / 3 设定，相应的 board play 区域也是这个比例

  final _tabletProfile = DeviceProfile(horizontalPadding: 60, verticalPadding: 10);

  final _profile = DeviceProfile(horizontalPadding: 10, verticalPadding: 10);

  double _bannerHeight = 0;

  int get previewImageSize => (boardSize.shortestSide * devicePixelRatio).toInt();

  set bannerHeight(double height) {
    _bannerHeight = height;
  }

  /// 获取当前配置
  DeviceProfile get profile => isTablet ? _tabletProfile : _profile;

  /// 屏幕尺寸
  Size get screenSize => MediaQuery.of(context).size;

  /// 像素密度
  double get devicePixelRatio => isLowEndDevice ? 1 : MediaQuery.of(context).devicePixelRatio;
  double get dpi => devicePixelRatio * 160;

  /// safeArea高度 Z
  double get safeAreaHeight => MediaQuery.of(context).viewPadding.top + MediaQuery.of(context).viewPadding.bottom;

  /// 获取appbar的高度
  double get appBarHeight => AppBar().preferredSize.height;

  /// Play 尺寸
  Size get boardSize =>
      Size(screenSize.width - profile.horizontalPadding * 2, screenSize.height - (safeAreaHeight + appBarHeight + profile.verticalPadding * 2));

  /// 根据谷歌的描述，banner尺寸最小50px， 最大不超过15%
  double get estimateBannerHeight => max(50, screenSize.height * 0.15).truncateToDouble();
  // double get bannerHeight => _bannerHeight == 0 ? estimateBannerHeight : _bannerHeight;
  double get bannerHeight => _bannerHeight != 0
      ? _bannerHeight
      : isTablet
      ? 90
      : 50;

  /// 是否平板
  bool get isTablet => screenSize.shortestSide >= 600;

  String filePath(String relativePath) => '${baseDir.path}/$relativePath';

  // board核心绘制区域
  Rect get targetRect {
    final double availableHeight = screenSize.height - appBarHeight - bannerHeight - 10; // -10 是预留给banner广告的间隔距离

    final double paddedWidth = screenSize.width - 2 * profile.horizontalPadding;
    final double paddedHeight = availableHeight - 2 * profile.verticalPadding;

    final double targetWidth = paddedWidth;
    final double targetHeight = targetWidth * aspectRatio;

    final double finalPuzzleWidth;
    final double finalPuzzleHeight;

    if (targetHeight > paddedHeight) {
      finalPuzzleHeight = paddedHeight;
      finalPuzzleWidth = paddedHeight / aspectRatio;
    } else {
      finalPuzzleWidth = targetWidth;
      finalPuzzleHeight = targetHeight;
    }

    final double targetYStart = appBarHeight + profile.verticalPadding + (paddedHeight - finalPuzzleHeight) / 2;
    final double targetXStart = profile.horizontalPadding + (paddedWidth - finalPuzzleWidth) / 2;

    final newTargetRect = Rect.fromLTWH(targetXStart, targetYStart, finalPuzzleWidth, finalPuzzleHeight);

    return newTargetRect;
  }

  // 最佳图片分辨率
  Size get bestImageSize => Size(targetRect.width * devPixelRatio, targetRect.height * devPixelRatio);
}

class DeviceProfile {
  final double horizontalPadding; // 水平padding
  final double verticalPadding; // 垂直padding

  DeviceProfile({
    this.horizontalPadding = 10,
    this.verticalPadding = 10, //Play board padding
  });
}
