import 'package:intl/intl.dart';

import '../resource/assets.gen.dart';

class AgeZodiacSignUtil {
  AgeZodiacSignUtil._();

  static List<Zodiac> zodiacs = [
    Zodiac(name: '白羊座', image: Assets.images.iconAries),
    Zodiac(name: '金牛座', image: Assets.images.iconTaurus),
    Zodiac(name: '双子座', image: Assets.images.iconGemini),
    Zodiac(name: '巨蟹座', image: Assets.images.iconCancer),
    Zodiac(name: '狮子座', image: Assets.images.iconLeo),
    Zodiac(name: '处女座', image: Assets.images.iconVirgo),
    Zodiac(name: '天秤座', image: Assets.images.iconLibra),
    Zodiac(name: '天蝎座', image: Assets.images.iconScorpio),
    Zodiac(name: '射手座', image: Assets.images.iconSagittarius),
    Zodiac(name: '摩羯座', image: Assets.images.iconCapricorn),
    Zodiac(name: '水瓶座', image: Assets.images.iconAquarius),
    Zodiac(name: '双鱼座', image: Assets.images.iconPisces),
  ];

  /// 计算星座（传入 DateTime）
  /// 通过生日获取 Zodiac 对象（包含名称和图片）
  static Zodiac getZodiacSign(DateTime date) {
    int month = date.month;
    int day = date.day;

    String sign;

    if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
      sign = '白羊座';
    } else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
      sign = '金牛座';
    } else if ((month == 5 && day >= 21) || (month == 6 && day <= 20)) {
      sign = '双子座';
    } else if ((month == 6 && day >= 21) || (month == 7 && day <= 22)) {
      sign = '巨蟹座';
    } else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
      sign = '狮子座';
    } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
      sign = '处女座';
    } else if ((month == 9 && day >= 23) || (month == 10 && day <= 22)) {
      sign = '天秤座';
    } else if ((month == 10 && day >= 23) || (month == 11 && day <= 21)) {
      sign = '天蝎座';
    } else if ((month == 11 && day >= 22) || (month == 12 && day <= 21)) {
      sign = '射手座';
    } else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
      sign = '摩羯座';
    } else if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
      sign = '水瓶座';
    } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
      sign = '双鱼座';
    } else {
      return const Zodiac(name: '未知星座', image: AssetGenImage('')); // 空图片占位
    }

    return zodiacs.firstWhere(
      (zodiac) => zodiac.name == sign,
      orElse: () => const Zodiac(name: '未知星座', image: AssetGenImage('')),
    );
  }

  /// 计算年龄（传入 DateTime）
  static int calculateAge(DateTime birthDate) {
    DateTime currentDate = DateTime.now();
    int age = currentDate.year - birthDate.year;
    if (currentDate.month < birthDate.month ||
        (currentDate.month == birthDate.month &&
            currentDate.day < birthDate.day)) {
      age--;
    }
    return age;
  }

  /// 从字符串计算星座
  static Zodiac getZodiacSignFromString(
    String dateStr, {
    String format = 'yyyy-MM-dd',
  }) {
    try {
      DateTime date = DateFormat(format).parse(dateStr);
      return getZodiacSign(date);
    } catch (e) {
      return const Zodiac(name: '未知星座', image: AssetGenImage(''));
    }
  }

  /// 从字符串计算年龄
  static int? calculateAgeFromString(
    String dateStr, {
    String format = 'yyyy-MM-dd',
  }) {
    try {
      DateTime date = DateFormat(format).parse(dateStr);
      return calculateAge(date);
    } catch (e) {
      return null;
    }
  }

  /// 格式化生日（传入字符串）
  static String? formatBirthdayFromString(
    String? dateStr, {
    String outputFormat = 'yyyy-MM-dd',
  }) {
    if (dateStr == null || dateStr.isEmpty) return null;

    final possibleFormats = [
      'yyyy-MM-dd',
      'yyyy/MM/dd',
      'yyyy-MM-dd HH:mm:ss',
      'yyyy/MM/dd HH:mm:ss',
      "yyyy-MM-dd'T'HH:mm:ss'Z'",
      "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
      'yyyy-MM-dd HH:mm:sss',
    ];

    for (var format in possibleFormats) {
      try {
        DateTime date = DateFormat(format).parseStrict(dateStr);
        return DateFormat(outputFormat).format(date);
      } catch (e) {
        // 忽略失败，尝试下一个格式
      }
    }

    return null;
  }

  static Zodiac getZodiacWithGenderLabel(
      String birthStr,
      int genderCode, {
        String format = 'yyyy-MM-dd',
      }) {
    final zodiac = getZodiacSignFromString(birthStr, format: format);
    final gender = genderCode == 1 ? '男' : genderCode == 2 ? '女' : '';
    final baseName = zodiac.name.replaceAll('座', '');
    return Zodiac(name: '$baseName$gender', image: zodiac.image);
  }

}

class Zodiac {
  final String name;
  final AssetGenImage image;

  const Zodiac({required this.name, required this.image});
}

class DateComponents {
  final String yearPart;
  final String monthPart;
  final String dayPart;

  const DateComponents({
    required this.yearPart,
    required this.monthPart,
    required this.dayPart,
  });

  /// 从 DateTime 生成格式化数据
  factory DateComponents.fromDateTime(DateTime date) {
    return DateComponents(
      yearPart: DateFormat('yyyy').format(date),
      monthPart: DateFormat('MM').format(date),
      dayPart: DateFormat('dd').format(date),
    );
  }

  static String getZodiacLabel(Zodiac zodiac, String gender) {
    final name = zodiac.name.replaceAll('座', '');
    return '$name$gender'; // gender: '男' 或 '女'
  }

  String toFormattedString() => '$yearPart年$monthPart月$dayPart日';
}
