import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/lab_text_tool_tip.dart';

class RectIconButton extends StatefulWidget {
  const RectIconButton({
    super.key,
    this.onPressed,
    this.isSelected = false,
    this.icon = Icons.grid_view,
    this.tooltipMessage,
    this.bgColor = Colors.transparent,
    this.hoverBgColor = LabColors.base300,
    this.selectedBgColor = LabColors.buttonColor,
    this.iconColor = LabColors.text600,
    this.selectedIconColor = LabColors.text100,
    this.size = const Size(40, 40),
    this.iconSize = 25,
  });

  final VoidCallback? onPressed;
  final String? tooltipMessage;
  final bool isSelected;
  final IconData icon;
  final Color bgColor;
  final Color hoverBgColor;
  final Color selectedBgColor;
  final Color iconColor;
  final Color selectedIconColor;
  final Size size;
  final double iconSize;

  @override
  State<RectIconButton> createState() => _RectIconButtonState();
}

class _RectIconButtonState extends State<RectIconButton> {
  bool _isHover = false;

  @override
  Widget build(BuildContext context) {
    Color bgColor = _isHover ? widget.hoverBgColor : widget.bgColor;
    if (widget.isSelected) {
      bgColor = widget.selectedBgColor;
    }
    return LabTextTooltip(
      message: widget.tooltipMessage ?? '',
      disable: widget.isSelected || widget.tooltipMessage == null,
      offset: Offset(5, -8),
      child: Focus(
        onFocusChange: (value) {
          setState(() {
            _isHover = value;
          });
        },
        onKey: (node, event) {
          if (!(event is RawKeyDownEvent)) {
            return KeyEventResult.ignored;
          }
          if (event.logicalKey.keyLabel == 'Enter' ||
              event.logicalKey.keyLabel == 'Numpad Enter') {
            widget.onPressed?.call();
            return KeyEventResult.handled;
          }
          return KeyEventResult.ignored;
        },
        child: SizedBox(
          width: widget.size.width,
          height: widget.size.height,
          child: MouseRegion(
            cursor: SystemMouseCursors.click,
            onEnter: _handleMouseEnter,
            onExit: _handleMouseExit,
            child: GestureDetector(
              onTap: () {
                widget.onPressed?.call();
              },
              child: Container(
                decoration: BoxDecoration(
                  color: bgColor,
                  borderRadius: BorderRadius.circular(5),
                ),
                child: Icon(
                  widget.icon,
                  size: widget.iconSize,
                  color: widget.isSelected
                      ? widget.selectedIconColor
                      : widget.iconColor,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

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

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