// Run in <head>
// Loads Art Deco font from localStorage
// @TODO: Refactor to pemberly implmentation

var FONT_DATA = {};

FONT_DATA.FONT_VERSION = '20161013';
FONT_DATA.VERSION_KEY_LIST = {
  'ru' : ['artdeco_fonts_version_ru'],
  'th' : ['artdeco_fonts_version_th'],
  'ar' : ['artdeco_fonts_version_ar'],
  'default' : ['artdeco_fonts_version']
};
FONT_DATA.CACHE_KEY_LIST = {
  'ru' : ['artdeco_font_styles_ru'],
  'th' : ['artdeco_font_styles_th'],
  'ar' : ['artdeco_font_styles_ar'],
  'default' : ['artdeco_font_styles']
};

(function(){
  var locale = document.getElementsByClassName('js-font-script')[0].getAttribute('data-locale');

  FONT_DATA.VERSION_KEY = FONT_DATA.VERSION_KEY_LIST.hasOwnProperty(locale) ?
                          FONT_DATA.VERSION_KEY_LIST[locale] :
                          FONT_DATA.VERSION_KEY_LIST.default;
  FONT_DATA.CACHE_KEY = FONT_DATA.CACHE_KEY_LIST.hasOwnProperty(locale) ?
                        FONT_DATA.CACHE_KEY_LIST[locale] :
                        FONT_DATA.CACHE_KEY_LIST.default;

  var lastVersion = window.localStorage.getItem(FONT_DATA.VERSION_KEY);

  if (lastVersion && lastVersion === FONT_DATA.FONT_VERSION) {
    var style = document.createElement('style');
    style.textContent = window.localStorage.getItem(FONT_DATA.CACHE_KEY);
    document.head.appendChild(style);
  }
})();
