import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:flyinsono/pages/schedule/calendar/controller.dart';
import 'package:flyinsono/pages/schedule/calendar/popup_layer/popup_layer_controller.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:flyinsono/pages/schedule/calendar/calendar_page/month_calendar/schedule_list.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

/// 月历的每一格日期
class MonthDayItem extends FStatefulWidget {
  const MonthDayItem(
      {super.key,
      required this.dayData,
      // required this.onSelect,
      required this.maxScheduleLines});
  final MonthViewDayModel dayData;
  // final ValueChanged<int> onSelect;
  final int maxScheduleLines;

  /// 最大能容纳的日程条数
  @override
  FState<MonthDayItem> createState() => _MyWidgetState();
}

class _MyWidgetState extends FState<MonthDayItem> {
  late MonthViewDayModel _dayData;
  late BoxDecoration _dateContainerDecoration;
  CalendarController calendarController = Get.find<CalendarController>();
  PopupLayerController popupLayerController = Get.find<PopupLayerController>();
  BoxDecoration _gridContainerDecoration = const BoxDecoration(
    color: Colors.white,
  );
  late TextStyle _dayTextStyle;
  late TextStyle _lunarDayTextStyle;
  final GlobalKey _monthGridKey = GlobalKey();
  get displayStr => _dayData.isToday
      ? i18nBook.calendar.singleToday.t
      : _dayData.date.day.toString();
  @override
  void initState() {
    super.initState();
    _dayData = widget.dayData;
    _initStyle();
  }

  @override
  void didUpdateWidget(MonthDayItem oldWidget) {
    super.didUpdateWidget(oldWidget);
    _dayData = widget.dayData;
    _initStyle();
  }

  /// 初始化样式
  /// 存在三种背景样式，选中，未选中，“今日”未选中
  /// 存在四种文字样式，选中，未选中，非当月，“今日”未选中
  void _initStyle() {
    if (_dayData.isSelected) {
      _dayTextStyle = TextStyle(
        height: _dayData.isToday ? null : 1,
        fontSize: 14,
        color: Colors.white,
      );
      _dateContainerDecoration = BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(15),
      );
      _gridContainerDecoration = const BoxDecoration(
        color: Color.fromARGB(255, 226, 241, 254),
      );
      _lunarDayTextStyle = _dayTextStyle.copyWith(
        fontSize: 11,
        color: Colors.blue,
      );
    } else {
      if (_dayData.isToday) {
        _dayTextStyle = const TextStyle(
          height: null,
          fontSize: 14,
          color: Colors.blue,
        );
        _dateContainerDecoration = BoxDecoration(
          color: const Color.fromARGB(255, 224, 241, 255),
          borderRadius: BorderRadius.circular(15),
        );
        _gridContainerDecoration = const BoxDecoration(
          color: Color.fromARGB(255, 248, 248, 248),
        );
        _lunarDayTextStyle = _dayTextStyle.copyWith(
          color: Colors.black87,
          fontSize: 11,
        );
      } else {
        _dayTextStyle = TextStyle(
          height: _dayData.isToday ? null : 1,
          fontSize: 14,
          color: _dayData.isCurrentMonth ? Colors.black87 : Colors.black26,
        );
        _dateContainerDecoration = const BoxDecoration(
          color: Colors.transparent,
        );
        _gridContainerDecoration = const BoxDecoration(
          color: Colors.white,
        );
        _lunarDayTextStyle = _dayTextStyle.copyWith(
          fontSize: 11,
        );
      }
    }
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      key: _monthGridKey,
      decoration: _gridContainerDecoration,
      child: FColumn(
        children: [
          FRow(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              FContainer(
                decoration: _dateContainerDecoration,
                margin: const EdgeInsets.symmetric(vertical: 3),
                height: 24,
                width: 24,
                child: FCenter(
                  child: FText(displayStr, style: _dayTextStyle),
                ),
              ),
              if (i18nBook.isCurrentChinese)
                FContainer(
                  // decoration: _dateContainerDecoration,
                  margin: const EdgeInsets.all(3),
                  height: 24,
                  child: FCenter(
                    child: FText(CalendarUtils.getLunarDay(_dayData.date),
                        style: _lunarDayTextStyle),
                  ),
                ),
            ],
          ),
          _buildScheduleContainer()
        ],
      ),
    );
  }

  /// 构建日程容器
  FWidget _buildScheduleContainer() {
    return FExpanded(
      // ignore: sized_box_for_whitespace
      child: FContainer(
        width: double.infinity,
        child: ScheduleList(
          maxLines: widget.maxScheduleLines,
          scheduleDataList: calendarController
              .scheduleListFilter(widget.dayData.scheduleList),
          onTapShowMore: () {
            popupLayerController.handlePopupMoreSchedule(
              _monthGridKey,
              widget.dayData,
            );
          },
        ),
      ),
    );
  }
}
