import { getCurrentRound } from "./apiMethods";

const localStorageGameId = "terrassumptions:gameId";
const localStoragePlayerName = "terrassumptions:playerName";
const localStoragePlayerId = "terrassumptions:playerId";

const localStorageTimer = "terrassumptions:timer";

const localStoragePanoLat = "terrassumptions:pano:lat";
const localStoragePanoLng = "terrassumptions:pano:lng";

const localStoragePanoHeading = "terrassumptions:pano:heading";
const localStoragePanoPitch = "terrassumptions:pano:pitch";

export const hasSavedGameInfo = async () => {
  const gameId = localStorage.getItem(localStorageGameId);
  if (gameId === null) {
    return false;
  }
  const playerName = localStorage.getItem(localStoragePlayerName);
  if (playerName === null) {
    return false;
  }
  const playerId = localStorage.getItem(localStoragePlayerId);
  if (playerId === null) {
    return false;
  }

  try {
    await getCurrentRound(gameId, playerId);
    return true;
  } catch (_) {
    return false;
  }
};

export const saveGameInfoToLocalStorage = (gameId, playerName, playerId) => {
  localStorage.setItem(localStorageGameId, gameId);
  localStorage.setItem(localStoragePlayerName, playerName);
  localStorage.setItem(localStoragePlayerId, playerId);
};

export const saveTimerToLocalStorage = timer => {
  localStorage.setItem(localStorageTimer, timer.toString());
};

export const savePanoPositionToLocalStorage = (lat, lng) => {
  localStorage.setItem(localStoragePanoLat, lat.toString());
  localStorage.setItem(localStoragePanoLng, lng.toString());
};

export const savePanoPovToLocalStorage = (heading, pitch) => {
  localStorage.setItem(localStoragePanoHeading, heading.toString());
  localStorage.setItem(localStoragePanoPitch, pitch.toString());
};

export const clearGameInfoFromLocalStorage = () => {
  localStorage.removeItem(localStorageGameId);
  localStorage.removeItem(localStoragePlayerName);
  localStorage.removeItem(localStoragePlayerId);
};

export const clearRoundInfoFromLocalStorage = () => {
  localStorage.removeItem(localStorageTimer);
  localStorage.removeItem(localStoragePanoLat);
  localStorage.removeItem(localStoragePanoLng);
  localStorage.removeItem(localStoragePanoHeading);
  localStorage.removeItem(localStoragePanoPitch);
};

const parseFloatFromStorage = key => {
  const val = localStorage.getItem(key);
  return val === null ? null : Number.parseFloat(val);
};

export const getInfoFromLocalStorage = () => {
  const timer = localStorage.getItem(localStorageTimer);
  const position = {
    lat: parseFloatFromStorage(localStoragePanoLat),
    lng: parseFloatFromStorage(localStoragePanoLng),
  };
  const pov = {
    heading: parseFloatFromStorage(localStoragePanoHeading),
    pitch: parseFloatFromStorage(localStoragePanoPitch),
  };

  return {
    gameId: localStorage.getItem(localStorageGameId),
    playerName: localStorage.getItem(localStoragePlayerName),
    playerId: localStorage.getItem(localStoragePlayerId),
    timer: timer !== null ? Number.parseInt(timer, 10) : null,
    position: position.lat !== null && position.lng !== null ? position : null,
    pov: pov.heading !== null && pov.pitch !== null ? pov : null,
  };
};
