
const userKey = 'user-key';
const areaCodeKey = 'area-code-key';
const mineListKey = 'mine-list-key';
const coinsKey = 'coin-key';
const drawCoinsKey = 'draw-coin-key';

// user info from now on
function storeUser(user: any = undefined) {
  if (user === undefined) {
    localStorage.removeItem(userKey);
  } else {
    localStorage.setItem(userKey, JSON.stringify(user));
  }
}

function loadUser() {
  try {
    return JSON.parse(localStorage.getItem(userKey) || '{}');
  } catch(e) {
    return {};
  }
}

const clearUser = storeUser;


// area code info from now on
function storeAreaCode(codes: any) {
  localStorage.setItem(areaCodeKey, JSON.stringify(codes));
}

function loadAreaCode() {
  try {
    return JSON.parse(localStorage.getItem(areaCodeKey) || 'null');
  } catch(e) {
    return null;
  }
}


// mine list info from now on
function storeMineList(list: any) {
  localStorage.setItem(mineListKey, JSON.stringify(list));
}

function loadMineList() {
  try {
    return JSON.parse(localStorage.getItem(mineListKey) || 'null');
  } catch (e) {
    return null;
  }
}

function clearMineList() {
  localStorage.removeItem(mineListKey);
}

// coin list info from now on
function storeCoins(coins: any) {
  localStorage.setItem(coinsKey, JSON.stringify(coins));
}

function loadCoins() {
  try {
    return JSON.parse(localStorage.getItem(coinsKey) || 'null');
  } catch (e) {
    return null;
  }
}

function clearCoins() {
  localStorage.removeItem(coinsKey);
}

// coin list info of draw page from now on
function storeDrawCoins(coins: any) {
  localStorage.setItem(drawCoinsKey, JSON.stringify(coins));
}

function loadDrawCoins() {
  try {
    return JSON.parse(localStorage.getItem(drawCoinsKey) || 'null');
  } catch (e) {
    return null;
  }
}

function clearDrawCoins() {
  localStorage.removeItem(drawCoinsKey);
}

export {
  storeUser,
  loadUser,
  clearUser,

  storeAreaCode,
  loadAreaCode,

  storeMineList,
  loadMineList,
  clearMineList,

  storeCoins,
  loadCoins,
  clearCoins,

  storeDrawCoins,
  loadDrawCoins,
  clearDrawCoins,
}
