import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';

class DataGridItem extends StatefulWidget {
  const DataGridItem({
    super.key,
    required this.projectDataName,
    required this.projectDataId,
    required this.onClick,
    this.selectable = false,
    this.selected = false,
    this.highlighted = false,
    this.onCheckBoxChanged,
    required this.projectDataFileSize,
    this.isExistUrmData = false,
  });
  final String projectDataName;
  final String projectDataId;
  final String projectDataFileSize;
  final VoidCallback onClick;
  final bool selectable; // 是否可选中
  final bool selected; // 是否选中
  final bool highlighted; // 是否高亮
  final bool isExistUrmData;
  final ValueChanged<bool?>? onCheckBoxChanged; // 选中状态改变

  @override
  State<DataGridItem> createState() => _DataGridItemState();
}

class _DataGridItemState extends State<DataGridItem> {
  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 = _isHover ? LabColors.base300 : Colors.transparent;
    if (widget.selected || widget.highlighted) {
      _color = LabColors.base300;
    }
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: _handleMouseEnter,
      onExit: _handleMouseExit,
      child: GestureDetector(
        onTap: () {
          widget.onClick.call();
        },
        child: Container(
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: _color,
            borderRadius: BorderRadius.circular(5),
            border: Border.all(
              color: LabColors.base500,
              width: 1,
            ),
          ),
          child: Column(
            children: [
              Row(
                children: [
                  Text(
                    widget.projectDataName.breakWord(),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 18, color: LabColors.text700),
                  ),
                  Expanded(child: Container()),
                  if (widget.isExistUrmData)
                    Container(
                      decoration: LabBoxDecoration.base,
                      child: Text(
                        'URM',
                        style: TextStyle(
                          color: LabColors.green400,
                          fontWeight: FontWeight.bold,
                          fontSize: 12,
                        ),
                      ),
                    ),
                  if (widget.selectable)
                    Container(
                      height: 25,
                      width: 25,
                      child: Center(
                        child: Checkbox(
                          checkColor: LabColors.base100,
                          fillColor:
                              MaterialStateProperty.resolveWith(getFillColor),
                          value: widget.selected,
                          onChanged: widget.onCheckBoxChanged,
                        ),
                      ),
                    ),
                ],
              ),
              Expanded(child: Container()),
              Row(
                children: [
                  Text(
                    widget.projectDataId,
                    style: TextStyle(fontSize: 14, color: LabColors.text700),
                  ),
                  Expanded(child: Container()),
                  Text(
                    widget.projectDataFileSize,
                    style: TextStyle(fontSize: 14, color: LabColors.text700),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

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

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