package net.minecraft.src;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.lax1dude.eaglercraft.EaglercraftRandom;

public class ComponentMineshaftRoom extends StructureComponent
{
    /** List of other Mineshaft components linked to this room. */
    private List roomsLinkedToTheRoom = new LinkedList();

    public ComponentMineshaftRoom() {}

    public ComponentMineshaftRoom(int par1, EaglercraftRandom par2Random, int par3, int par4)
    {
        super(par1);
        this.boundingBox = new StructureBoundingBox(par3, 50, par4, par3 + 7 + par2Random.nextInt(6), 54 + par2Random.nextInt(6), par4 + 7 + par2Random.nextInt(6));
    }

    /**
     * Initiates construction of the Structure Component picked, at the current Location of StructGen
     */
    public void buildComponent(StructureComponent par1StructureComponent, List par2List, EaglercraftRandom par3Random)
    {
        int var4 = this.getComponentType();
        int var6 = this.boundingBox.getYSize() - 3 - 1;

        if (var6 <= 0)
        {
            var6 = 1;
        }

        int var5;
        StructureComponent var7;
        StructureBoundingBox var8;

        for (var5 = 0; var5 < this.boundingBox.getXSize(); var5 += 4)
        {
            var5 += par3Random.nextInt(this.boundingBox.getXSize());

            if (var5 + 3 > this.boundingBox.getXSize())
            {
                break;
            }

            var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX + var5, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.minZ - 1, 2, var4);

            if (var7 != null)
            {
                var8 = var7.getBoundingBox();
                this.roomsLinkedToTheRoom.add(new StructureBoundingBox(var8.minX, var8.minY, this.boundingBox.minZ, var8.maxX, var8.maxY, this.boundingBox.minZ + 1));
            }
        }

        for (var5 = 0; var5 < this.boundingBox.getXSize(); var5 += 4)
        {
            var5 += par3Random.nextInt(this.boundingBox.getXSize());

            if (var5 + 3 > this.boundingBox.getXSize())
            {
                break;
            }

            var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX + var5, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.maxZ + 1, 0, var4);

            if (var7 != null)
            {
                var8 = var7.getBoundingBox();
                this.roomsLinkedToTheRoom.add(new StructureBoundingBox(var8.minX, var8.minY, this.boundingBox.maxZ - 1, var8.maxX, var8.maxY, this.boundingBox.maxZ));
            }
        }

        for (var5 = 0; var5 < this.boundingBox.getZSize(); var5 += 4)
        {
            var5 += par3Random.nextInt(this.boundingBox.getZSize());

            if (var5 + 3 > this.boundingBox.getZSize())
            {
                break;
            }

            var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.minX - 1, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.minZ + var5, 1, var4);

            if (var7 != null)
            {
                var8 = var7.getBoundingBox();
                this.roomsLinkedToTheRoom.add(new StructureBoundingBox(this.boundingBox.minX, var8.minY, var8.minZ, this.boundingBox.minX + 1, var8.maxY, var8.maxZ));
            }
        }

        for (var5 = 0; var5 < this.boundingBox.getZSize(); var5 += 4)
        {
            var5 += par3Random.nextInt(this.boundingBox.getZSize());

            if (var5 + 3 > this.boundingBox.getZSize())
            {
                break;
            }

            var7 = StructureMineshaftPieces.getNextComponent(par1StructureComponent, par2List, par3Random, this.boundingBox.maxX + 1, this.boundingBox.minY + par3Random.nextInt(var6) + 1, this.boundingBox.minZ + var5, 3, var4);

            if (var7 != null)
            {
                var8 = var7.getBoundingBox();
                this.roomsLinkedToTheRoom.add(new StructureBoundingBox(this.boundingBox.maxX - 1, var8.minY, var8.minZ, this.boundingBox.maxX, var8.maxY, var8.maxZ));
            }
        }
    }

    /**
     * second Part of Structure generating, this for example places Spiderwebs, Mob Spawners, it closes Mineshafts at
     * the end, it adds Fences...
     */
    public boolean addComponentParts(World par1World, EaglercraftRandom par2Random, StructureBoundingBox par3StructureBoundingBox)
    {
        if (this.isLiquidInStructureBoundingBox(par1World, par3StructureBoundingBox))
        {
            return false;
        }
        else
        {
            this.fillWithBlocks(par1World, par3StructureBoundingBox, this.boundingBox.minX, this.boundingBox.minY, this.boundingBox.minZ, this.boundingBox.maxX, this.boundingBox.minY, this.boundingBox.maxZ, Block.dirt.blockID, 0, true);
            this.fillWithBlocks(par1World, par3StructureBoundingBox, this.boundingBox.minX, this.boundingBox.minY + 1, this.boundingBox.minZ, this.boundingBox.maxX, Math.min(this.boundingBox.minY + 3, this.boundingBox.maxY), this.boundingBox.maxZ, 0, 0, false);
            Iterator var4 = this.roomsLinkedToTheRoom.iterator();

            while (var4.hasNext())
            {
                StructureBoundingBox var5 = (StructureBoundingBox)var4.next();
                this.fillWithBlocks(par1World, par3StructureBoundingBox, var5.minX, var5.maxY - 2, var5.minZ, var5.maxX, var5.maxY, var5.maxZ, 0, 0, false);
            }

            this.randomlyRareFillWithBlocks(par1World, par3StructureBoundingBox, this.boundingBox.minX, this.boundingBox.minY + 4, this.boundingBox.minZ, this.boundingBox.maxX, this.boundingBox.maxY, this.boundingBox.maxZ, 0, false);
            return true;
        }
    }

    protected void func_143012_a(NBTTagCompound par1NBTTagCompound)
    {
        NBTTagList var2 = new NBTTagList("Entrances");
        Iterator var3 = this.roomsLinkedToTheRoom.iterator();

        while (var3.hasNext())
        {
            StructureBoundingBox var4 = (StructureBoundingBox)var3.next();
            var2.appendTag(var4.func_143047_a(""));
        }

        par1NBTTagCompound.setTag("Entrances", var2);
    }

    protected void func_143011_b(NBTTagCompound par1NBTTagCompound)
    {
        NBTTagList var2 = par1NBTTagCompound.getTagList("Entrances");

        for (int var3 = 0; var3 < var2.tagCount(); ++var3)
        {
            this.roomsLinkedToTheRoom.add(new StructureBoundingBox(((NBTTagIntArray)var2.tagAt(var3)).intArray));
        }
    }
}
