import 'package:base_app/common/common.dart';
import 'package:base_app/res/index.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lpinyin/lpinyin.dart';
import 'dart:convert' as convert;

/// 工具类
class Utils {
  // 是否需要登录
  static bool isNeedLogin(String pageId) {
    return false;
  }

  /// 显示提示信息，在屏幕下方
  static void showSnackBar(BuildContext context, String msg) {
    Scaffold.of(context).showSnackBar(
      SnackBar(content: Text("$msg")),
    );
  }

  // 加载文本资源
  Future<String> loadAsset(String filename) async {
    return await rootBundle.loadString('assets/data/$filename');
  }

  /// 获取图片路径
  /// [name] 图片名称 不含后缀，assets/images文件夹下地图图片
  /// [format] 图片格式(后缀，如png,jpg等)
  static String getImgPath(String name, {String format: 'png'}) {
    return 'assets/images/$name.$format';
  }

  /// 获取文件名称
  /// [urlPath] 文件路径
  static String getFileName(String urlPath) {
    if (ObjectUtil.isEmpty(urlPath)) return '';
    List<String> listStr = urlPath.split("/");
    String name = listStr[listStr.length - 1];
    return name;
  }

  /// 获取字符串对应拼音的首字母的大写字母
  /// [str] 待处理字符串
  static String getPinyin(String str) {
    return PinyinHelper.getShortPinyin(str).substring(0, 1).toUpperCase();
  }

  /// 获取圆圈的背景
  static Color getCircleBg(String str) {
    String pinyin = getPinyin(str);
    return getCircleAvatarBg(pinyin);
  }

  /// 获取头像背景颜色
  /// [key] 对照表中的key
  static Color getCircleAvatarBg(String key) {
    return circleAvatarMap[key];
  }

  static Color getChipBgColor(String name) {
    String pinyin = PinyinHelper.getFirstWordPinyin(name);
    pinyin = pinyin.substring(0, 1).toUpperCase();
    return nameToColor(pinyin);
  }

  static Color nameToColor(String name) {
    // assert(name.length > 1);
    final int hash = name.hashCode & 0xffff;
    final double hue = (360.0 * hash / (1 << 15)) % 360.0;
    return HSVColor.fromAHSV(1.0, hue, 0.4, 0.90).toColor();
  }

  static String getTimeLine(BuildContext context, int timeMillis) {
//    LogUtil.e("countryCode: " +
//        Localizations.localeOf(context).countryCode +
//        "   languageCode: " +
//        Localizations.localeOf(context).languageCode);
    return TimelineUtil.format(timeMillis,
        locale: Localizations.localeOf(context).languageCode,
        dayFormat: DayFormat.Common);
  }

  /// 计算标题字号
  /// [title] 标题
  static double getTitleFontSize(String title) {
    // 小于10个字，18号字体
    if (ObjectUtil.isEmpty(title) || title.length < 10) {
      return 18.0;
    }
    int count = 0;
    List<String> list = title.split("");
    for (int i = 0, length = list.length; i < length; i++) {
      String ss = list[i];
      if (RegexUtil.isZh(ss)) {
        count++;
      }
    }
    //10个汉字或16个字符以上，14号字
    return (count >= 10 || title.length > 16) ? 14.0 : 18.0;
  }

  /// 判断升级状态，如果远程版本小于等于当前按本，返回0 不升级，
  /// 如果返回 1 升级
  /// > x 强升
  static int getUpdateStatus(String version, {String local}) {
    if (ObjectUtil.isEmpty(version)) return 0;
    String locVersion = local ?? AppConfig.version;
    int remote = int.tryParse(version.replaceAll('.', ''));
    int loc = int.tryParse(locVersion.replaceAll('.', ''));
    if (remote <= loc) {
      return 0;
    } else {
      return remote - loc;
    }
  }

  /// 获取加载状态
  static int getLoadStatus(bool hasError, List data) {
    if (hasError) return LoadStatus.fail;
    if (data == null) {
      // data为空，加载中
      return LoadStatus.loading;
    } else if (data.isEmpty) {
      // data为空数组，返回空
      return LoadStatus.empty;
    } else {
      //
      return LoadStatus.success;
    }
  }

  /// 字符串转列表
  /// [str] 待转字符串 如：[1,2,3,4,5]
  static List strToList(String str) {
    if (str == '') {
      str = "[]";
    }
    List list = convert.jsonDecode(str);
    print(list);
    return list;
  }

  /// 列表转字符串
  /// [list] 待转列表
  static String listToStr(List list) {
    String str = convert.jsonEncode(list);
    print(str);
    return str;
  }
}
