import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:resume/l10n/app_localizations.dart';

import '../providers/locale_provider.dart';

class LanguageDropdown extends StatefulWidget {
  const LanguageDropdown({super.key});

  @override
  State<LanguageDropdown> createState() => _LanguageDropdownState();
}

class _LanguageDropdownState extends State<LanguageDropdown> {
  @override
  Widget build(BuildContext context) {
    LocaleProvider localeProvider = Provider.of<LocaleProvider>(context);
    return DropdownButton<String>(
      value: localeProvider.locale.languageCode,
      items: [
        DropdownMenuItem(
          value: 'de',
          child: getMenuItem(
            AppLocalizations.of(context)!.german,
            'assets/de_icon.png',
          ),
        ),
        DropdownMenuItem(
          value: 'en',
          child: getMenuItem(
            AppLocalizations.of(context)!.english,
            'assets/gb_icon.png',
          ),
        ),
      ],
      onChanged: (value) {
        if (value == null) return;
        localeProvider.setLocale(Locale(value));
      },
    );
  }

  Widget getMenuItem(String label, String imagePath) {
    return Row(
      children: [
        Text(label),
        const Padding(padding: EdgeInsets.only(right: 8)),
        Image.asset(imagePath, width: 30),
      ],
    );
  }
}
