import 'package:easy_localization/easy_localization.dart' hide tr;
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:userfront/i18n.dart';

import '../i18n/locale_storage.dart';
import '../i18n/locale_utils.dart';

class LanguageSelector extends StatelessWidget {
  const LanguageSelector({super.key, this.compact = false});

  final bool compact;

  @override
  Widget build(BuildContext context) {
    final localization = EasyLocalization.of(context);
    final resolvedCurrent = normalizeLocaleCode(
      localization?.currentLocale?.languageCode,
    );
    final current = (resolvedCurrent == 'ko' || resolvedCurrent == 'en')
        ? resolvedCurrent
        : 'en';
    final items = [
      DropdownMenuItem(value: 'ko', child: Text(tr('ui.common.language_ko'))),
      DropdownMenuItem(
        value: 'en',
        child: Text(tr('ui.common.language_en', fallback: 'English')),
      ),
    ];

    final iconSize = compact ? 16.0 : 18.0;
    final dropdown = DropdownButtonHideUnderline(
      child: DropdownButton<String>(
        value: current,
        items: items,
        isDense: true,
        icon: Icon(Icons.arrow_drop_down, size: compact ? 18 : 20),
        onChanged: (value) async {
          if (value == null || value == current) {
            return;
          }
          LocaleStorage.write(value);
          if (localization != null) {
            await localization.setLocale(Locale(value));
          }
          if (!context.mounted) return;
          Uri uri;
          try {
            uri = GoRouterState.of(context).uri;
          } catch (_) {
            uri = Uri.base;
          }
          final target = buildLocalizedPath(value, uri);
          context.go(target);
        },
      ),
    );

    return Padding(
      padding: EdgeInsets.only(top: compact ? 0 : 2),
      child: ConstrainedBox(
        constraints: BoxConstraints(minHeight: compact ? 24 : 28),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(Icons.language, size: iconSize),
            const SizedBox(width: 6),
            dropdown,
          ],
        ),
      ),
    );
  }
}
