import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flutter/material.dart';
import 'package:fis_theme/theme.dart';
import 'package:flyinsono/lab/components/lab_overflow_tooltip_wrapper.dart';
import 'package:flyinsono/lab/components/lab_text_tool_tip.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/pages/lab_export/controller.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:get/get.dart';

class ClassifyMultipleListItem extends StatefulWidget {
  const ClassifyMultipleListItem({
    super.key,
    required this.projectName,
    required this.onClick,
    required this.isSelected,
    required this.multipleCheckList,
    required this.index,
    required this.id,
    required this.onMultipleItemClick,
    required this.project,
  });
  final String projectName;
  final VoidCallback onClick;
  final Function(bool? selected) onMultipleItemClick;
  final bool isSelected;
  final List<MultipleCheckModel> multipleCheckList;
  final int index;
  final String id;
  final PatientModel project;

  @override
  State<ClassifyMultipleListItem> createState() =>
      _ClassifyMultipleListItemState();
}

class _ClassifyMultipleListItemState extends State<ClassifyMultipleListItem> {
  bool _isHover = false;

  Color getFillColor(Set<MaterialState> states) {
    const Set<MaterialState> interactiveStates = <MaterialState>{
      MaterialState.pressed,
      MaterialState.hovered,
      MaterialState.focused,
    };
    if (states.any(interactiveStates.contains)) {
      return LabColors.base600;
    }
    return LabColors.base700;
  }

  @override
  Widget build(BuildContext context) {
    Color _color = widget.isSelected ? LabColors.base400 : Colors.transparent;
    if (_isHover && !widget.isSelected) {
      _color = LabColors.base300;
    }

    MultipleCheckModel? multipleCheckModel =
        widget.multipleCheckList.firstWhereOrNull(
      (element) => element.checkCode == widget.id,
    );
    String allCheck = (multipleCheckModel?.allCheck ?? 0).toString();
    String checkedCheck = (multipleCheckModel?.checkedCheck ?? 0).toString();
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: _handleMouseEnter,
      onExit: _handleMouseExit,
      child: GestureDetector(
        onTap: () {
          widget.onClick.call();
        },
        child: Container(
          height: 40,
          padding:
              const EdgeInsets.symmetric(horizontal: 25).copyWith(right: 0),
          decoration: BoxDecoration(
            color: _color,
            borderRadius: BorderRadius.circular(5),
          ),
          child: Row(
            children: [
              Expanded(
                child: LabOverflowTooltipWrapper(
                  position: DisplayPosition.right,
                  offset: Offset.zero,
                  child: Text(
                    widget.projectName.breakWord(),
                    maxLines: 1,
                    style: TextStyle(
                      color: LabColors.text700,
                      overflow: TextOverflow.ellipsis,
                      fontFamily: FTheme.ins.localeSetting.fontFamily,
                      fontFamilyFallback: [
                        FTheme.ins.data.localeSettings.chinese.fontFamily,
                      ],
                    ),
                  ),
                ),
              ),
              Text(
                '$checkedCheck/$allCheck',
                style: TextStyle(
                  color: LabColors.text600,
                ),
              ),
              const SizedBox(width: 10),
              Checkbox(
                checkColor: LabColors.base100,
                fillColor: MaterialStateProperty.resolveWith(getFillColor),
                value: widget.multipleCheckList.any(
                  (element) {
                    return element.checkCode == widget.id &&
                        element.checkedCheck != 0;
                  },
                ),
                onChanged: (selected) {
                  widget.onMultipleItemClick(selected);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

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

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