import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/widgets/language_switcher.dart';

import '../../generated/locale_keys.g.dart';

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  final VoidCallback onBackPressed;
  final ValueChanged<String> onLanguageChanged;
  final bool hideLogoAndLang;

  const CustomAppBar({
    super.key,
    required this.onBackPressed,
    required this.onLanguageChanged,
    this.hideLogoAndLang = false,
  });

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

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: Colors.transparent,
      leading: null,
      automaticallyImplyLeading: false,
      title: Padding(
        padding: EdgeInsets.symmetric(horizontal: 10.h),
        child: Row(
          // mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Expanded(
              child: Align(
                alignment: context.locale.languageCode == "ar" ? Alignment.centerRight : Alignment.centerLeft,
                child: GestureDetector(
                  onTap: onBackPressed,
                  child: context.locale.languageCode == "ar"
                      ? RotatedBox(
                          quarterTurns: 90,
                          child: Utils.buildSvgWithAssets(
                            icon: AppAssets.forward_top_nav_icon,
                            width: 24.w,
                            height: 24.h,
                          ),
                        )
                      : Utils.buildSvgWithAssets(
                          icon: AppAssets.forward_top_nav_icon,
                          width: 24.w,
                          height: 24.h,
                        ),
                ),
              ),
            ),

            // Logo
            if (!hideLogoAndLang) Utils.buildSvgWithAssets(icon: AppAssets.habiblogo, height: 24.h, applyThemeColor: false),

            if (!hideLogoAndLang)
              Expanded(
                child: Align(
                  alignment: context.locale.languageCode == "ar" ? Alignment.centerLeft : Alignment.centerRight,
                  child: LanguageSelector(
                    currentLanguage: context.locale.languageCode,
                    showOnlyIcon: false,
                    onLanguageChanged: onLanguageChanged,
                    languages: [
                      {'code': 'ar', 'name': LocaleKeys.arabic.tr()},
                      {'code': 'en', 'name': LocaleKeys.english.tr()}
                    ],
                  ),
                ),
              ),
          ],
        ),
      ),
      centerTitle: true,
    );
  }
}
