import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../constant/box_name.dart';
import '../../main.dart';
import '../themes/themes.dart';

class LocaleController extends GetxController {
  Locale? language;
  String countryCode = '';
  void restartApp() {
    runApp(MyApp());
  }

  ThemeData appTheme = themeEnglish;

  changeLang(String langcode) {
    Locale locale;
    switch (langcode) {
      case "ar":
        locale = const Locale("ar");
        appTheme = themeArabic;
        box.write(BoxName.lang, 'ar');
        break;
      case "en":
        locale = const Locale("en");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'en');
        break;
      case "tr":
        locale = const Locale("tr");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'tr');
        break;
      case "fr":
        locale = const Locale("fr");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'fr');
        break;
      case "it":
        locale = const Locale("it");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'it');
        break;
      case "de":
        locale = const Locale("de");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'de');
        break;
      case "el":
        locale = const Locale("el");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'el');
        break;
      case "es":
        locale = const Locale("es");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'es');
        break;
      case "fa":
        locale = const Locale("fa");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'fa');
        break;
      case "zh":
        locale = const Locale("zh");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'zh');
        break;
      case "ru":
        locale = const Locale("ru");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'ru');
        break;
      case "hi":
        locale = const Locale("hi");
        appTheme = themeEnglish;
        box.write(BoxName.lang, 'hi');
        break;
      default:
        locale = Locale(Get.deviceLocale!.languageCode);
        box.write(BoxName.lang, Get.deviceLocale!.languageCode);
        appTheme = themeEnglish;
        break;
    }

    box.write(BoxName.lang, langcode);
    // box.write(BoxName.lang, langcode);
    Get.changeTheme(appTheme);
    Get.updateLocale(locale);
    restartApp();
    update();
  }

  @override
  void onInit() {
    String storedLang = box.read(BoxName.lang) ?? "";
    switch (storedLang) {
      case "ar":
        language = const Locale("ar");
        appTheme = themeArabic;
        break;
      case "en":
        language = const Locale("en");
        appTheme = themeEnglish;
        break;
      case "tr":
        language = const Locale("tr");
        appTheme = themeEnglish;
        break;
      case "fr":
        language = const Locale("fr");
        appTheme = themeEnglish;
        break;
      case "it":
        language = const Locale("it");
        appTheme = themeEnglish;
        break;
      case "de":
        language = const Locale("de");
        appTheme = themeEnglish;
        break;
      case "el":
        language = const Locale("el");
        appTheme = themeEnglish;
        break;
      case "es":
        language = const Locale("es");
        appTheme = themeEnglish;
        break;
      case "fa":
        language = const Locale("fa");
        appTheme = themeArabic;
        break;
      case "zh":
        language = const Locale("zh");
        appTheme = themeEnglish;
        break;
      case "ru":
        language = const Locale("ru");
        appTheme = themeEnglish;
        break;
      case "hi":
        language = const Locale("hi");
        appTheme = themeEnglish;
        break;
      default:
        language = Locale(Get.deviceLocale!.languageCode);
        // language = Locale(Get.deviceLocale!.languageCode);
        appTheme = themeEnglish;
        break;
    }

    super.onInit();
  }
}
