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

class FExpandedButtons extends FStatefulWidget implements FPage {
  final Map<String, void Function()> operates;
  final FWidget Function(String, Function) textButton;
  final bool canEditReport;
  FExpandedButtons(this.operates, this.textButton, this.canEditReport);

  @override
  FState<FStatefulWidget> createState() {
    return FExpandedButtonsState();
  }

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

class FExpandedButtonsState extends FState<FExpandedButtons> {
  bool _isExpandButtons = false;

  @override
  FWidget build(BuildContext context) {
    List<FWidget> children = [
      widget.textButton(
          i18nBook.common.view.t, widget.operates[i18nBook.common.view.t]!),
      if (widget.canEditReport) ...[
        widget.textButton(i18nBook.common.modify.t,
            widget.operates[i18nBook.common.modify.t]!),
      ],
    ];
    if (_isExpandButtons) {
      children = widget.operates.keys
          .map((e) => widget.textButton(e, widget.operates[e]!))
          .toList();
    }
    if (children.length % 3 == 0) {
      ///占位用，为了让收缩按钮在最右边
      children.add(FSizedBox());
    }
    if (widget.operates.length > 2) {
      children.add(
        FTextButton(
          child: FText(_isExpandButtons
              ? i18nBook.measure.collapseGroup.t
              : i18nBook.measure.expandGroup.t),
          onPressed: () {
            setState(() {
              _isExpandButtons = !_isExpandButtons;
            });
          },
          style: ButtonStyle(
            padding:
                MaterialStateProperty.all(EdgeInsets.symmetric(horizontal: 5)),
            fixedSize: MaterialStateProperty.all<Size>(
              Size(70, 25),
            ),
          ),
          businessParent: widget,
          name: "expand button",
        ),
      );
    }
    return FWrap(
      alignment: WrapAlignment.spaceBetween,
      spacing: 10,
      runSpacing: 5,
      children: children,
    );
  }
}
