// 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/>.

/// This file contains the abstract class [TopBarLayout] and two concrete
/// classes [TopAppBarLayout] and [TopNavigationBarLayout].
/// The [TopBarLayout] abstract class defines a layout structure with a top bar.
/// The [TopAppBarLayout] and [TopNavigationBarLayout] classes are concrete
///  implementations of the [TopBarLayout] class.

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

/// An abstract class for creating layouts with a top bar component.
///
/// This class provides a base for creating layouts that include a top bar
/// component, such as an app bar or navigation bar.
///
/// Implementations of this class must provide a concrete implementation of
/// the [child] method, which returns the specific top bar component for the
/// given [BuildContext].
///
/// [T] represents the type of the top bar component.
abstract class TopBarLayout<T extends TopBarComponent>
    extends StructuralLayout {
  /// Creates a [TopBarLayout] instance.
  ///
  /// [body] represents the main content of the layout.
  /// [custom] is an optional function that can be used to customize
  ///  the top bar component.
  /// [height] represents the height of the top bar.
  const TopBarLayout({
    required this.body,
    this.custom,
    this.height = 60,
    super.key,
  });

  final Widget body;

  final T? Function(T?)? custom;
  final double height;

  /// Returns the top bar component for the given [BuildContext].
  T? child(BuildContext context);

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

/// A concrete implementation of [TopBarLayout] for an app bar.
class TopAppBarLayout extends TopBarLayout<TopAppBarComponent> {
  /// Creates a [TopAppBarLayout] instance.
  ///
  /// [body] represents the main content of the layout.
  /// [custom] is an optional function that can be used to customize
  ///  the top bar component.
  /// [height] represents the height of the top bar.
  const TopAppBarLayout({
    required super.body,
    super.custom,
    super.height,
    super.key,
  });

  @override
  TopAppBarComponent? child(BuildContext context) => context.components.appBar;
}

/// A concrete implementation of [TopBarLayout] for a navigation bar.
class TopNavigationBarLayout extends TopBarLayout<TopNavigationBarComponent> {
  /// Creates a [TopNavigationBarLayout] instance.
  ///
  /// [body] represents the main content of the layout.
  /// [custom] is an optional function that can be used to customize
  ///  the top bar component.
  /// [height] represents the height of the top bar.
  const TopNavigationBarLayout({
    required super.body,
    super.custom,
    super.height,
    super.key,
  });

  @override
  TopNavigationBarComponent? child(BuildContext context) =>
      context.components.topNavigationBarComponent;
}
