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 = '';

  ThemeData appTheme = lightThemeEnglish;

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

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

  @override
  void onInit() {
    String? storedLang = box.read(BoxName.lang);
    if (storedLang == null) {
      // Use device language if no language is stored
      storedLang = Get.deviceLocale!.languageCode;
      box.write(BoxName.lang, storedLang);
    }

    changeLang(storedLang);
    super.onInit();
  }
}

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

//   ThemeData appTheme = themeEnglish;

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

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

//   @override
//   void onInit() {
//     String? storedLang = box.read(BoxName.lang);
//     if (storedLang == null) {
//       // Use device language if no language is stored
//       storedLang = Get.deviceLocale!.languageCode;
//       box.write(BoxName.lang, storedLang);
//     }

//     changeLang(storedLang);

//     super.onInit();
//   }
// }
