import {EventEmitter} from 'events'
import Board from './Board'
import cuid from 'cuid'


export default class Game extends EventEmitter {

	constructor(board, turn) {
		super()
		// EventEmitter.call(this);
		this.players = []
		this.configure(board, turn)
	}

	configure(board, turn) {
		this.id = cuid()
		this.turn = turn || "X"
		this.board = board || new Board(3);	
		this.status = "waiting";
		this.player = {}
	}

	restart() {
		console.log('restart');
		this.configure()
		return this
	}

	detectEndOfGame() {
		let result = this.isEndOfGame()
		if(result) {
			this.status = "game over";
			if(result !== "draw") {
				this.player[result].emit('you win')
				this.player[result === "O" ? "X" : "O"].emit('you lost')
			}
			this.emit('game over', result);
			return true
		}
		return false
	}

	isEndOfGame() {
		let winner = this.detectWinner(); // If false then no winner
		if(winner)
			return winner
		else if(this.board.openPositions() === 0)
			return "draw";
		else 
			return false;
	}

	detectWinner() {
		for(let i = 0; i < this.board.size; i++) {

			let r = this.checkLine(...this.board.getRow(i));
			if(r)
				return r;

			let c = this.checkLine(...this.board.getColumn(i))
			if(c)
				return c;
		}

		let firstDiag = this.checkLine(...this.board.getFirstDiagonal())
		if(firstDiag)
			return firstDiag

		let secondDiag = this.checkLine(...this.board.getSecondDiagonal())
		if(secondDiag)
			return secondDiag
		
		return false;
	}

	checkLine(a, b, c) {
		if(a.getPiece() === "X" && b.getPiece() === "X" && c.getPiece() === "X")
			return "X";
		else if(a.getPiece() === "O" && b.getPiece() === "O" && c.getPiece() === "O")
			return "O";
		else
			return false;
	}

	addPlayer(player) {
		if(this.players.length == 2) {
			console.log('Game full');
			return false;
		}
		this.players.push(player)
	}
	
	start() {
		if(this.players.length < 2) {
			return false;
		}
		this.status = "started";

		let rand = Math.round(Math.random());
		this.player.X = this.players[rand];
		this.player.O = this.players[1-rand];
		this.player.X.on('make move', (x, y) => this.onPlayerPlayed(x, y, "X"))
		this.player.O.on('make move', (x, y) => this.onPlayerPlayed(x, y, "O"))
		this.requestAMove();
	}

	requestAMove() {
		this.player[this.turn].emit('play', this.board);
	}

	onPlayerPlayed(x, y, turn) {
		if(this.turn != turn) {
			console.log('Not in your turn')
			return false;
		}
		this.player[this.turn].emit('dont play');
		this.emit("player played", this.turn, x, y)
		this.board.setPiece(x, y, this.turn);
		this.nextTurn();
	}
	
	nextTurn() {
		// Stop the loop if the game is over
		if(this.detectEndOfGame())
			return false

		// Next turn
		this.turn = (this.turn === "X" ? "O" : "X");
		this.requestAMove();
	}

}

@@ -1022,677 +1022,42 @@
 %7B%0A%09%09
-for(let i = 0; i %3C this.board.size; i++) %7B%0A%0A%09%09%09let r = this.checkLine(...this.board.getRow(i));%0A%09%09%09if(r)%0A%09%09%09%09return r;%0A%0A%09%09%09let c = this.checkLine(...this.board.getColumn(i))%0A%09%09%09if(c)%0A%09%09%09%09return c;%0A%09%09%7D%0A%0A%09%09let firstDiag = this.checkLine(...this.board.getFirstDiagonal())%0A%09%09if(firstDiag)%0A%09%09%09return firstDiag%0A%0A%09%09let secondDiag = this.checkLine(...this.board.getSecondDiagonal())%0A%09%09if(secondDiag)%0A%09%09%09return secondDiag%0A%09%09%0A%09%09return false;%0A%09%7D%0A%0A%09checkLine(a, b, c) %7B%0A%09%09if(a.getPiece() === %22X%22 && b.getPiece() === %22X%22 && c.getPiece() === %22X%22)%0A%09%09%09return %22X%22;%0A%09%09else if(a.getPiece() === %22O%22 && b.getPiece() === %22O%22 && c.getPiece() === %22O%22)%0A%09%09%09return %22O%22;%0A%09%09else%0A%09%09%09return false;%0A%09%7D
+return this.board.findWinner()%0A%09%7D%0A
 %0A%0A%09a
@@ -1642,16 +1642,27 @@
 is.board
+, this.turn
 );%0A%09%7D%0A%0A%09

Call moved function