import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';

/// 动态按钮，默认显示状态[displayString]，鼠标悬浮时显示操作提示[actionString]
class DynamicButton extends FStatefulWidget implements FInteractiveContainer {
  const DynamicButton({
    super.key,
    required this.displayString,
    required this.actionString,
    required this.onPressed,
    this.displayColor = Colors.green,
    this.actionColor = Colors.red,
  });
  final String displayString;
  final String actionString;
  final Function onPressed;
  final Color displayColor;
  final Color actionColor;

  @override
  FState<DynamicButton> createState() => _DynamicButtonState();

  @override
  String get pageName => "DynamicButton";
}

class _DynamicButtonState extends FState<DynamicButton> {
  String showString = '';
  Color bgColor = Colors.white;
  Color textColor = Colors.black;
  @override
  void initState() {
    super.initState();
    showString = widget.displayString;
    textColor = widget.displayColor;
  }

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      margin: const EdgeInsets.only(right: 15),
      child: FMouseRegion(
        onEnter: (_) {
          setState(() {
            showString = widget.actionString;
            bgColor = widget.actionColor;
            textColor = Colors.white;
          });
        },
        onExit: (_) {
          setState(() {
            showString = widget.displayString;
            bgColor = Colors.white;
            textColor = widget.displayColor;
          });
        },
        child: FOutlinedButton(
          onPressed: () => widget.onPressed.call(),
          style: ButtonStyle(
            padding: MaterialStateProperty.all(
              const EdgeInsets.symmetric(vertical: 8, horizontal: 15),
            ),
            overlayColor: MaterialStateProperty.all<Color>(bgColor),
          ),
          businessParent: widget,
          name: showString,
          child: FCenter(
            child: FText(
              showString,
              style: TextStyle(color: textColor),
            ),
          ),
        ),
      ),
    );
  }
}
