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

class DataListItem extends StatefulWidget {
  const DataListItem({
    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<DataListItem> createState() => _DataListItemState();
}

class _DataListItemState extends State<DataListItem> {
  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 _bgColor = _isHover ? LabColors.base300 : Colors.transparent;
    if (widget.selected || widget.highlighted) {
      _bgColor = LabColors.base300;
    }
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: _handleMouseEnter,
      onExit: _handleMouseExit,
      child: GestureDetector(
        onTap: () {
          widget.onClick.call();
        },
        child: Container(
          height: 40,
          margin: EdgeInsets.only(right: 10),
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: _bgColor,
            // borderRadius: BorderRadius.circular(5),
            border: Border(
              bottom: BorderSide(
                color: LabColors.base500,
                width: 1,
              ),
            ),
          ),
          child: Row(
            children: [
              Flexible(
                flex: 1,
                fit: FlexFit.tight,
                child: Text(
                  widget.projectDataId,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 14, color: LabColors.text700),
                ),
              ),
              Flexible(
                flex: 3,
                fit: FlexFit.tight,
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      Text(
                        widget.projectDataName.breakWord(),
                        style:
                            TextStyle(fontSize: 14, color: LabColors.text700),
                      ),
                      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,
                            ),
                          ),
                        ),
                    ],
                  ),
                ),
              ),
              Flexible(
                flex: 1,
                fit: FlexFit.tight,
                child: Align(
                  alignment: Alignment.centerRight,
                  child: Text(
                    widget.projectDataFileSize,
                    style: TextStyle(fontSize: 14, color: LabColors.text700),
                  ),
                ),
              ),
              if (widget.selectable)
                Container(
                  width: 60,
                  margin: EdgeInsets.only(left: 5),
                  child: Center(
                    child: Checkbox(
                      checkColor: LabColors.base100,
                      fillColor:
                          MaterialStateProperty.resolveWith(getFillColor),
                      value: widget.selected,
                      onChanged: widget.onCheckBoxChanged,
                    ),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }

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

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

class DataListTitle extends StatelessWidget {
  const DataListTitle({
    super.key,
    required this.title,
    this.selectable = false,
  });
  final String title;
  final bool selectable;

  // 字体样式
  TextStyle get _titileTextStyle => TextStyle(
        fontSize: 14,
        color: LabColors.text600,
      );

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 30,
      margin: EdgeInsets.only(right: 10),
      padding: EdgeInsets.only(left: 5, right: 5),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: LabColors.base500,
            width: 1,
          ),
        ),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Flexible(
            flex: 1,
            fit: FlexFit.tight,
            child: Text(
              "ID",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: _titileTextStyle,
            ),
          ),
          Flexible(
            flex: 3,
            fit: FlexFit.tight,
            child: Align(
              alignment: Alignment.centerLeft,
              child: Text(
                title,
                style: _titileTextStyle,
              ),
            ),
          ),
          Flexible(
            flex: 1,
            fit: FlexFit.tight,
            child: Align(
              alignment: Alignment.centerRight,
              child: Text(
                "大小",
                style: _titileTextStyle,
              ),
            ),
          ),
          if (selectable)
            Container(
              width: 60,
              margin: EdgeInsets.only(left: 5),
              child: Center(
                  // child: Text(
                  //   "选择",
                  //   style: TextStyle(
                  //     fontSize: 16,
                  //     color: LabColors.base700,
                  //     fontWeight: FontWeight.bold,
                  //   ),
                  // ),
                  ),
            ),
        ],
      ),
    );
  }
}
