import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';

/// 多元素混合布局
class FMixtureLayout extends FStatelessWidget {
  FMixtureLayout({super.key, required this.children});
  final List<MixtureLayoutElement> children;
  final ScrollController _scrollController = ScrollController();
  @override
  FWidget build(BuildContext context) {
    return FLayoutBuilder(builder: (context, constraints) {
      List<FWidget> rows = [];
      List<FWidget> rowElements = [];
      var singleCounter = 0;
      var doubleCounter = 0;
      for (var i = 0; i < children.length; i++) {
        if (children[i] is HalfRowLayout) {
          singleCounter++;
          rowElements.add(children[i]);
        } else if (children[i] is FullRowLayout) {
          doubleCounter++;
          rowElements.add(children[i]);
        }
        if (singleCounter == 2) {
          rows.add(FRow(
              children: rowElements.map((e) => FExpanded(child: e)).toList()));
          rowElements = [];
          singleCounter = 0;
          doubleCounter = 0;
        } else if (singleCounter == 1 && doubleCounter == 1) {
          if (rowElements[0] is HalfRowLayout) {
            rows.add(FRow(children: [
              FExpanded(child: rowElements[0]),
              const FExpanded(child: FSizedBox())
            ]));
            rows.add(rowElements[1]);
          } else {
            rows.add(rowElements[0]);
            rows.add(FRow(children: [
              FExpanded(child: rowElements[1]),
              const FExpanded(child: FSizedBox())
            ]));
          }
          rowElements = [];
          singleCounter = 0;
          doubleCounter = 0;
        } else if (singleCounter == 0 && doubleCounter == 1) {
          rows.add(rowElements[0]);
          rowElements = [];
          singleCounter = 0;
          doubleCounter = 0;
        }
      }
      if (rowElements.isNotEmpty) {
        if (rowElements.last is HalfRowLayout) {
          rows.add(FRow(children: [
            FExpanded(child: rowElements.last),
            const FExpanded(child: FSizedBox())
          ]));
        } else {
          rows.add(rowElements.last);
        }
      }

      return FAlwaysScrollListView(
          scrollController: _scrollController,
          child: QuickFWidget(ScrollConfiguration(
            behavior:
                ScrollConfiguration.of(context).copyWith(scrollbars: false),
            child: FListView(
              shrinkWrap: true,
              controller: _scrollController,
              children: [
                FColumn(
                  children: rows,
                ),
              ],
            ),
          )));
    });
  }
}

/// 占位一半的布局
class HalfRowLayout extends MixtureLayoutElement {
  const HalfRowLayout({super.key, required this.child}) : super();

  final FWidget child;
  @override
  MixtureLayoutType get layoutType => MixtureLayoutType.single;

  @override
  FWidget build(BuildContext context) {
    return child;
  }
}

/// 占位全部的布局
class FullRowLayout extends MixtureLayoutElement {
  const FullRowLayout({super.key, required this.child}) : super();

  final FWidget child;
  @override
  MixtureLayoutType get layoutType => MixtureLayoutType.double;

  @override
  FWidget build(BuildContext context) {
    return child;
  }
}

/// Flow 不能自适应子组件大小，必须通过指定父容器大小或实现TestFlowDelegate的getSize返回固定大小。
abstract class MixtureLayoutElement extends FStatelessWidget {
  const MixtureLayoutElement({super.key});
  MixtureLayoutType get layoutType;
}

enum MixtureLayoutType {
  /// 占据一列
  single,

  /// 占据两列
  double,
}
