const Dice = require("./Dice");
const Timer = require("./Timer");

class Room {
  constructor(state) {
    this.availableColors = ['red', 'green', 'yellow', 'blue'];
    this.players = [];
    this.state = state;
    this.dice = new Dice(20, this.onDiceFinish);
    this.timer = new Timer(30);
    this.timer.onEnd(this.nextPlayer);
  }

  addPlayer(player) {
    this.players.push(player);
  }

  getPlayersNames() {
    let names = [];
    for (const player of this.players)
      names.push(player.name);
    return names;
  }

  getPlayersColors() {
    let colors = [];
    for (const player of this.players)
      colors.push(player.color);
    return colors;
  }

  getPlayersStates() {
    let states = [];
    for (const player of this.players)
      states.push(player.state);
    return states;
  }

  hasPlayer(id) {
    if (id == undefined) return false;

    for (const player of this.players)
      if (player.id == id) return true;
    return false;
  }

  isFull() {
    return this.players.length == 4;
  }

  startGame() {
    this.state = 'game';
    this.currentPlayerIndex = 0;
    this.updateStates();
    this.timer.startCounting();
  }

  updateStates() {
    for (let i = 0; i < this.players.length; i++)
      this.players[i].state = 'game-waiting';
    this.players[this.currentPlayerIndex].state = 'game-dice';
  }

  nextPlayer = () => {
    this.currentPlayerIndex++;

    if (this.currentPlayerIndex == this.players.length)
      this.currentPlayerIndex = 0;
  
    this.updateStates();
    this.timer.startCounting();
  }

  getPositions() {
    let positions = {
      red: [],
      blue: [],
      green: [],
      yellow: [],
    };

    for (const player of this.players) {
      positions[player.color] = player.positions;
    }

    return positions;
  }

  getPlayerFromColor(color) {
    for (const player of this.players) {
      if (player.color == color)
        return player;
    }
  }

  setMoveableCountersIndicies() {
    const stride = this.dice.currentNumber;
    const player = this.players[this.currentPlayerIndex];
    this.moveableCountersIndicies = player.getMoveableCountersIndicies(stride);

    if (this.moveableCountersIndicies.length == 0)
      this.nextPlayer();
  }

  onDiceFinish = () => {
    this.setMoveableCountersIndicies();
  }

  captureCounter(position, color) {
    const allPositions = this.getPositions();

    let colorsToCheck = ['red', 'blue', 'green', 'yellow'];
    colorsToCheck.splice(colorsToCheck.indexOf(color), 1);
    
    for (const colorToCheck of colorsToCheck)
      for (let i = 0; i < allPositions[colorToCheck].length; i++) {
        const positionToCheck = allPositions[colorToCheck][i];
        if (positionToCheck.x == position.x && positionToCheck.y == position.y) {
          const player = this.getPlayerFromColor(colorToCheck)
          player.backToBase(i);
        }
      }
  }

  getWinPlayer() {
    for (const player of this.players) {
      if (player.win == true) {
        this.state = 'finished';
        this.setFinishedStateToPlayers();
        return { name: player.name, color: player.color };
      }
    }
    return
  }
  
  setFinishedStateToPlayers() {
    for (const player of this.players) {
      player.state = 'finished';
    }
  }
}

module.exports = Room;