const { getRoom, updateRoom } = require('../services/roomService');
const { sendToPlayersRolledNumber, sendWinner } = require('../socket/emits');
const { rollDice, isMoveValid } = require('./handlersFunctions');

module.exports = socket => {
    const req = socket.request;

    const handleMovePawn = async pawnId => {
        const room = await getRoom(req.session.roomId);
        if (room.winner) return;
        const pawn = room.getPawn(pawnId);
        if (isMoveValid(req.session, pawn, room)) {
            const newPositionOfMovedPawn = pawn.getPositionAfterMove(room.rolledNumber);
            room.changePositionOfPawn(pawn, newPositionOfMovedPawn);
            const beaten = room.beatPawns(newPositionOfMovedPawn, req.session.color);
            // If pawn killed any opponent pawns, attacker gets another turn.
            // Also a roll of 6 grants another turn.
            // Additionally, grant an extra turn when a pawn reaches its final home position.
            const FINAL_POSITIONS = { red: 73, blue: 79, green: 85, yellow: 91 };
            const reachedHome = FINAL_POSITIONS[pawn.color] === newPositionOfMovedPawn;
            const winner = room.getWinner();
            if ((beaten > 0 || room.rolledNumber === 6 || reachedHome) && !winner) {
                room.resetTurnForSamePlayer();
            } else {
                room.changeMovingPlayer();
            }
            // const winner = room.getWinner();
            if (winner) {
                room.endGame(winner);
                sendWinner(room._id.toString(), winner);
            }
            await updateRoom(room);
        }
    };

    const handleRollDice = async () => {
        const rolledNumber = rollDice();
        sendToPlayersRolledNumber(req.session.roomId, rolledNumber);
        const room = await updateRoom({ _id: req.session.roomId, rolledNumber: rolledNumber });
        const player = room.getPlayer(req.session.playerId);
        if (!player.canMove(room, rolledNumber)) {
            room.changeMovingPlayer();
            await updateRoom(room);
        }
    };

    socket.on('game:roll', handleRollDice);
    socket.on('game:move', handleMovePawn);
};
