import 'dart:ui' as ui;

import 'package:flutter/material.dart';

import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';

class LocalizationManager with ChangeNotifier {
  Locale? _locale;

  Locale? get locale => _locale;

  LocalizationManager() {
    initializeLanguageSettings();
  }

  initializeLanguageSettings() {
    String language = DataPersistence.getLang();
    if (language.isEmpty) {
      Locale systemLocale = ui.window.locale;
      if (systemLocale.languageCode.startsWith("ar")) {
        _locale = Locale('ar', '');
      } else if (systemLocale.languageCode == "en") {
        _locale = Locale('en', '');
      } else if (systemLocale.languageCode == "tr") {
        _locale = Locale('tr', '');
      } else if (systemLocale.languageCode == "bn") {
        _locale = Locale('bn', '');
      } else {
        _locale = Locale('en', '');
      }
    } else {
      if (language.startsWith("ar")) {
        _locale = Locale('ar', '');
      } else if (language == "tr") {
        _locale = Locale('tr', '');
      } else if (language == "bn") {
        _locale = Locale('bn', '');
      } else {
        _locale = Locale('en', '');
      }
    }
    if (_locale != null) {
      changeAppLanguage(_locale!);
    }
  }

  void changeAppLanguage(Locale locale) {
    _locale = locale;
    SCGlobalConfig.lang = locale.languageCode;
    DataPersistence.setLang(locale.languageCode);
    notifyListeners();
  }
}
