import 'package:flutter/material.dart';
import 'package:fotodocumentation/pages/ui_utils/header_button_wrapper.dart';

class ModernAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Widget title;
  final List<Widget> actions;
  final Widget? leading;
  final bool automaticallyImplyLeading;

  const ModernAppBar({
    super.key,
    required this.title,
    this.actions = const [],
    this.leading,
    this.automaticallyImplyLeading = true,
  });

  @override
  Widget build(BuildContext context) {
    Widget? effectiveLeading = _effectiveLeading(context);
    return Container(
      decoration: BoxDecoration(
        color: Colors.grey[50],
        border: Border(
          bottom: BorderSide(
            color: Colors.grey[300]!,
            width: 1,
          ),
        ),
      ),
      child: SafeArea(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
          child: Row(
            children: [
              if (effectiveLeading != null) ...[
                effectiveLeading,
                const SizedBox(width: 16),
              ],
              Expanded(child: title),
              const SizedBox(width: 16),
              ...actions.map((action) => Padding(
                    padding: const EdgeInsets.only(left: 8.0),
                    child: action,
                  )),
            ],
          ),
        ),
      ),
    );
  }

  Widget? _effectiveLeading(BuildContext context) {
    // Determine if we should show a back button
    final ScaffoldState? scaffold = Scaffold.maybeOf(context);
    final ModalRoute<dynamic>? parentRoute = ModalRoute.of(context);
    final bool hasDrawer = scaffold?.hasDrawer ?? false;
    final bool canPop = parentRoute?.canPop ?? false;
    final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;

    Widget? effectiveLeading = leading;
    if (effectiveLeading == null && automaticallyImplyLeading) {
      if (hasDrawer) {
        effectiveLeading = HeaderButtonWrapper(
          icon: Icons.menu,
          onPressed: () {
            Scaffold.of(context).openDrawer();
          },
          tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
          iconColor: Colors.grey[700],
        );
      } else if (canPop) {
        if (useCloseButton) {
          effectiveLeading = HeaderButtonWrapper(
            icon: Icons.close,
            onPressed: () {
              Navigator.of(context).pop();
            },
            tooltip: MaterialLocalizations.of(context).closeButtonTooltip,
            iconColor: Colors.grey[700],
          );
        } else {
          effectiveLeading = HeaderButtonWrapper(
            icon: Icons.arrow_back,
            onPressed: () {
              Navigator.of(context).pop();
            },
            tooltip: MaterialLocalizations.of(context).backButtonTooltip,
            iconColor: Colors.grey[700],
          );
        }
      }
    }
    return effectiveLeading;
  }

  @override
  Size get preferredSize => const Size.fromHeight(80);
}
