import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';

class RAppBar extends StatelessWidget implements PreferredSizeWidget {
  final List<Widget>? children;

  final bool centerTitle;

  final Color backgroundColor;

  final bool isBase;

  final bool hasBack;
  final VoidCallback? onBackTap;
  final int? backId;

  final bool hasSearch;
  final VoidCallback? onSearchTap;

  final bool hasFilter;
  final VoidCallback? onFilterTap;

  final bool hasNotification;
  final VoidCallback? onNotificationTap;

  final bool hasNews;
  final VoidCallback? onNewsTap;

  /// Preferred size widget for the AppBar bottom.
  final PreferredSizeWidget? bottom;


  const RAppBar({
    super.key,
    this.children,
    this.backgroundColor = AppColor.blueNormal,
    this.hasBack = true,
    this.hasSearch = false,
    this.hasNews = false,
    this.hasNotification = false,
    this.hasFilter = false,
    this.isBase = false,
    this.centerTitle = false,
    this.bottom,
    this.backId,
    this.onBackTap,
    this.onSearchTap,
    this.onNewsTap,
    this.onNotificationTap,
    this.onFilterTap,
  });

  @override
  Widget build(BuildContext context) {
    return AppBar(
      automaticallyImplyLeading: false,
      backgroundColor: backgroundColor,
      elevation: 0,
      excludeHeaderSemantics: true,
      scrolledUnderElevation: 0,
      title: Row(
        mainAxisAlignment: _getMainAxisAlignment(),
        mainAxisSize: MainAxisSize.min,
        textDirection: TextDirection.rtl, // Support for RTL languages
        children: [
          if (children != null) ...children!,

          if (hasNews || hasBack || hasSearch || hasNotification || hasFilter) const Spacer(),

          if (hasNews)
            GestureDetector(
              onTap: onNewsTap,

              child: Icon(CupertinoIcons.news_solid, color: Colors.white),
            ),
          if (hasNews) SizedBox(width: 8.w),

          if (hasNotification)
            GestureDetector(
              onTap: onNotificationTap,
              child: Icon(CupertinoIcons.bell_fill, color: Colors.white),
            ),

          if (hasNotification) SizedBox(width: 8.w),

          if (!isBase) ...{
            if (hasFilter)
              GestureDetector(
                onTap: onFilterTap,
                child: Assets.vec.filterSvg.svg(
                  width: 24.w,
                  height: 24.h,
                  colorFilter: ColorFilter.mode(Colors.white, BlendMode.srcIn),
                ),
              ),
            if (hasBack) SizedBox(width: 8.w),

            if (hasSearch)  GestureDetector(
              onTap: onSearchTap,
              child: Assets.vec.searchSvg.svg(
                width: 24.w,
                height: 24.h,
                colorFilter: ColorFilter.mode(Colors.white, BlendMode.srcIn),
              ),
            ),
            if (hasSearch) SizedBox(width: 8.w),
          },
          if (hasBack)
            GestureDetector(
              onTap: onBackTap ?? () => Get.back(id: backId),
              child: Padding(
                padding: const EdgeInsets.fromLTRB(10, 0, 2, 0),
                child: Assets.vec.arrowLeftSvg.svg(
                  width: 24.w,
                  height: 24.h,
                  colorFilter: ColorFilter.mode(Colors.white, BlendMode.srcIn),
                ),
              ),
            ),
          if (hasBack) SizedBox(width: 8.w),
        ],
      ),
      titleSpacing: 2,
      bottom: bottom,
    );
  }

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

  MainAxisAlignment _getMainAxisAlignment() {
    if (centerTitle) {
      return MainAxisAlignment.center;
    } else if (children != null && children!.isNotEmpty) {
      return MainAxisAlignment.start;
    } else {
      return MainAxisAlignment.end;
    }
  }
}

class RAppBar2 extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final String? iconTitle;
  final Color backgroundColor;
  final Color iconColor;
  final bool hasBack;
  final bool centerTitle;
  final TextStyle? titleTextStyle;
  final VoidCallback? onBackPressed;
  final List<Widget>? additionalActions;
  final double? leadingWidth;
  final Widget? leading;
  final PreferredSizeWidget? bottom;

  const RAppBar2({
    super.key,
    this.title,
    this.iconTitle,
    this.backgroundColor = AppColor.blueNormal,
    this.iconColor = Colors.white,
    this.titleTextStyle,
    this.onBackPressed,
    this.additionalActions,
    this.leading,
    this.hasBack = true,
    this.centerTitle = false,
    this.leadingWidth,
    this.bottom,
  });

  @override
  Widget build(BuildContext context) {
    return AppBar(
      automaticallyImplyLeading: false,
      backgroundColor: backgroundColor,
      elevation: 0,
      excludeHeaderSemantics: true,
      scrolledUnderElevation: 0,
      centerTitle: centerTitle,
      titleTextStyle: titleTextStyle ?? AppFonts.yekan16.copyWith(color: Colors.white),
      title: Row(
        children: [
          if (leading != null) ...{
            Padding(padding: const EdgeInsets.only(right: 6), child: leading),
          },
          if (title != null) ...[Text(title!), if (iconTitle != null) const SizedBox(width: 8)],
          if (iconTitle != null) ...{const SizedBox(width: 8)},
          if (iconTitle != null) ...{SvgGenImage.vec(iconTitle!).svg(width: 24, height: 24)},
        ],
      ),
      titleSpacing: 8,
      actions: [
        if (additionalActions != null) ...additionalActions!,
        if (hasBack) ...{
          GestureDetector(
            onTap: onBackPressed ?? () => Get.back(),
            child: Padding(
              padding: const EdgeInsets.fromLTRB(10, 0, 2, 0),
              child: Assets.vec.arrowLeftSvg.svg(
                width: 24.w,
                height: 24.h,
                colorFilter: ColorFilter.mode(iconColor ?? Colors.white, BlendMode.srcIn),
              ),
            ),
          ),
        },
      ],
      bottom: bottom,
    );
  }

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