package net.linuxdemon.demoncraft.util.world;

import net.linuxdemon.demoncraft.handler.ConfigurationHandler;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GetOreVein
{

	public GetOreVein ()
	{

	}

	public static ArrayList<ArrayList<Integer>> fromBlock ( World world, int x, int y, int z, boolean enableDiag )
	{
		List<Block> blockBlacklist = Arrays.asList( Blocks.air, Blocks.stone, Blocks.grass, Blocks.dirt );
		Block blockType = world.getBlock( x, y, z );
		ArrayList<ArrayList<Integer>> veinList = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> coords = new ArrayList<Integer>();
		coords.add( x );
		coords.add( y );
		coords.add( z );
		veinList.add( coords );

		if ( blockBlacklist.contains( blockType ) )
		{
			return null;
		}
		if ( enableDiag )
		{
			List<Integer> modifierList = Arrays.asList( - 1, 0, 1 );
			for ( int i = 0; i < veinList.size(); i++ )
			{
				for ( int xListPos = 0; xListPos < modifierList.size(); xListPos++ )
				{
					for ( int yListPos = 0; yListPos < modifierList.size(); yListPos++ )
					{
						for ( int zListPos = 0; zListPos < modifierList.size(); zListPos++ )
						{
							int newX = veinList.get( i ).get( 0 ) + ( modifierList.get( xListPos ) );
							int newY = veinList.get( i ).get( 1 ) + ( modifierList.get( yListPos ) );
							int newZ = veinList.get( i ).get( 2 ) + ( modifierList.get( zListPos ) );
							coords = new ArrayList<Integer>();
							coords.add( newX );
							coords.add( newY );
							coords.add( newZ );
							if ( world.getBlock( newX, newY, newZ ).equals( blockType ) && ! veinList.contains( coords ) )
							{
								veinList.add( coords );
								if ( veinList.size() > ConfigurationHandler.magnetBlockLimit )
								{
									return veinList;
								}
							}
						}
					}
				}
			}
		}
		else
		{
			for ( int i = 0; i < veinList.size(); i++ )
			{
				for ( int side = 0; side < 6; side++ )
				{
					int newX;
					int newY;
					int newZ;
					if ( side == 0 )
					{
						newX = x + 1;
						newY = y;
						newZ = z;
					}
					else if ( side == 1 )
					{
						newX = x - 1;
						newY = y;
						newZ = z;
					}
					else if ( side == 2 )
					{
						newX = x;
						newY = y + 1;
						newZ = z;
					}
					else if ( side == 3 )
					{
						newX = x;
						newY = y - 1;
						newZ = z;
					}
					else if ( side == 4 )
					{
						newX = x;
						newY = y;
						newZ = z + 1;
					}
					else if ( side == 5 )
					{
						newX = x;
						newY = y;
						newZ = z - 1;
					}
					else
					{
						newX = x;
						newY = y;
						newZ = z;
					}
					coords = new ArrayList<Integer>();
					coords.add( newX );
					coords.add( newY );
					coords.add( newZ );
					if ( world.getBlock( newX, newY, newZ ).equals( blockType ) && ! veinList.contains( coords ) )
					{
						veinList.add( coords );
						if ( veinList.size() > ConfigurationHandler.magnetBlockLimit )
						{
							return veinList;
						}
					}
				}
			}
		}
		return veinList;
	}

}
