import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'log_utils.dart';
import 'package:intl/intl.dart';

class Utils {
  /// 是否字符串为空
  static bool isEmpty(String? value) {
    if (value == null) {
      return true;
    }
    return value.isEmpty || value == 'null';
  }

  /// 字符串是否不为空
  static bool isNotEmpty(String? value) {
    return !isEmpty(value);
  }

  /// 判断邮箱
  static isEmail(dynamic value, {String? country = "sg"}) {
    switch (country) {
      case "sg":
        RegExp regExp = RegExp(r"^\w{1,}(.\w+)*@[A-z0-9]+(\.[A-z]{2,6}){1,2}$");
        if (regExp.hasMatch(value.toString())) {
          return true;
        } else {
          return false;
        }
      default:
        return false;
    }
  }

  /// 判断中国的11位手机号码
  static bool isChinaPhoneNumber(dynamic value) {
    RegExp regExp = RegExp(r"^1\d{10}$"); // 匹配以1开头的11位数字
    if (regExp.hasMatch(value.toString())) {
      return true;
    } else {
      return false;
    }
  }

  /// 全局的路由key
  static final navKey = GlobalKey<NavigatorState>();

  /// 全局的组件key
  static final widgetKey = GlobalKey<State>();

  //页面缓存key
  static dynamic pageStorageKey(dynamic keyname) {
    return PageStorageKey<dynamic>(keyname);
  }

  /// 关闭软键盘
  static void hideKeyboard(BuildContext context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }
  }

  /// map 转 json 字符串
  static String encodeMap(Map<String, dynamic> mapData) {
    return json.encode(mapData);
  }

  /// json 字符串转 map
  static Map<String, dynamic> decodeJson(jsonStr) {
    return json.decode(jsonStr.toString());
  }

  /// 判断颜色是否是暗色还是亮色
  static num getComputeLuminance(Color color) {
    return color.computeLuminance();
  }

  /// 随机获取指定返回内的数值
  static int getRandomRangeInt(int min, int max) {
    final Random random = Random();
    return min + random.nextInt(max + 1 - min);
  }

  /// 无参数的防抖函数
  static VoidCallback debounce(
    VoidCallback func, [
    Duration delay = const Duration(milliseconds: 400),
  ]) {
    late Timer timer;
    return () {
      if (timer.isActive == true) {
        timer.cancel();
      }
      timer = Timer(delay, () {
        func.call();
      });
    };
  }

  /// 带参数的防抖函数
  static Function debounceArg<T>(
    Function func, [
    Duration delay = const Duration(milliseconds: 2000),
  ]) {
    late Timer timer;
    return (T arg) {
      if (timer.isActive == true) {
        timer.cancel();
      }
      timer = Timer(delay, () {
        func.call(arg);
      });
    };
  }

  /// 将时间转化为 多久之前
  static String getTimeAgo(String? dateTimeStr,{ String? format}) {
    if (dateTimeStr == null || dateTimeStr.isEmpty) {
      return 'Unknown time'; // 或者返回其他默认值
    }

    DateTime? dateTime;
    try {
      if (format == null){
        dateTime = DateTime.parse(dateTimeStr);
      }else{
        //如果有指定的格式可以按照这个格式来解析时间
        DateFormat dateFormat = DateFormat(format, 'en_US');
        dateTime = dateFormat.parse(dateTimeStr);
        Log.d("dateTime:$dateTime");
      }
    } catch (e) {
      Log.e("Error parsing date: $e");
      return 'Invalid date format'; // 或者返回其他默认值
    }

    if (dateTime == null) {
      return 'Unknown time'; // 或者返回其他默认值
    }

    final now = DateTime.now();
    final difference = now.difference(dateTime);

    if (difference.inHours < 1) {
      return "${difference.inMinutes} minutes ago";
    } else if (difference.inHours < 24) {
      return "${difference.inHours} hours ago";
    } else if (difference.inDays < 7) {
      return "${difference.inDays} days ago";
    } else if (difference.inDays < 30) {
      return "${(difference.inDays / 7).floor()} weeks ago";
    } else if (difference.inDays < 365) {
      return "${(difference.inDays / 30).floor()} months ago";
    } else {
      return "${(difference.inDays / 365).floor()} years ago";
    }
  }


  /// 从 Url 中找到对应的宽高
  static List<double>? extractWidthHeight(String? url) {
    if (isEmpty(url)) return null;

    // 使用正则表达式来查找形如 "宽x高" 的部分
    final RegExp regex = RegExp(r'-(\d+)x(\d+)\.');
    final matches = regex.allMatches(url!);

    if (matches.isNotEmpty) {
      // 获取最后一个匹配项
      final lastMatch = matches.last;
      final width = double.parse(lastMatch.group(1)!);
      final height = double.parse(lastMatch.group(2)!);
      return [width, height]; // 返回宽高列表
    }

    return null; // 未找到宽高
  }

  /// 将字符串的前三位和后两位显示，中间用 * 替代。
  static String maskString(String input) {
    // 检查字符串长度
    if (input.length <= 5) {
      // 如果字符串长度小于等于5，直接返回原字符串
      return input;
    }

    // 获取前三位和后两位
    String start = input.substring(0, 3);
    String end = input.substring(input.length - 2);

    // 计算中间需要的 * 的数量
    int asterisksCount = input.length - 5; // 计算需要的 * 的数量
    String asterisks = '*' * asterisksCount; // 创建 * 字符串

    // 拼接成新的字符串
    return '$start$asterisks$end';
  }

  /// 获取实际手机号码
  static String getRealMobile(String mobile) {
    mobile = mobile.trim();

    // 判断属于哪一个国家区号
    if (mobile.startsWith("+1-")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+1")) {
      return mobile.substring(2);
    } else if (mobile.startsWith("+31-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+31")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+33-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+33")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+34-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+34")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+39-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+39")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+44-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+44")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+49-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+49")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+60-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+60")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+61-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+61")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+62-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+62")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+63-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+63")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+65-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+65")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+66-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+66")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+81-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+81")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+82-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+82")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+84-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+84")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+86-")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+86")) {
      return mobile.substring(3);
    } else if (mobile.startsWith("+852-")) {
      return mobile.substring(5);
    } else if (mobile.startsWith("+852")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+855-")) {
      return mobile.substring(5);
    } else if (mobile.startsWith("+855")) {
      return mobile.substring(4);
    } else if (mobile.startsWith("+971-")) {
      return mobile.substring(5);
    } else if (mobile.startsWith("+971")) {
      return mobile.substring(4);
    } else {
      return mobile; // 返回原始手机号
    }
  }

  /// 获取国家区号
  static String getMobileCode(String mobile) {
    mobile = mobile.trim();

    // 判断属于哪一个国家区号
    if (mobile.startsWith("1") || mobile.startsWith("+1") || mobile.startsWith("1-") || mobile.startsWith("+1-")) {
      return "+1";
    } else if (mobile.startsWith("31") || mobile.startsWith("+31") || mobile.startsWith("31-") || mobile.startsWith("+31-")) {
      return "+31";
    } else if (mobile.startsWith("33") || mobile.startsWith("+33") || mobile.startsWith("33-") || mobile.startsWith("+33-")) {
      return "+33";
    } else if (mobile.startsWith("34") || mobile.startsWith("+34") || mobile.startsWith("34-") || mobile.startsWith("+34-")) {
      return "+34";
    } else if (mobile.startsWith("39") || mobile.startsWith("+39") || mobile.startsWith("39-") || mobile.startsWith("+39-")) {
      return "+39";
    } else if (mobile.startsWith("44") || mobile.startsWith("+44") || mobile.startsWith("44-") || mobile.startsWith("+44-")) {
      return "+44";
    } else if (mobile.startsWith("49") || mobile.startsWith("+49") || mobile.startsWith("49-") || mobile.startsWith("+49-")) {
      return "+49";
    } else if (mobile.startsWith("60") || mobile.startsWith("+60") || mobile.startsWith("60-") || mobile.startsWith("+60-")) {
      return "+60";
    } else if (mobile.startsWith("61") || mobile.startsWith("+61") || mobile.startsWith("61-") || mobile.startsWith("+61-")) {
      return "+61";
    } else if (mobile.startsWith("62") || mobile.startsWith("+62") || mobile.startsWith("62-") || mobile.startsWith("+62-")) {
      return "+62";
    } else if (mobile.startsWith("63") || mobile.startsWith("+63") || mobile.startsWith("63-") || mobile.startsWith("+63-")) {
      return "+63";
    } else if (mobile.startsWith("65") || mobile.startsWith("+65") || mobile.startsWith("65-") || mobile.startsWith("+65-")) {
      return "+65";
    } else if (mobile.startsWith("66") || mobile.startsWith("+66") || mobile.startsWith("66-") || mobile.startsWith("+66-")) {
      return "+66";
    } else if (mobile.startsWith("81") || mobile.startsWith("+81") || mobile.startsWith("81-") || mobile.startsWith("+81-")) {
      return "+81";
    } else if (mobile.startsWith("82") || mobile.startsWith("+82") || mobile.startsWith("82-") || mobile.startsWith("+82-")) {
      return "+82";
    } else if (mobile.startsWith("84") || mobile.startsWith("+84") || mobile.startsWith("84-") || mobile.startsWith("+84-")) {
      return "+84";
    } else if (mobile.startsWith("86") || mobile.startsWith("+86") || mobile.startsWith("86-") || mobile.startsWith("+86-")) {
      return "+86";
    } else if (mobile.startsWith("852") || mobile.startsWith("+852") || mobile.startsWith("852-") || mobile.startsWith("+852-")) {
      return "+852";
    } else if (mobile.startsWith("855") || mobile.startsWith("+855") || mobile.startsWith("855-") || mobile.startsWith("+855-")) {
      return "+855";
    } else if (mobile.startsWith("971") || mobile.startsWith("+971") || mobile.startsWith("971-") || mobile.startsWith("+971-")) {
      return "+971";
    } else {
      return ""; // 如果没有匹配的区号，返回空字符串
    }
  }

}
