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/controller.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flyinsono/pages/schedule/calendar/calendar_page/my_calendar/my_schedule_item.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

/// 我的日历模块，是个ListView，可以展开收起
class MyCalendar extends FStatefulWidget implements FInteractiveContainer {
  const MyCalendar({super.key});

  @override
  FState<MyCalendar> createState() => _MyCalendarState();

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

class _MyCalendarState extends FState<MyCalendar> {
  bool _isExpanded = true;
  bool _isTitleHover = false;
  List<ScheduleType> _scheduleTypeList = [];
  CalendarController calendarController = Get.find<CalendarController>();

  @override
  void initState() {
    super.initState();
    _scheduleTypeList = calendarController.scheduleTypeList;
    calendarController.onDaysListChange.addListener(_onDaysListChange);
  }

  @override
  void dispose() {
    super.dispose();
    calendarController.onDaysListChange.removeListener(_onDaysListChange);
  }

  void _onDaysListChange(_, e) {
    setState(() {
      _scheduleTypeList = calendarController.scheduleTypeList;
    });
  }

  @override
  FWidget build(BuildContext context) {
    return FListView(
      shrinkWrap: true,
      children: <FWidget>[
        _buildMyCalendarTitle(),
        if (_isExpanded) ..._scheduleTypeList.map(_buildMyCalendarItem),
      ],
    );
  }

  FWidget _buildMyCalendarTitle() {
    return FMouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: _handleMouseEnter,
      onExit: _handleMouseExit,
      child: FGestureDetector(
        name: "myCalendarTitle",
        businessParent: widget,
        onTap: () {
          setState(() {
            _isExpanded = !_isExpanded;
          });
        },
        child: FContainer(
          height: 40,
          decoration: BoxDecoration(
            color: _isTitleHover
                ? const Color.fromARGB(255, 233, 235, 236)
                : Colors.transparent,
            borderRadius: BorderRadius.circular(5),
          ),
          padding: const EdgeInsets.symmetric(horizontal: 10),
          // color: const Color.fromARGB(90, 59, 255, 59),
          child: FRow(
            children: [
              FCenter(
                child: FText(
                  i18nBook.realTimeConsultation.appointmentConsultation.t,
                  style: TextStyle(
                    color: Color.fromARGB(255, 116, 118, 119),
                  ),
                ),
              ),
              FExpanded(child: FContainer()),

              /// TODO:[Gavin] 占位功能区，暂时屏蔽，待实现
              // const FCenter(
              //   child: FIcon(
              //     Icons.more_horiz,
              //     size: 20,
              //     color: Color.fromARGB(255, 116, 118, 119),
              //   ),
              // ),
              const FSizedBox(width: 10),
              FCenter(
                child: FIcon(
                  _isExpanded ? Icons.expand_less : Icons.expand_more,
                  size: 20,
                  color: const Color.fromARGB(255, 116, 118, 119),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  FWidget _buildMyCalendarItem(ScheduleType type) {
    return MyScheduleItem(
      type: type,
      onClick: () => calendarController.updateScheduleTypeList(),
    );
  }

  void _handleMouseEnter(PointerEvent details) {
    setState(() {
      _isTitleHover = true;
    });
  }

  void _handleMouseExit(PointerEvent details) {
    setState(() {
      _isTitleHover = false;
    });
  }
}
