import 'package:flutter/material.dart';
import '../cache/local_manager.dart';
import '../constant/enums/local_keys_enums.dart';
import '../constant/lang/language_constants.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class LanguageServices {
  Future<Locale> setLocale(String languageCode) async {
    await LocaleManager.prefrencesInit();
    LocaleManager.instance
        .setStringValue(PreferencesKeys.LANGUAGE_CODE, languageCode);
    return _locale(languageCode);
  }

  Future<Locale> getLocale() async {
    await LocaleManager.prefrencesInit();
    String languageCode =
        LocaleManager.instance.getStringValue(PreferencesKeys.LANGUAGE_CODE);
    return _locale(languageCode);
  }

  Locale _locale(String languageCode) {
    switch (languageCode) {
      case LanguageConstants.ENGLISH:
        return const Locale(LanguageConstants.ENGLISH, '');
      case LanguageConstants.VIETNAM:
        return const Locale(LanguageConstants.VIETNAM, '');
      default:
        return const Locale(LanguageConstants.VIETNAM, '');
    }
  }
}

AppLocalizations appLocalization(BuildContext context) {
  return AppLocalizations.of(context)!;
}
