// 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:wyatt_ui_components/wyatt_ui_components.dart';
import 'package:wyatt_ui_layout/src/presentation/layouts/layout.dart';

/// A layout that contains a top app bar, a body and a bottom navigation bar.
///
/// This layout consists of a [TopAppBarComponent] at the top of the screen,
/// a [body] in the middle and a [BottomNavigationBarComponent] at the bottom.
/// You can customize the app bar and the bottom navigation bar by passing
/// a [customAppBar] and a [customBottomNavBar] functions that take
/// the corresponding components and return the customized ones.
class FrameLayout extends StructuralLayout {
  /// Creates a [FrameLayout] instance.
  ///
  /// [body] represents the main content of the layout.
  /// [customAppBar] is an optional function that can be used to customize
  ///  the top app bar component.
  /// [customBottomNavBar] is an optional function that can be used to customize
  ///  the bottom navigation bar component.
  /// [height] represents the height of the top app bar.
  const FrameLayout({
    required this.body,
    this.customAppBar,
    this.customBottomNavBar,
    this.height = 60,
    super.key,
  });

  final TopAppBarComponent? Function(TopAppBarComponent?)? customAppBar;
  final BottomNavigationBarComponent? Function(BottomNavigationBarComponent?)?
      customBottomNavBar;
  final Widget body;
  final double height;

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: (customAppBar?.call(context.components.appBar) != null ||
                context.components.appBar != null)
            ? PreferredSize(
                preferredSize: Size.fromHeight(height),
                child: customAppBar?.call(context.components.appBar) ??
                    context.components.appBar!,
              )
            : null,
        body: body,
        bottomNavigationBar:
            customBottomNavBar?.call(context.components.bottomNavigationBar) ??
                context.components.bottomNavigationBar,
      );
}
