import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_translate/ads/interstitial_ad_manage.dart';
import 'package:flutter_translate/generated/assets.dart';
import 'package:get/get.dart';

class BaseAppbar extends StatelessWidget implements PreferredSizeWidget {
  const BaseAppbar({
    super.key,
    this.backgroundColor,
    this.backColor,
    this.title,
    this.titleWidget,
    this.actionWidget,
    this.onBackTap,
  });

  final Color? backgroundColor;
  final Color? backColor;
  final String? title;
  final Widget? titleWidget;
  final Widget? actionWidget;
  final Function()? onBackTap;

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: backgroundColor ?? Colors.white,
      surfaceTintColor: Colors.transparent,
      automaticallyImplyLeading: false,
      centerTitle: false,
      title: _buildTitle(context),
      titleSpacing: 0.0,
      leadingWidth: 0.0,
    );
  }

  Widget _buildTitle(BuildContext context) {
    return SizedBox(
      height: preferredSize.height,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            width: 60.w,
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 4).w,
              child: FittedBox(
                fit: BoxFit.none,
                child: ClipOval(
                  child: Material(
                    color: Colors.transparent,
                    child: InkWell(
                      onTap: onBackTap ?? () {
                        InterstitialAdManager().showAdIfReady(
                          InterstitialAdManager().adIds[1],
                          onTap: () {
                            Get.back();
                          },
                        );
                      },
                      child: Padding(
                        padding: const EdgeInsets.all(10.0).w,
                        child: Image.asset(
                          Assets.iconBack,
                          width: 24.w,
                          height: 24.w,
                          color: backColor?? Colors.black,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
          titleWidget ??
              Text(
                title ?? '',
                overflow: TextOverflow.ellipsis,
                style:  TextStyle(
                  color: Colors.black,
                  fontSize: 18.sp,
                  fontWeight: FontWeight.w500,
                ),
              ),
          SizedBox(
            width: 60.w,
            child: actionWidget,
          ),
        ],
      ),
    );
  }

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