import { UnoSession, UnoCard, UnoPlayer, } from './UnoSession.js';
import * as logic from './UnoRules.js'
import * as Log from './../util/logging.js'

export function processUiEvents(game: UnoSession, player: UnoPlayer, command: any){
    switch(command.button){
        case 'ready':
            p_ready(game,player);
            break;
        case 'leave':
            p_leave(game,player);
            break;
        case 'play':
            p_play(game,player,command);
            break
        case 'double':
            p_double(game,player,command);
            break;
        case 'leave':
            p_leave(game,player);
            break;
        case 'pull':
            p_pull(game,player);
            break;
        case 'skip':
            p_skip(game,player);
            break;
        case 'uno':
            p_uno(game,player);
            break;
    }
}

function p_ready (game: UnoSession, player: UnoPlayer){
    if (game.started) {
        player.socket.send(JSON.stringify({"notify":"game already started"}))
    } else {
        player.ready = true;
        Log.logNotice("Player " + player.nickname + " (" + player.userId + ") is ready");
        game.unoChat.writeMessage(player.nickname + " ist bereit");
        game.checkReady();
        game.clientUpdatePlayer(player);
    }
}

function p_leave (game: UnoSession, player: UnoPlayer){
    if (game.started) {
        logic.leave(game,player)
    } else {
        //remove player
        const newPlayers = game.players.filter(el => el.userId != player.userId);
        Log.logNotice("Player " + player.nickname + " (" + player.userId + ") left in lobby");
        game.unoChat.writeMessage(player.nickname + " hat die Lobby verlassen");
        game.players = newPlayers;
        game.checkReady();
        player.socket.send(JSON.stringify({kick:"Spiel verlassen."}));
        game.clientRemovePlayer(player);
    }
}

function p_play(game: UnoSession, player: UnoPlayer,command:any) {
    if (!game.started) {
        player.socket.send(JSON.stringify({"notify":"game not started"}))
    } else {
        let ret = logic.playCard(game, player, command.card)
        player.socket.send(JSON.stringify({
            notify: ret[1],
            warning: ret[0],
        }))
        game.clientUpdatePlayer(player);
    }
}

function p_double(game: UnoSession, player: UnoPlayer,command:any) {
    if (!game.started) {
        player.socket.send(JSON.stringify({"notify":"game not started"}))
    } else {
        let ret = logic.playCard(game, player, command.card[0], command.card[1])
        player.socket.send(JSON.stringify({
            notify: ret[1],
            warning: ret[0],
        }))
        game.clientUpdatePlayer(player);
    }
}

function p_pull(game: UnoSession, player: UnoPlayer) {
    if (!game.started) {
        player.socket.send(JSON.stringify({"notify":"game not started"}))
    } else {
        let ret = logic.pull(game, player)
        player.socket.send(JSON.stringify({
            notify: ret[1],
            warning: ret[0],
        }))
        game.clientUpdatePlayer(player);
    }
    

}

function p_skip(game: UnoSession, player: UnoPlayer) {
    if (!game.started) {
        player.socket.send(JSON.stringify({"notify":"game not started"}))
    } else {
        let ret = logic.skip(game, player)
        player.socket.send(JSON.stringify({
            notify: ret[1],
            warning: ret[0],
        }))
        
    }
}

function p_uno(game: UnoSession, player: UnoPlayer) {
    if (!game.started) {
        player.socket.send(JSON.stringify({"notify":"game not started"}))
    } else {
        let ret = logic.uno(game, player)
        player.socket.send(JSON.stringify({
            notify: ret[1],
            warning: ret[0],
        }))
        game.clientUpdatePlayer(player);
    }
}