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

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

class LocaleController extends GetxController {
  Locale? language;
  String countryCode = '';

  ThemeData get appTheme {
    String lang = box.read(BoxName.lang) ?? 'en';
    bool isDarkMode = box.read('isDarkMode') ?? false;
    return _getThemeFor(lang, isDarkMode);
  }

  ThemeData get lightTheme {
    String lang = box.read(BoxName.lang) ?? 'en';
    return _getThemeFor(lang, false);
  }

  ThemeData get darkTheme {
    String lang = box.read(BoxName.lang) ?? 'en';
    return _getThemeFor(lang, true);
  }

  ThemeData _getThemeFor(String lang, bool isDarkMode) {
    if (lang.startsWith('ar')) {
      return isDarkMode ? darkThemeArabic : lightThemeArabic;
    } else {
      return isDarkMode ? darkThemeEnglish : lightThemeEnglish;
    }
  }

  void refreshTheme() {
    update();
  }

  void changeLang(String langcode) {
    Locale locale = Locale(langcode);
    box.write(BoxName.lang, langcode);
    Get.updateLocale(locale);
    update();
  }


  @override
  void onInit() {
    String? storedLang = box.read(BoxName.lang);
    if (storedLang == null) {
      storedLang = Get.deviceLocale!.languageCode;
      box.write(BoxName.lang, storedLang);
    }
    language = Locale(storedLang);
    super.onInit();
  }
}

