import app from '../../app.json';
import I18n from 'react-native-i18n';
import en from './locales/en';
import es from './locales/es';
import de from './locales/de';
import fr from './locales/fr';
import ja from './locales/ja';
import km from './locales/km';
import ko from './locales/ko';
import my from './locales/my';
import th from './locales/th';
import vi from './locales/vi';
import zh from './locales/zh';
import tw from './locales/zh-TW';
import { getStorage } from '../utils/storage';

I18n.fallbacks = true;
//I18n.l("currency", 630, {})
//I18n.t("", {})
const SETTING_KEY = "locale-settings";
const DEBUG = app.debug && !app.product;
const LOCALES = [{
  name: en.name,
  value: "en",//英语
  enable: true
}, {
  name: de.name,
  value: "de",//德语
  enable: DEBUG
}, {
  name: es.name,
  value: "es",//西语
  enable: DEBUG
}, {
  name: fr.name,
  value: "fr",//法语
  enable: DEBUG
}, {
  name: ja.name,
  value: "ja",//日语
  enable: DEBUG
}, {
  name: ko.name,
  value: "ko",//韩语
  enable: DEBUG
}, {
  name: km.name,
  value: "km",//柬埔寨-高棉语
  enable: DEBUG
}, {
  name: my.name,
  value: "my",//缅甸语
  enable: DEBUG
}, {
  name: th.name,
  value: "th",//泰语
  enable: DEBUG
}, {
  name: vi.name,
  value: "vi",//越南语
  enable: DEBUG
}, {
  name: zh.name,
  value: "zh-CN",//简体中文
  enable: true
}, {
  name: tw.name,
  value: "zh-HK",//繁體中文
  enable: true
}]
if (app.modules.i18n) {
  I18n.translations = {
    default: en,
    en, es, de, fr,
    ja, km, ko, my,
    th, vi, zh, 
    "zh-CN": zh,
    "zh-HK": tw
  }

  global.defaultLocale = I18n.currentLocale();
  if (app.debug)
    console.log("[I18n] defaultLocal", global.defaultLocale);
} else {
  I18n.translations = {
    default: en,
    en
  }
  global.defaultLocale = "en";
}
const getLocaleName = () => {
  var locale = ""
  for (let l of LOCALES) {
    if (l.value == defaultLocale) {
      locale = l.value;
      break;
    }
  }
  if (!locale) {
    if (defaultLocale.indexOf("zh") >= 0) {
      if (defaultLocale.indexOf("CN") >= 0) {
        locale = "zh-CN"
      } else {
        locale = "zh-HK"
      }
    } else {
      for (let l of LOCALES) {
        if (defaultLocale.indexOf(l.value) >= 0) {
          locale = l.value;
          break;
        }
      }
    }
    if (app.debug)
      console.log("[I18n] getLocaleName(2)", locale);
  } else {
    if (app.debug)
      console.log("[I18n] getLocaleName(1)", locale);
  }
  return locale;
}

global.$t = (scope) => {
  return I18n.t(scope);
}

const init = (back) =>  {
  if (app.modules.i18n) {
    getStorage(SETTING_KEY).then(res => {
      if (res) {
        global.currentLocale = I18n.locale = res;
      } else {
        global.currentLocale = getLocaleName();//"zh-CN"
        I18n.locale = global.currentLocale;//设置local
      }
      if (app.debug) console.log("[I18n] currentLocale", global.currentLocale, I18n.locale);
      back();
    })
  } else {
    global.currentLocale = "en";
    back();
  }
}


export const i18nUtil = {
  init: init,
  locales: LOCALES,
  SETTING_KEY: SETTING_KEY,
  getLocaleName: getLocaleName,
  isChinese: () => global.currentLocale.indexOf("zh") >= 0,
  getLocale: () => global.currentLocale,
  setlocale: (localeName) => {
    global.currentLocale = I18n.locale = localeName;
    global.ComCountryList = undefined;
  },
  getDefaultLocale: () => global.defaultLocale,
  analyzeLocaleData: (data, def) => {
    for (let l in data) {
      //console.log(l);
      if (currentLocale.indexOf(l) >= 0) {
        return data[l]
      }
    }
    return def ?? data;
  }
}