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/style/lab_box_decoration.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';

class ClassifyListItem extends StatefulWidget {
  const ClassifyListItem({
    super.key,
    required this.projectName,
    required this.onClick,
    required this.isSelected,
    this.itemActionButton,
    this.isExistUrmData = false,
  });
  final String projectName;
  final VoidCallback onClick;
  final bool isSelected;
  final bool isExistUrmData;
  final Widget Function()? itemActionButton;

  @override
  State<ClassifyListItem> createState() => _ClassifyListItemState();
}

class _ClassifyListItemState extends State<ClassifyListItem> {
  bool _isHover = false;

  @override
  Widget build(BuildContext context) {
    Color _color = widget.isSelected ? LabColors.base400 : Colors.transparent;
    if (_isHover && !widget.isSelected) {
      _color = LabColors.base300;
    }
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: _handleMouseEnter,
      onExit: _handleMouseExit,
      child: GestureDetector(
        onTap: () {
          widget.onClick.call();
        },
        child: Container(
          height: 40,
          padding: const EdgeInsets.symmetric(horizontal: 20),
          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,
                      ],
                    ),
                  ),
                ),
              ),
              if (widget.isExistUrmData)
                SizedBox(
                  width: 10,
                ),
              if (widget.isExistUrmData)
                Container(
                  decoration: LabBoxDecoration.base,
                  child: Text(
                    'URM',
                    style: TextStyle(
                      color: LabColors.green400,
                      fontWeight: FontWeight.bold,
                      fontSize: 12,
                    ),
                  ),
                ),
              if (widget.isSelected && widget.itemActionButton != null)
                widget.itemActionButton!()
            ],
          ),
        ),
      ),
    );
  }

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

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