import { useState, useEffect } from 'react';
import { getCurrentRound } from "../domain/apiMethods";
import { useGameId, usePlayerId } from '../domain/gameStore';

export default () => {
  const gameId = useGameId();
  const playerId = usePlayerId();
  const [finished, setFinished] = useState(false);
  const [roundInfo, setRoundInfo] = useState({currentRound: null, targetPoint: null, roundSeconds: null});

  useEffect(() => {
    const setup = async () => {
      const { currentRound, coord, timer } = await getCurrentRound(gameId, playerId);
      if (currentRound) {
        setRoundInfo({ currentRound, targetPoint: coord, roundSeconds: timer });
      } else {
        setFinished(true);
      }
    }
    setup();
  }, [gameId, playerId]);

  return [finished, roundInfo];
}