import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';

class FHoverButton extends FStatefulWidget implements FPage {
  final String text;
  final VoidCallback onPressed;
  final double? width;
  final double? height;
  final Color? hoverColor;
  final Color? defaultColor;
  final bool isSelected;
  final IconData icon;
  final bool clickOnly;
  final double fontSize;

  /// 仅用于点击，无选中状态

  FHoverButton({
    required this.text,
    required this.onPressed,
    required this.icon,
    this.width = 200,
    this.height = 40,
    this.hoverColor,
    this.fontSize = 18,
    this.isSelected = false,
    this.clickOnly = false,
    this.defaultColor = Colors.transparent,
  });

  @override
  _FHoverButtonState createState() => _FHoverButtonState();

  @override
  String get pageName => 'FHoverButton';
}

class _FHoverButtonState extends FState<FHoverButton> {
  bool isHovering = false;
  bool _isSelected = false;
  Color get _hoverColor =>
      widget.hoverColor ?? FTheme.ins.colorScheme.secondary;
  Color get _selectedColor => FTheme.ins.colorScheme.primary;
  Color get _defaultTextColor => Color.fromRGBO(113, 123, 133, 1);

  @override
  void initState() {
    _isSelected = widget.isSelected;
    super.initState();
  }

  @override
  void didUpdateWidget(FHoverButton oldWidget) {
    if (oldWidget.isSelected != widget.isSelected) {
      setState(() {
        _isSelected = widget.isSelected;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  FWidget build(BuildContext context) {
    var color = widget.defaultColor;
    if (_isSelected) {
      color = _selectedColor;
    } else if (isHovering) {
      color = _hoverColor;
    }

    return FInkWell(
      onTap: () {
        if (!widget.clickOnly) {
          _isSelected = !_isSelected;
        }
        widget.onPressed.call();
      },
      onHover: (value) {
        setState(() {
          isHovering = value;
        });
      },
      child: AnimatedContainer(
        duration: Duration(milliseconds: 200),
        margin: EdgeInsets.symmetric(horizontal: 15, vertical: 5),
        width: widget.width ?? 120,
        height: widget.height ?? 50,
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(8),
          boxShadow: (isHovering || _isSelected)
              ? [
                  BoxShadow(
                    color: _hoverColor.withOpacity(0.6),
                    blurRadius: 10,
                    offset: Offset(0, 10),
                  ),
                ]
              : [],
        ),
        child: FContainer(
          alignment: Alignment.centerLeft,
          margin: EdgeInsets.only(left: 15),
          child: FRow(
            children: [
              FIcon(
                widget.icon,
                color: (isHovering || _isSelected)
                    ? Colors.white
                    : _defaultTextColor,
              ),
              FSizedBox(
                width: 10,
              ),
              FText(
                widget.text,
                style: TextStyle(
                    fontSize: widget.fontSize,
                    fontWeight: FontWeight.bold,
                    color: (isHovering || _isSelected)
                        ? Colors.white
                        : _defaultTextColor),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
