import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:yumi/app_localizations.dart';

class SCMDateUtils {
  static const num ONE_HOUR = 3600000;
  static const num ONE_DAY = 86400000;
  static const num ONE_WEEK = 604800000;
  static final String _ft1 = "yyyy-MM-dd";
  static final String _ft2 = "yyyy/MM/dd/ HH:mm:ss";
  static final String _ft3 = "yyyy-MM-dd HH:mm:ss";

  static String formatDateTime(DateTime date) {
    return DateFormat(_ft1).format(date);
  }

  static String formatDateTime2(DateTime date) {
    return DateFormat(_ft2).format(date);
  }

  static String formatDateTime3(DateTime date) {
    return DateFormat(_ft3).format(date);
  }

  static String formatMessageTime(BuildContext context, DateTime date) {
    var now = DateTime.now();
    num delta = now.millisecondsSinceEpoch - date.millisecondsSinceEpoch;
    //    LogUtils.log(
    //        "Delta:$delta,now.millisecondsSinceEpoch:${now.millisecondsSinceEpoch},date.millisecondsSinceEpoch:${date.millisecondsSinceEpoch}");
    if (delta < 1 * ONE_DAY) {
      if (date.day < now.day) {
        return SCAppLocalizations.of(context)!.yesterday(
          "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
        );
      } else {
        return "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}";
      }
    } else if (delta < 1 * ONE_WEEK) {
      switch (date.weekday) {
        case DateTime.monday:
          return SCAppLocalizations.of(context)!.monday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
        case DateTime.tuesday:
          return SCAppLocalizations.of(context)!.tuesday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
        case DateTime.wednesday:
          return SCAppLocalizations.of(context)!.wednesday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
        case DateTime.thursday:
          return SCAppLocalizations.of(context)!.thursday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
        case DateTime.friday:
          return SCAppLocalizations.of(context)!.friday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
        case DateTime.saturday:
          return SCAppLocalizations.of(context)!.saturday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
        case DateTime.sunday:
          return SCAppLocalizations.of(context)!.sunday(
            "${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}",
          );
      }
    } else {
      return "${date.year}/${date.month}/${date.day} ${date.hour >= 10 ? date.hour : "0${date.hour}"}:${date.minute >= 10 ? date.minute : "0${date.minute}"}";
    }
    return "";
  }

  static int calculateDaysDifference(DateTime from, DateTime to) {
    // 归一化日期（忽略时间部分，确保按整天计算）
    from = DateTime(from.year, from.month, from.day);
    to = DateTime(to.year, to.month, to.day);

    // 计算差值并返回天数
    return to.difference(from).inDays;
  }

  static String secondsToMinutes(int seconds) {
    Duration duration = Duration(seconds: seconds);
    return _df(duration);
  }

  static String millisecondsToMinutes(int milliseconds) {
    Duration duration = Duration(milliseconds: milliseconds);
    return _df(duration);
  }

  static String _df(Duration duration) {
    String twoDigits(int n) => n.toString().padLeft(2, "0");

    if (duration.inHours > 0) {
      return "${twoDigits(duration.inHours)}:${twoDigits(duration.inMinutes.remainder(60))}:${twoDigits(duration.inSeconds.remainder(60))}";
    } else {
      return "${twoDigits(duration.inMinutes.remainder(60))}:${twoDigits(duration.inSeconds.remainder(60))}";
    }
  }
}
