package net.minecraft.block.state;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPistonBase;
import net.minecraft.block.material.Material;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;

public class BlockPistonStructureHelper
{
    private final World field_177261_a;
    private final BlockPos field_177259_b;
    private final BlockPos field_177260_c;
    private final EnumFacing field_177257_d;
    private final List field_177258_e = Lists.newArrayList();
    private final List field_177256_f = Lists.newArrayList();
    private static final String __OBFID = "CL_00002033";

    public BlockPistonStructureHelper(World worldIn, BlockPos p_i45664_2_, EnumFacing p_i45664_3_, boolean p_i45664_4_)
    {
        this.field_177261_a = worldIn;
        this.field_177259_b = p_i45664_2_;

        if (p_i45664_4_)
        {
            this.field_177257_d = p_i45664_3_;
            this.field_177260_c = p_i45664_2_.offset(p_i45664_3_);
        }
        else
        {
            this.field_177257_d = p_i45664_3_.getOpposite();
            this.field_177260_c = p_i45664_2_.offset(p_i45664_3_, 2);
        }
    }

    public boolean func_177253_a()
    {
        this.field_177258_e.clear();
        this.field_177256_f.clear();
        Block block = this.field_177261_a.getBlockState(this.field_177260_c).getBlock();

        if (!BlockPistonBase.func_180696_a(block, this.field_177261_a, this.field_177260_c, this.field_177257_d, false))
        {
            if (block.getMobilityFlag() != 1)
            {
                return false;
            }
            else
            {
                this.field_177256_f.add(this.field_177260_c);
                return true;
            }
        }
        else if (!this.func_177251_a(this.field_177260_c))
        {
            return false;
        }
        else
        {
            for (int i = 0; i < this.field_177258_e.size(); ++i)
            {
                BlockPos blockpos = (BlockPos)this.field_177258_e.get(i);

                if (this.field_177261_a.getBlockState(blockpos).getBlock() == Blocks.slime_block && !this.func_177250_b(blockpos))
                {
                    return false;
                }
            }

            return true;
        }
    }

    private boolean func_177251_a(BlockPos p_177251_1_)
    {
        Block block = this.field_177261_a.getBlockState(p_177251_1_).getBlock();

        if (block.isAir(field_177261_a, p_177251_1_))
        {
            return true;
        }
        else if (!BlockPistonBase.func_180696_a(block, this.field_177261_a, p_177251_1_, this.field_177257_d, false))
        {
            return true;
        }
        else if (p_177251_1_.equals(this.field_177259_b))
        {
            return true;
        }
        else if (this.field_177258_e.contains(p_177251_1_))
        {
            return true;
        }
        else
        {
            int i = 1;

            if (i + this.field_177258_e.size() > 12)
            {
                return false;
            }
            else
            {
                while (block == Blocks.slime_block)
                {
                    BlockPos blockpos1 = p_177251_1_.offset(this.field_177257_d.getOpposite(), i);
                    block = this.field_177261_a.getBlockState(blockpos1).getBlock();

                    if (block.isAir(field_177261_a, blockpos1)|| !BlockPistonBase.func_180696_a(block, this.field_177261_a, blockpos1, this.field_177257_d, false) || blockpos1.equals(this.field_177259_b))
                    {
                        break;
                    }

                    ++i;

                    if (i + this.field_177258_e.size() > 12)
                    {
                        return false;
                    }
                }

                int i1 = 0;
                int j;

                for (j = i - 1; j >= 0; --j)
                {
                    this.field_177258_e.add(p_177251_1_.offset(this.field_177257_d.getOpposite(), j));
                    ++i1;
                }

                j = 1;

                while (true)
                {
                    BlockPos blockpos2 = p_177251_1_.offset(this.field_177257_d, j);
                    int k = this.field_177258_e.indexOf(blockpos2);

                    if (k > -1)
                    {
                        this.func_177255_a(i1, k);

                        for (int l = 0; l <= k + i1; ++l)
                        {
                            BlockPos blockpos3 = (BlockPos)this.field_177258_e.get(l);

                            if (this.field_177261_a.getBlockState(blockpos3).getBlock() == Blocks.slime_block && !this.func_177250_b(blockpos3))
                            {
                                return false;
                            }
                        }

                        return true;
                    }

                    block = this.field_177261_a.getBlockState(blockpos2).getBlock();

                    if (block.isAir(field_177261_a, blockpos2))
                    {
                        return true;
                    }

                    if (!BlockPistonBase.func_180696_a(block, this.field_177261_a, blockpos2, this.field_177257_d, true) || blockpos2.equals(this.field_177259_b))
                    {
                        return false;
                    }

                    if (block.getMobilityFlag() == 1)
                    {
                        this.field_177256_f.add(blockpos2);
                        return true;
                    }

                    if (this.field_177258_e.size() >= 12)
                    {
                        return false;
                    }

                    this.field_177258_e.add(blockpos2);
                    ++i1;
                    ++j;
                }
            }
        }
    }

    private void func_177255_a(int p_177255_1_, int p_177255_2_)
    {
        ArrayList arraylist = Lists.newArrayList();
        ArrayList arraylist1 = Lists.newArrayList();
        ArrayList arraylist2 = Lists.newArrayList();
        arraylist.addAll(this.field_177258_e.subList(0, p_177255_2_));
        arraylist1.addAll(this.field_177258_e.subList(this.field_177258_e.size() - p_177255_1_, this.field_177258_e.size()));
        arraylist2.addAll(this.field_177258_e.subList(p_177255_2_, this.field_177258_e.size() - p_177255_1_));
        this.field_177258_e.clear();
        this.field_177258_e.addAll(arraylist);
        this.field_177258_e.addAll(arraylist1);
        this.field_177258_e.addAll(arraylist2);
    }

    private boolean func_177250_b(BlockPos p_177250_1_)
    {
        EnumFacing[] aenumfacing = EnumFacing.values();
        int i = aenumfacing.length;

        for (int j = 0; j < i; ++j)
        {
            EnumFacing enumfacing = aenumfacing[j];

            if (enumfacing.getAxis() != this.field_177257_d.getAxis() && !this.func_177251_a(p_177250_1_.offset(enumfacing)))
            {
                return false;
            }
        }

        return true;
    }

    public List func_177254_c()
    {
        return this.field_177258_e;
    }

    public List func_177252_d()
    {
        return this.field_177256_f;
    }
}