package ncrashed.warp.cores.actions;

import java.util.List;
import java.util.ListIterator;

import ncrashed.warp.IntVec3;
import ncrashed.warp.api.ICoreAction;
import ncrashed.warp.api.IWarpBeacon;
import ncrashed.warp.api.IWarpCore;
import ncrashed.warp.cores.SimpleWarpBeacon;
import net.minecraftforge.common.ForgeDirection;

public class BeaconSummon extends BaseBeaconAction implements ICoreAction 
{
	private boolean isNoBeacon = false;
	
	@Override
	public int getRequiredEnergy(IWarpCore core, int multCounter, int north,
			int south, int west, int east, int up, int down) 
	{
		IWarpBeacon beacon = new SimpleWarpBeacon();
		List<IntVec3> list = getBeaconList(core.getCoreWorld(), beacon);
		ListIterator<IntVec3> iter = list.listIterator();
		int players = 0;
		while(iter.hasNext())
		{
			IntVec3 pos = iter.next();
			if(beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.NORTH) == north &&
					beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.SOUTH) == south &&
					beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.WEST) == west &&
					beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.EAST) == east)
			{
				players++;
			}
		}
		
		return players*90000;
	}

	@Override
	public boolean performAction(IWarpCore core, int multCounter, int north,
			int south, int west, int east, int up, int down) 
	{
		boolean isNoBeacon = false;
		
		int dx = 0;
		int dy = 2;
		int dz = 0;

		IWarpBeacon beacon = new SimpleWarpBeacon();
		List<BeaconTemp> list = getBeaconListWithPlayers(core.getCoreWorld(), beacon);
		ListIterator<BeaconTemp> iter = list.listIterator();
		int players = 0;
		boolean flag = false;
		while(iter.hasNext())
		{
			BeaconTemp temp = iter.next();
			IntVec3 pos = temp.vec;
			System.out.println(beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.NORTH)+" "+north);
			System.out.println(beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.SOUTH)+" "+south);
			System.out.println(beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.WEST)+" "+west);
			System.out.println(beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.EAST)+" "+east);
			
			if(beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.NORTH) == north &&
					beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.SOUTH) == south &&
					beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.WEST) == west &&
					beacon.getCounterValue(core.getCoreWorld(), pos.x, pos.y, pos.z, ForgeDirection.EAST) == east)
			{
				temp.player.playerNetServerHandler.setPlayerLocation(core.getCoreX()+dx, core.getCoreY()+dy, core.getCoreZ()+dz, temp.player.rotationYaw, temp.player.rotationPitch);
				flag = true;
			}
		}
		
		if(!flag) isNoBeacon = true;
		return flag;
	}

	@Override
	public int getLowMultCounter() 
	{
		return 16;
	}

	@Override
	public int getHighMultCounter() 
	{
		return 16;
	}

	@Override
	public int getSuccessCooldown(IWarpCore core, int multCounter, int north,
			int south, int west, int east, int up, int down) 
	{
		return 5;
	}

	@Override
	public int getFailCooldown(IWarpCore core, int multCounter, int north,
			int south, int west, int east, int up, int down) 
	{
		return 5;
	}

	@Override
	public int getMinimumCooldown(IWarpCore core, int multCounter, int north,
			int south, int west, int east, int up, int down) 
	{
		return 0;
	}

	@Override
	public boolean printActionResult() 
	{
		return true;
	}

	@Override
	public int getErrorBit() 
	{
		if(isNoBeacon) return 1;
		return 2;
	}

}
