const API_BASE = process.env.REACT_APP_API_BASE;

export const getStatus = async () => {
  try {
    const res = await fetch(`${API_BASE}/health`);
    if (!res.ok) {
      throw Error(res.statusText);
    }
    return await res.json();
  } catch (err) {
    return { status: err.message, version: null };
  }
};

export const checkScore = async (point1, point2) => {
  const res = await fetch(`${API_BASE}/score`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ point1, point2 }),
  });
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const getGenerators = async () => {
  const res = await fetch(`${API_BASE}/generators`);
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const createGame = async (
  timer,
  rounds,
  countryLock,
  generationMethod,
  ruleSet
) => {
  const res = await fetch(`${API_BASE}/game`, {
    method: "PUT",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      timer,
      rounds,
      countryLock,
      generationMethod,
      ruleSet,
    }),
  });
  if (!res.ok) {
    throw Error(res.statusText);
  }
  const { gameId } = await res.json();
  return gameId;
};

export const getGameConfig = async gameId => {
  const res = await fetch(`${API_BASE}/game/${gameId}/config`);
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const getGameCoords = async gameId => {
  const res = await fetch(`${API_BASE}/game/${gameId}/coords`);
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const getPlayers = async gameId => {
  const res = await fetch(`${API_BASE}/game/${gameId}/players`);
  if (!res.ok) {
    throw Error(res.statusText);
  }
  const { players } = await res.json();
  return players;
};

export const getLinkedGame = async gameId => {
  const res = await fetch(`${API_BASE}/game/${gameId}/linked`);
  if (!res.ok) {
    throw Error(res.statusText);
  }
  const { linkedGame } = await res.json();
  return linkedGame;
};

export const linkGame = async (gameId, linkedGame) => {
  const res = await fetch(`${API_BASE}/game/${gameId}/linked`, {
    method: "PUT",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ linkedGame }),
  });
  if (!res.ok) {
    throw Error(res.statusText);
  }
};

export const getFirstSubmitter = async (gameId, round) => {
  const res = await fetch(`${API_BASE}/game/${gameId}/round/${round}/first`);
  if (!res.ok) {
    return null; // API is that 404 means no submitter yet (or wrong game)
  }
  const { first } = await res.json();
  return first;
};

export const joinGame = async (gameId, playerName) => {
  const res = await fetch(`${API_BASE}/game/${gameId}/join`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ playerName }),
  });
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const getCurrentRound = async (gameId, playerId) => {
  const res = await fetch(
    `${API_BASE}/game/${gameId}/players/${playerId}/current`
  );
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const sendGuess = async (
  gameId,
  playerId,
  round,
  point,
  timeRemaining
) => {
  const res = await fetch(
    `${API_BASE}/game/${gameId}/round/${round}/guess/${playerId}`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ timeRemaining, ...point }),
    }
  );
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};

export const sendTimeout = async (gameId, playerId, round) => {
  const res = await fetch(
    `${API_BASE}/game/${gameId}/round/${round}/timeout/${playerId}`,
    { method: "POST" }
  );
  if (!res.ok) {
    throw Error(res.statusText);
  }
  return res.json();
};
