import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/components/operate_button/more_operate_menu.dart';
import 'package:flyinsono/lab/components/operate_button/more_operate_button.dart';

class OperateButton extends StatefulWidget {
  const OperateButton({
    super.key,
    required this.operateName,
    required this.onClick,
    this.iconData = Icons.menu,
    this.moreOperateButtons = const [],
  });
  final String operateName;
  final VoidCallback onClick;
  final List<MoreOperateButton> moreOperateButtons;
  final IconData iconData;

  @override
  State<OperateButton> createState() => _OperateButtonState();
}

class _OperateButtonState extends State<OperateButton> {
  bool _isHover = false;

  @override
  Widget build(BuildContext context) {
    Color _color = _isHover ? LabColors.base400 : Colors.transparent;
    return Column(
      children: [
        MouseRegion(
          cursor: SystemMouseCursors.click,
          onEnter: _handleMouseEnter,
          onExit: _handleMouseExit,
          child: GestureDetector(
            onTap: widget.onClick,
            child: Container(
              decoration: BoxDecoration(
                color: _color,
                borderRadius: BorderRadius.circular(5),
              ),
              child: Container(
                width: 80,
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      width: 40,
                      height: 40,
                      child: Icon(
                        widget.iconData,
                        size: 40,
                        color: LabColors.text700,
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(top: 5),
                      child: Text(
                        widget.operateName,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          height: 1,
                          fontSize: 14,
                          color: LabColors.text700,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
        SizedBox(height: 1),
        if (widget.moreOperateButtons.isNotEmpty)
          MoreOperateMenu(
            buttons: widget.moreOperateButtons,
          ),
      ],
    );
  }

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

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