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(const 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;
      default:
        locale = Locale(Get.deviceLocale!.languageCode);
        box.write(BoxName.lang, 'en');
        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;
      default:
        language = Locale(Get.deviceLocale!.languageCode);
        appTheme = themeEnglish;
        break;
    }
    super.onInit();
  }
}
