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

class ExpandableIconText extends FStatefulWidget implements FPage {
  final IconData icon;
  final String text;
  final bool isSelected;
  final bool isExpand;
  final VoidCallback onPressed;
  final bool isExpandChild;
  final void Function(bool)? onHoverChange;
  final double horizontalMargin;
  final bool isDistinguishChinese;

  ExpandableIconText({
    required this.icon,
    required this.text,
    this.isSelected = false,
    this.isExpand = false,
    this.isExpandChild = true,
    this.isDistinguishChinese = true,
    this.horizontalMargin = 0,
    this.onHoverChange,
    required this.onPressed,
  });

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

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

class _ExpandableIconTextState extends FState<ExpandableIconText> {
  bool _isSelected = false;
  bool _isHovered = false;
  bool _isExpand = false;

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

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

  @override
  FWidget build(BuildContext context) {
    var child = FMouseRegion(
      cursor: SystemMouseCursors.click,
      onEnter: (event) {
        setState(() {
          _isHovered = true;
        });
        widget.onHoverChange?.call(_isHovered);
      },
      onExit: (event) {
        setState(() {
          _isHovered = false;
        });
        widget.onHoverChange?.call(_isHovered);
      },
      child: _buildContentView(),
    );
    if (_isSelected || _isHovered) {
      return child;
    }
    if (widget.isExpandChild) {
      return FExpanded(child: child);
    }
    return child;
  }

  FWidget _buildContentView() {
    Color back = Colors.black;
    if (_isSelected) {
      back = FTheme.ins.colorScheme.primary;
    } else if (_isHovered) {
      back = FTheme.ins.colorScheme.secondary;
    }
    return FContainer(
      margin: EdgeInsets.symmetric(horizontal: widget.horizontalMargin),
      padding: EdgeInsets.only(bottom: 7),
      constraints: BoxConstraints(
        minWidth: 70.0, // 设置最小宽度为100像素
      ),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: (_isSelected || _isHovered) ? back : Colors.transparent,
            width: 2.0,
          ),
        ),
      ),
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const FSizedBox(
            width: 10,
          ),
          if (!i18nBook.isCurrentChinese && widget.isDistinguishChinese)
            FGestureDetector(
              onTap: () {
                if (!_isSelected) {
                  setState(() {
                    _isSelected = true;
                  });
                }
                widget.onPressed();
              },
              businessParent: widget,
              child: FIcon(
                widget.icon,
                color: back,
              ),
              name: widget.text,
            ),
          if (_isExpand ||
              !widget.isDistinguishChinese ||
              (i18nBook.isCurrentChinese && widget.isDistinguishChinese)) ...[
            FGestureDetector(
              onTap: () {
                if (!_isSelected) {
                  setState(() {
                    _isSelected = true;
                  });
                }
                widget.onPressed.call();
              },
              child: FText(
                widget.text,
                style: TextStyle(
                  color: back,
                ),
              ),
              name: widget.text,
              businessParent: widget,
            ),
          ],
          const FSizedBox(
            width: 10,
          ),
        ],
      ),
    );
  }
}
