package com.beecavegames.poker.handlers;

import com.beecavegames.GameResponse;
import com.beecavegames.bjc.handlers.GameAction;
import com.beecavegames.common.TableException;
import com.beecavegames.dispatch.Param;
import com.beecavegames.poker.PokerTable;
import com.beecavegames.poker.PokerTablePlayer;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ImBackHandler extends PokerActHandler {

	public GameResponse<PokerTable> handle(@Param("gameId") long gameId, @Param("rev") int revision) {
		return execute(revision, gameId, new GameAction<PokerTable>() {
			
			@Override
			public void doAction(long playerId, PokerTable game) throws TableException {
				PokerTablePlayer ptp = game.getPlayer(playerId);

				if(ptp != null){
					ptp.idleCount = 0;
					ptp.sitOut = false;
					ptp.keepAliveTime = game.clock.now();
					if(ptp.getBalance().isPositive()){
						ptp.hasLeft = false;
					}
				}else{
					log.warn("Player tried to come back but was already removed from the table.");
				}
			}
			
			@Override
			public String getName() {
				return "back";
			}
		});
	}
}
