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

typedef void ParentSelectChanged(bool isSelected);

/// # Parent widget
///
/// The [Parent] widget holds the [Parent.parent] widget and
/// [Parent.childList] which is a [List] of child widgets.
///
/// The [Parent] widget is wrapped around a [Column]. The [Parent.childList]
/// is collapsed by default. When clicked the child widget is expanded.
class Parent extends FStatefulWidget implements FPage {
  final FWidget parent;
  final ChildList childList;
  final MainAxisSize mainAxisSize;
  final CrossAxisAlignment crossAxisAlignment;
  final MainAxisAlignment mainAxisAlignment;
  final ParentSelectChanged? callback;
  final Key? key;
  @override
  final String pageName;
  Parent({
    required this.parent,
    required this.childList,
    this.mainAxisAlignment = MainAxisAlignment.center,
    this.crossAxisAlignment = CrossAxisAlignment.start,
    this.mainAxisSize = MainAxisSize.min,
    this.callback,
    this.key,
    this.pageName = 'Parent',
  });

  @override
  FState<Parent> createState() => ParentState();
}

class ParentState extends FState<Parent> {
  /// 默认打开下拉
  bool _isSelected = true;

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      mainAxisSize: widget.mainAxisSize,
      crossAxisAlignment: widget.crossAxisAlignment,
      mainAxisAlignment: widget.mainAxisAlignment,
      children: <FWidget>[
        FGestureDetector(
          name: "expand",
          businessParent: this.widget,
          child: widget.parent,
          onTap: expand,
        ),
        _getChild(),
      ],
    );
  }

  ///点击展开事件
  void expand() {
    if (widget.callback != null) widget.callback!(_isSelected);
    setState(() {
      _isSelected = _toggleBool(_isSelected);
      if (_isSelected) {
        print('ws');
      } else {
        print('wbs');
      }
    });
  }

  ///选中事件
  bool _toggleBool(bool b) {
    return b ? false : true;
  }

  ///获取子组件
  FWidget _getChild() {
    if (_isSelected) {
      return widget.childList;
    } else {
      return FContainer();
    }
  }
}

/// # ChildList widget
///
/// The [ChildList] widget holds a [List] of widget which will be displayed as
/// children of the [Parent] widget
class ChildList extends FStatelessWidget {
  ///子集
  final List<FWidget> children;

  ///主轴对齐方式
  final MainAxisAlignment mainAxisAlignment;

  ///非主轴对齐方式
  final CrossAxisAlignment crossAxisAlignment;

  ///主轴方向尺寸
  final MainAxisSize mainAxisSize;

  ChildList({
    this.children = const <FWidget>[],
    this.mainAxisSize = MainAxisSize.min,
    this.crossAxisAlignment = CrossAxisAlignment.start,
    this.mainAxisAlignment = MainAxisAlignment.center,
  });

  @override
  FWidget build(BuildContext context) {
    return FColumn(
      mainAxisAlignment: mainAxisAlignment,
      crossAxisAlignment: crossAxisAlignment,
      mainAxisSize: mainAxisSize,
      children: children,
    );
  }
}
