import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hmg_patient_app/uitl/font_utils.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';
          print(newLanguage);
          widget.onLanguageChanged(newLanguage);
        },
        child: SvgPicture.asset("assets/images/svg/globe_black.svg", width: 32, height: 32,),
      );
    } else {
      return Stack(clipBehavior: Clip.none, children: [
        Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12),
            color: Color(0xFFFEE9EA),
          ),
          child: InkWell(
            onTap: () {
              final newLanguage = widget.currentLanguage == 'ar' ? 'en' : 'ar';
              print(newLanguage);
              widget.onLanguageChanged(newLanguage);
            },
            child: Container(
              padding: EdgeInsets.all(8),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(12),
              ),
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SvgPicture.asset("assets/images/svg/language.svg", width: 24),
                  const SizedBox(width: 6),
                  Text(
                    currentLangData['name']?.toUpperCase() ?? 'EN',
                    style: context.dynamicTextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 14,
                      color: const Color(0xFFED1C2B),
                      letterSpacing: 0.1,
                      isLanguageSwitcher: true,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ]);
    }
  }
}
