var CONFIG = CONFIG || {};

/**
 * 実体参照文字、HTMLタグを含む文字列から、テキスト部分を抽出
 * - 実体参照文字を文字列に変換
 * - HTMLタグを除去
 *
 * @method toText
 * @param  {[type]} string [description]
 * @return {[type]}        [description]
 */
function toText(string) {
  var text;

  text = $('<textarea />').html(string).text();
  return text.replace(/(<([^>]+)>)/ig,"");
}

/**
 * 設定
 *
 * @param data
 */
function configure(data) {
  var dataType;

  // データ種別をセット
  dataType = _.get(data, 'data-type', '');
  if (!! dataType) {
    CONFIG.DATA_TYPE = dataType.replace(/\s/g, '_').toLowerCase();
  }
}

/**
 * localStorageが使用できるか
 */
function localStorageEnabled() {
  var ua = navigator.userAgent;

  // Microsoft Edge の場合 localStorage を使用しない
  if (ua.indexOf('Edge') !== -1) {
    return false;
  }

  return window.localStorage;
}

var DB = {};

//Chrome , Firefox
if (localStorageEnabled()) {
  DB = {
    /**
     * キーにプレフィックスを追加
     *
     * @param key
     * @returns {*}
     * @private
     */
    _makeKey: function(key) {
      if (!! _.get(CONFIG, 'DATA_TYPE')) {
        key = _.get(CONFIG, 'DATA_TYPE') + '_' + key;
      }

      return key;
    },

    set: function(key, obj) {
      key = DB._makeKey(key);

      localStorage.setItem(key, JSON.stringify(obj));
    },

    get: function(key) {
      key = DB._makeKey(key);

      if (localStorage.getItem(key) != undefined) {
        return JSON.parse(localStorage.getItem(key));
      }
      else {
        return null;
      }
    },

    remove: function(key) {
      key = DB._makeKey(key);

      localStorage.removeItem(key);
    },

    bulk_set: function(key) {
      this.set("support_search_tab", key.tab);
      this.set("support_search_models_ds", key.models.ds);
      this.set("support_search_models_mn", key.models.mn);
    },

    bulk_get: function() {
      return {
        search: {
          tab: this.get('support_search_tab'),
          models: {
            ds: this.get('support_search_models_ds'),
            mn: this.get('support_search_models_mn')
          }
        }
      };
    }
  };
}
else { //IE
  DB = {
    /**
     * キーにプレフィックスを追加
     *
     * @param key
     * @returns {*}
     * @private
     */
    _makeKey: function(key) {
      if (!! _.get(CONFIG, 'DATA_TYPE')) {
        key = _.get(CONFIG, 'DATA_TYPE') + '_' + key;
      }

      return key;
    },

    set: function(key, obj) {
      key = DB._makeKey(key);

      $.cookie(key, this.compress(JSON.stringify(obj)), {expires: 10800, path: '/'});
    },

    get: function(key) {
      key = DB._makeKey(key);

      if($.cookie(key) != undefined) {
          return JSON.parse(this.uncompress($.cookie(key)));
      }
      else {
      	return ;
      }
    },

    remove: function(key) {
      key = DB._makeKey(key);

      $.removeCookie(key);
    },

    bulk_set: function(key) {
      this.set("support_search_tab", key.tab);
      this.set("support_search_models_ds", key.models.ds);
      this.set("support_search_models_mn", key.models.mn);
    },

    bulk_get: function() {
      return {
        search: {
          tab: this.get('support_search_tab'),
          models: {
            ds: this.get('support_search_models_ds'),
            mn: this.get('support_search_models_mn')
          }
        }
      };
    },

    compress: function(str) {
          //utf8変換
          var data = utf16to8(str);

          //圧縮
          var out_stream = ZLIB.deflateInit({level:9});
          var encoding_text = out_stream.deflate(data);

          //base64encode
          encoding_text = base64encode(encoding_text);

          return encoding_text;
    },

    uncompress: function(str) {
          //base64decode
          decode_text = base64decode(str);

          //展開
          var in_stream = ZLIB.inflateInit();
          var decode_text = in_stream.inflate(decode_text);

          //utf16変換
          return utf8to16(decode_text);
    }
  };
}
