import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 小日历的每一格日期
class MiniDayItem extends FStatefulWidget implements FInteractiveContainer {
  const MiniDayItem({super.key, required this.dayData, required this.onSelect});
  final MiniViewDayModel dayData;
  final ValueChanged<int> onSelect;
  @override
  FState<MiniDayItem> createState() => _MyWidgetState();

  @override
  String get pageName => "MiniDayItem";
}

class _MyWidgetState extends FState<MiniDayItem> {
  late MiniViewDayModel _dayData;
  late BoxDecoration containerDecoration;
  late TextStyle dayTextStyle;
  get displayStr => _dayData.isToday
      ? i18nBook.calendar.singleToday.t
      : _dayData.date.day.toString();
  @override
  void initState() {
    super.initState();
    _dayData = widget.dayData;
    _initStyle();
  }

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

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

  @override
  FWidget build(BuildContext context) {
    return FGestureDetector(
      onTap: () {
        widget.onSelect.call(_dayData.index);
      },
      businessParent: widget,
      name: "MiniDayItem",
      child: FMouseRegion(
        cursor: SystemMouseCursors.click,
        onEnter: _handleMouthEnter,
        onExit: _handleMouthExit,
        child: FColumn(
          children: [
            FContainer(
              decoration: containerDecoration,
              height: 25,
              width: 25,
              child: FCenter(
                child: FText(displayStr, style: dayTextStyle),
              ),
            ),
            const FSizedBox(
              height: 2,
            ),
            _buildScheduleIndicator()
          ],
        ),
      ),
    );
  }

  /// 构建日程指示器
  FWidget _buildScheduleIndicator() {
    return FContainer(
      height: 4,
      width: 4,
      decoration: _dayData.hasSchedule
          ? BoxDecoration(
              color: const Color.fromARGB(255, 218, 219, 220),
              borderRadius: BorderRadius.circular(2),
            )
          : null,
    );
  }

  /// 鼠标移入时，如果未选中，背景色设置为灰色
  void _handleMouthEnter(PointerEnterEvent event) {
    if (!_dayData.isSelected) {
      setState(() {
        containerDecoration = BoxDecoration(
          color: const Color.fromARGB(255, 234, 236, 237),
          borderRadius: BorderRadius.circular(15),
        );
      });
    }
  }

  /// 鼠标移出时，如果未选中，背景色设置为透明
  void _handleMouthExit(PointerExitEvent event) {
    if (!_dayData.isSelected) {
      _initStyle();
      setState(() {});
    }
  }
}
