import 'package:fis_common/event/event_type.dart';

/// 格式化的时间差
class FmtedDuration {
  FmtedDuration();

  /// 计算多少岁，多少月
  static List<int> countDurYearAndMonth(DateTime start) {
    var end = DateTime.now();
    int dayDur = end.day - start.day;
    int monthDur = end.month - start.month;
    int yearDur = end.year - start.year;
    if (dayDur < 0) {
      monthDur -= 1;
      dayDur += 30;
    }
    if (monthDur < 0) {
      yearDur -= 1;
      monthDur += 12;
    }
    return [yearDur, monthDur];
  }

  /// 计算多少月，多少周
  static List<int> countDurMonthAndWeek(DateTime start) {
    var end = DateTime.now();
    int dayDur = end.difference(start).inDays;
    int monthDur = dayDur ~/ 30;
    int weekDur = dayDur % 30 ~/ 7;
    return [monthDur, weekDur];
  }

  /// 计算多少周，多少天
  static List<int> countDurWeekAndDay(DateTime start) {
    var end = DateTime.now();
    int allDayDur = end.difference(start).inDays;

    int weekDur = allDayDur ~/ 7;
    int dayDur = allDayDur % 7;
    return [weekDur, dayDur];
  }

  /// 计算相隔多少岁月的日期
  static String countDurYearAndMonthBirthDay(
      [String year = '', String month = '']) {
    var end = DateTime.now();
    int dayDur = end.day;
    int monthDur = end.month - (month.isEmpty ? 0 : int.parse(month));
    int yearDur = end.year - (year.isEmpty ? 0 : int.parse(year));
    if (monthDur < 0) {
      yearDur -= 1;
      monthDur += 12;
    }
    String birthDay =
        DateTime.utc(yearDur, monthDur, dayDur).toString().split(' ')[0];
    return birthDay;
  }

  /// 计算相隔多少月周的日期
  static String countDurMonthAndWeekBirthDay(
      [String month = '', String week = '']) {
    var end = DateTime.now();
    var days = int.parse(month.isEmpty ? '0' : month) * 30 +
        int.parse(week.isEmpty ? '0' : week) * 7;
    Duration time = new Duration(days: -days);
    String birthDay = end.add(time).toString().split(' ')[0];
    return birthDay;
  }

  /// 计算相隔多少周天的日期
  static String countDurWeekAndDayBirthDay(
      [String week = '', String day = '']) {
    print('week: ' + week);
    print('day: ' + day);
    var end = DateTime.now();
    var days = int.parse(week.isEmpty ? '0' : week) * 7 +
        int.parse(day.isEmpty ? '0' : day);
    Duration time = new Duration(days: -days);
    String birthDay = end.add(time).toString().split(' ')[0];
    return birthDay;
  }

  bool _isChangeTime = false;
  bool _isChangeBirth = false;
  var isChangeTimeChanged = FEventHandler<bool>();
  var isChangeBirthChanged = FEventHandler<bool>();

  /// 为了更新ageType
  bool get isChangeTime => _isChangeTime;
  bool get isChangeBirth => _isChangeBirth;

  set isChangeBirth(bool value) {
    if (value != _isChangeBirth) {
      _isChangeBirth = value;
      _onBirthChanged();
    }
  }

  set isChangeTime(bool value) {
    if (value != _isChangeTime) {
      _isChangeTime = value;
      _onAgeYearChanged();
    }
  }

  void _onAgeYearChanged() {
    isChangeTimeChanged.emit(this, isChangeTime);
  }

  void _onBirthChanged() {
    isChangeBirthChanged.emit(this, isChangeBirth);
  }
}
