import 'package:flutter/material.dart';

import '../ui_icons.dart';

/// A custom AppBar for the Krow UI design system.
///
/// This widget provides a consistent look and feel for top app bars across the application.
class UiAppBar extends StatelessWidget implements PreferredSizeWidget {
  /// The title text to display in the app bar.
  final String? title;

  /// A widget to display instead of the title text.
  final Widget? titleWidget;

  /// The widget to display before the title.
  /// Usually an [IconButton] for navigation.
  final Widget? leading;

  /// A list of Widgets to display in a row after the [title] widget.
  final List<Widget>? actions;

  /// The height of the app bar. Defaults to [kToolbarHeight].
  final double height;

  /// Whether the title should be centered.
  final bool centerTitle;

  /// Signature for the callback that is called when the leading button is pressed.
  /// If [leading] is null, this callback will be used for a default back button.
  final VoidCallback? onLeadingPressed;

  /// Whether to show a default back button if [leading] is null.
  final bool showBackButton;

  /// This widget appears across the bottom of the app bar.
  /// Typically a [TabBar]. Only widgets that implement [PreferredSizeWidget] can be used at the bottom of an app bar.
  final PreferredSizeWidget? bottom;

  const UiAppBar({
    super.key,
    this.title,
    this.titleWidget,
    this.leading,
    this.actions,
    this.height = kToolbarHeight,
    this.centerTitle = true,
    this.onLeadingPressed,
    this.showBackButton = true,
    this.bottom,
  });

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: titleWidget ??
          (title != null
              ? Text(
                  title!,
                )
              : null),
      leading: leading ??
          (showBackButton
              ? IconButton(
                  icon: const Icon(UiIcons.chevronLeft, size: 20),
                  onPressed: onLeadingPressed ?? () => Navigator.of(context).pop(),
                )
              : null),
      actions: actions,
      centerTitle: centerTitle,
      bottom: bottom,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(height + (bottom?.preferredSize.height ?? 0.0));
}
