import 'package:fis_ui/index.dart';
import 'package:flyinsono/pages/schedule/calendar/popup_layer/popup_layer_controller.dart';
import 'package:flyinsono/pages/schedule/calendar/models.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:get/get.dart';

class SchedualItem extends FStatefulWidget {
  const SchedualItem({
    super.key,
    required this.scheduleData,
    required this.itemHeight,
  });
  final ScheduleModel scheduleData;
  final double itemHeight;

  @override
  FState<SchedualItem> createState() => _SchedualItemState();
}

class _SchedualItemState extends FState<SchedualItem> {
  ScheduleType get scheduleType => widget.scheduleData.type;
  ScheduleModel get schedule => widget.scheduleData;
  bool _isSelected = false; // 是否选中状态
  GlobalKey _scheduleListKey = GlobalKey();

  final PopupLayerController _popupLayerController =
      Get.find<PopupLayerController>();

  @override
  void initState() {
    super.initState();

    _popupLayerController.onCloseSchedulePopup.addListener(_onGlobalClick);
  }

  @override
  void dispose() {
    super.dispose();
    _popupLayerController.onCloseSchedulePopup.removeListener(_onGlobalClick);
  }

  void _onGlobalClick(e, _) {
    setState(() {
      _isSelected = false;
    });
  }

  ////TODO:[Gavin] 监听一个popupController的关闭事件，关闭时将选中状态置为false
  @override
  FWidget build(BuildContext context) {
    return FMaterial(
      color: Colors.transparent,
      child: FInkWell(
        key: _scheduleListKey,
        customBorder: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
        ),
        onTap: () {
          setState(() {
            _isSelected = !_isSelected;
          });
          _popupLayerController.handlePopupScheduleDetail(
              _scheduleListKey, schedule);
        },
        hoverColor: const Color.fromARGB(255, 227, 228, 228),
        child: Container(
          height: widget.itemHeight,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          decoration: BoxDecoration(
            color: _isSelected ? scheduleType.color : Colors.transparent,
            borderRadius: BorderRadius.circular(5),
          ),
          child: Row(
            children: [
              Container(
                width: 6,
                height: 6,
                decoration: BoxDecoration(
                  color: scheduleType.color,
                  borderRadius: BorderRadius.circular(5),
                ),
              ),
              const SizedBox(width: 5),
              Text(
                CalendarUtils.getStartTime(schedule.startTime),
                style: TextStyle(
                  fontSize: 12,
                  color: _isSelected
                      ? Colors.white
                      : const Color.fromARGB(167, 0, 0, 0),
                ),
                overflow: TextOverflow.ellipsis,
              ),
              const SizedBox(width: 5),
              Expanded(
                child: Text(
                  schedule.title.breakWord(),
                  style: TextStyle(
                      fontSize: 12,
                      color: _isSelected ? Colors.white : Colors.black),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
