package JAKJ . RedstoneInMotion ;

public class TemplateCarriageEntity extends CarriageEntity
{
	public BlockRecordSet Pattern ;

	@Override
	public void ToggleSide ( int Side )
	{
		if ( Pattern == null )
		{
			AbsorbPattern ( ) ;

			Propagate ( ) ;
		}
	}

	public void AbsorbPattern ( )
	{
		Pattern = new BlockRecordSet ( ) ;

		BlockRecordSet BlocksChecked = new BlockRecordSet ( ) ;

		BlocksChecked . add ( new BlockRecord ( xCoord , yCoord , zCoord ) ) ;

		BlockRecordSet BlocksToCheck = new BlockRecordSet ( ) ;

		BlocksToCheck . add ( new BlockRecord ( xCoord , yCoord , zCoord ) ) ;

		while ( BlocksToCheck . size ( ) > 0 )
		{
			BlockRecord Record = BlocksToCheck . pollFirst ( ) ;

			for ( Directions Direction : Directions . values ( ) )
			{
				BlockRecord NextRecord = Record . NextInDirection ( Direction ) ;

				if ( ! BlocksChecked . add ( NextRecord ) )
				{
					continue ;
				}

				NextRecord . Identify ( this ) ;

				if ( NextRecord . Id == Blocks . Carriage . GetId ( ) )
				{
					if ( NextRecord . Meta == Carriage . Types . Template . ordinal ( ) )
					{
						Pattern . add ( new BlockRecord ( NextRecord ) ) ;

						BlocksToCheck . add ( NextRecord ) ;
					}
				}
			}
		}

		if ( Pattern . size ( ) == 0 )
		{
			Pattern = null ;

			return ;
		}

		for ( BlockRecord PatternBlock : Pattern )
		{
			WorldUtil . SetBlock ( worldObj , PatternBlock . X , PatternBlock . Y , PatternBlock . Z , 0 , 0 ) ;

			PatternBlock . X -= xCoord ;
			PatternBlock . Y -= yCoord ;
			PatternBlock . Z -= zCoord ;
		}

		for ( Directions Direction : Directions . values ( ) )
		{
			SideClosed [ Direction . ordinal ( ) ] = true ;
		}
	}

	@Override
	public void ReadFromNbt ( NbtCompound TagCompound , boolean FromServer )
	{
		super . ReadFromNbt ( TagCompound , FromServer ) ;

		if ( FromServer )
		{
			return ;
		}

		if ( TagCompound . hasKey ( "Pattern" ) )
		{
			Pattern = new BlockRecordSet ( ) ;

			for ( NbtCompound PatternBlockRecord : TagCompound . getTagList ( "Pattern" ) )
			{
				Pattern . add ( new BlockRecord ( PatternBlockRecord . getInteger ( "X" ) , PatternBlockRecord . getInteger ( "Y" ) , PatternBlockRecord . getInteger ( "Z" )) ) ;
			}
		}
	}

	@Override
	public void WriteToNbt ( NbtCompound TagCompound , boolean ToClient )
	{
		super . WriteToNbt ( TagCompound , ToClient ) ;

		if ( ToClient )
		{
			return ;
		}

		if ( Pattern != null )
		{
			NbtList PatternRecord = new NbtList ( ) ;

			for ( BlockRecord PatternBlock : Pattern )
			{
				NbtCompound PatternBlockRecord = new NbtCompound ( ) ;

				PatternBlockRecord . setInteger ( "X" , PatternBlock . X ) ;
				PatternBlockRecord . setInteger ( "Y" , PatternBlock . Y ) ;
				PatternBlockRecord . setInteger ( "Z" , PatternBlock . Z ) ;

				PatternRecord . appendTag ( PatternBlockRecord ) ;
			}

			TagCompound . setTag ( "Pattern" , PatternRecord ) ;
		}
	}

	@Override
	public void FillPackage ( CarriagePackage Package ) throws CarriageMotionException
	{
		if ( Pattern == null )
		{
			throw ( new CarriageMotionException ( "template carriage has not yet been patterned" ) ) ;
		}

		Package . AddBlock ( Package . AnchorRecord ) ;

		for ( BlockRecord PatternBlock : Pattern )
		{
			BlockRecord Record = new BlockRecord ( PatternBlock . X + xCoord , PatternBlock . Y + yCoord , PatternBlock . Z + zCoord ) ;

			if ( worldObj . isAirBlock ( Record . X , Record . Y , Record . Z ) )
			{
				continue ;
			}

			Record . Identify ( worldObj ) ;

			Package . AddBlock ( Record ) ;

			Package . AddPotentialObstruction ( Record . NextInDirection ( Package . MotionDirection ) ) ;
		}
	}
}
