/*
 *   Author: Florian Fink, 2014
 *   Based on Code by: veqryn
 *   
 *   This project is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
 *
 *   This project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along with this project. If not, see http://www.gnu.org/licenses/.
 */
package usergames.othello.othelloDelegate;

import java.util.Iterator;

import games.strategy.common.delegate.AbstractDelegate;
import games.strategy.engine.data.PlayerID;
import games.strategy.engine.data.Unit;
import games.strategy.engine.message.IRemote;
import games.strategy.grid.ui.display.IGridGameDisplay;

/**
 * The delegate that is run at the end of each players turn to check for a game over
 */
public class EndDelegate extends AbstractDelegate {

	@Override
	public Class<? extends IRemote> getRemoteType() {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public void start()
	{
		super.start();
		
		//check if opponent has to pass
		PlayerID opponent=this.m_player;
		//get opponent
		Iterator<PlayerID> it=this.m_bridge.getData().getPlayerList().getPlayers().iterator();
		while(opponent.equals(this.m_player))
			opponent=it.next();
		//can they move?
		boolean opponentWillPass=PlayDelegate.getAllPossibleMoves(opponent, m_bridge.getData()).isEmpty();
		
		//if opponent has to pass check if we can move after that
		if(opponentWillPass){
			if(PlayDelegate.getAllPossibleMoves(m_player, m_bridge.getData()).isEmpty()){ //the game is over
				//find out who wins
				int ourScore=getStoneCount(m_player);
				int theirScore=getStoneCount(opponent);
				
				if (ourScore>theirScore)
				{
					signalGameOver(m_player.getName() + " wins!");
				}
				else if(ourScore<theirScore){
					signalGameOver(opponent.getName() + " wins!");
				}
				else
				{
					signalGameOver("Game Is A Draw!");
				}
			}
		}
	}
	
	@Override
	public boolean delegateCurrentlyRequiresUserInput() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * Notify all players that the game is over.
	 * 
	 * @param status
	 *            the "game over" text to be displayed to each user.
	 */
	private void signalGameOver(final String status)
	{
		// If the game is over, we need to be able to alert all UIs to that fact.
		// The display object can send a message to all UIs.
		m_bridge.getHistoryWriter().startEvent(status);
		final IGridGameDisplay display = (IGridGameDisplay) m_bridge.getDisplayChannelBroadcaster();
		display.setStatus(status);
		display.setGameOver();
		m_bridge.stopGameSequence();
	}
	
	private int getStoneCount(PlayerID player){
		int i=0;
		
		for(Unit u:m_bridge.getData().getUnits()){
			if(u.getOwner().equals(player))
				i++;
		}
		
		return i;
	}

}
