import 'package:cs_resources/theme/app_colors_theme.dart';
import 'package:flutter/material.dart';
import 'package:shared/utils/date_time_utils.dart';
import 'calendar_utils.dart';

class DayCell extends StatelessWidget {
  const DayCell({
    super.key,
    required this.display,
    required this.selected,
    required this.maxDate,
    required this.current,
  });

  final DateTime display;
  final DateTime selected;
  final DateTime maxDate;
  final DateTime current;

  //显示的日期文本
  String get dayText {
    return DateTimeUtils.formatDate(display, format: 'dd');
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: const Duration(milliseconds: 300),
      width: 40,
      height: 40,
      decoration: BoxDecoration(
        color: isPastDate(current, display) || isAfterMaxDate(maxDate, display)
            ? context.appColors.disEnableGray.withOpacity(0.6) // 小于今天的日期背景颜色和大于Max日期变灰并禁用
            : _isSelected(display)
                ? _isToday(display)
                    ? context.appColors.btnBgDefault // 今天的日期背景颜色
                    : context.appColors.btnBgDefault // 选中的日期背景颜色
                : context.appColors.grayBgE9, // 默认的背景颜色
        shape: BoxShape.rectangle,
        borderRadius: BorderRadius.circular(2.5), // 圆角
      ),
      alignment: Alignment.center,
      child: Text(
        dayText,
        style: TextStyle(
          fontSize: 17,
          fontWeight: FontWeight.w500,
          color: _dayTextColor(context, display),
        ),
      ),
    );
  }

  Color _dayTextColor(BuildContext context, DateTime date) {
    if (_isSelected(date)) {
      return Colors.white;
    }

    if (_isToday(date)) {
      return context.appColors.textBlack;
    }

    if (_isWeekend(date)) {
      return context.appColors.textBlack;
    }

    return context.appColors.textBlack;
  }

  //是否是周末
  bool _isWeekend(DateTime date) {
    return date.weekday == DateTime.saturday || date.weekday == DateTime.sunday;
  }

  //是否是选中的
  bool _isSelected(DateTime date) {
    return DateTimeUtils.formatDate(date, format: 'yyyyMMdd') == DateTimeUtils.formatDate(selected, format: 'yyyyMMdd');
  }

  //是否是今天
  bool _isToday(DateTime date) {
    return DateTimeUtils.formatDate(date, format: 'yyyyMMdd') == DateTimeUtils.formatDate(current, format: 'yyyyMMdd');
  }
}
