// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:wyatt_ui_layout/src/presentation/layouts/layout.dart';

class GridLayout extends ContentLayout {
  const GridLayout({
    required this.children,
    this.verticalGap = 30,
    this.horizontalGap = 30,
    super.key,
  });

  final List<Widget> children;
  final double verticalGap;
  final double horizontalGap;

  @override
  Widget build(BuildContext context) {
    if (children.isEmpty) {
      return const SizedBox.shrink();
    }

    final childrenLeft = <Widget>[];
    final childrenRight = <Widget>[];

    int i = 0;
    for (final child in children) {
      if (i.isEven) {
        childrenLeft
          ..add(child)
          ..add(Gap(verticalGap));
      } else {
        childrenRight
          ..add(child)
          ..add(Gap(verticalGap));
      }
      i++;
    }

    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Column(
                children: childrenLeft,
              ),
            ),
            Gap(horizontalGap),
            Expanded(
              child: Column(
                children: childrenRight,
              ),
            ),
          ],
        ),
      ],
    );
  }
}
