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/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

class LanguageSelector extends StatefulWidget {
  final String currentLanguage;
  final ValueChanged<String> onLanguageChanged;
  final List<Map<String, String>> languages;
  final bool showOnlyIcon;

  const LanguageSelector({
    super.key,
    required this.currentLanguage,
    required this.onLanguageChanged,
    required this.languages,
    required this.showOnlyIcon,
  });

  @override
  State<LanguageSelector> createState() => _LanguageSelectorState();
}

class _LanguageSelectorState extends State<LanguageSelector> {
  @override
  Widget build(BuildContext context) {
    final currentLangData = widget.languages.firstWhere(
      (lang) => lang['code'] == widget.currentLanguage,
      orElse: () => {'code': 'en', 'name': 'English'},
    );

    if (widget.showOnlyIcon) {
      return InkWell(
          onTap: () {
            final newLanguage = widget.currentLanguage == 'ar' ? 'en' : 'ar';
            widget.onLanguageChanged(newLanguage);
          },
          child: Utils.buildSvgWithAssets(icon: AppAssets.globe_black));
    } else {
      return Stack(clipBehavior: Clip.none, children: [
        Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular((10.r)),
            color: AppColors.secondaryLightRedColor,
          ),
          child: InkWell(
            onTap: () {
              final newLanguage = widget.currentLanguage == 'ar' ? 'en' : 'ar';
              widget.onLanguageChanged(newLanguage);
            },
            child: Container(
              padding: EdgeInsets.all(8.h),
              decoration: BoxDecoration(borderRadius: BorderRadius.circular((12.r))),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [

                  Text(
                    // currentLangData['name']?.toUpperCase() ?? 'EN',
                    widget.currentLanguage == 'ar' ? "ENGLISH" : "العربية",
                    style: context.dynamicTextStyle(
                        fontWeight: FontWeight.w600,
                      fontSize: 14.f,
                      color: AppColors.primaryRedColor,
                      letterSpacing: 0.1,
                      isLanguageSwitcher: true,
                        fontFamily: widget.currentLanguage == 'ar' ? "Poppins" : "CairoArabic"),
                  ),
                  SizedBox(width: 6.h),
                  Utils.buildSvgWithAssets(icon: AppAssets.language),

                ],
              ),
            ),
          ),
        ),
      ]);
    }
  }
}
