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

class RAppBar 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 int? leadingWidth;
  final Widget? leading;
  final PreferredSizeWidget? bottom;

  const RAppBar({
    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:title != null ?  Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(title!),
          if (iconTitle != null) ...{const SizedBox(width: 8)},
          if (iconTitle != null) ...{SvgGenImage.vec(iconTitle!).svg(width: 24, height: 24)},
        ],
      ): null,
      leadingWidth: leadingWidth?.toDouble(),
      leading: leading != null ? Padding(padding: const EdgeInsets.only(right: 6), child: leading) : null,
      titleSpacing: 8,
      actions: [
        if (additionalActions != null) ...additionalActions!,
        if (hasBack) ...{
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: IconButton(
              onPressed: onBackPressed ?? () => Get.back(),
              icon: const Icon(CupertinoIcons.chevron_back),
              color: iconColor,
            ),
          ),
        },
      ],
      bottom: bottom,
    );
  }

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