package net.minecraft.tileentity;

import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.block.BlockFlower;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class TileEntityBanner extends TileEntity
{
    private int baseColor;
    private NBTTagList field_175118_f;
    private boolean field_175119_g;
    private List field_175122_h;
    private List field_175123_i;
    private String field_175121_j;
    private static final String __OBFID = "CL_00002044";

    public void setItemValues(ItemStack p_175112_1_)
    {
        this.field_175118_f = null;

        if (p_175112_1_.hasTagCompound() && p_175112_1_.getTagCompound().hasKey("BlockEntityTag", 10))
        {
            NBTTagCompound nbttagcompound = p_175112_1_.getTagCompound().getCompoundTag("BlockEntityTag");

            if (nbttagcompound.hasKey("Patterns"))
            {
                this.field_175118_f = (NBTTagList)nbttagcompound.getTagList("Patterns", 10).copy();
            }

            if (nbttagcompound.hasKey("Base", 99))
            {
                this.baseColor = nbttagcompound.getInteger("Base");
            }
            else
            {
                this.baseColor = p_175112_1_.getMetadata() & 15;
            }
        }
        else
        {
            this.baseColor = p_175112_1_.getMetadata() & 15;
        }

        this.field_175122_h = null;
        this.field_175123_i = null;
        this.field_175121_j = "";
        this.field_175119_g = true;
    }

    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);
        compound.setInteger("Base", this.baseColor);

        if (this.field_175118_f != null)
        {
            compound.setTag("Patterns", this.field_175118_f);
        }
    }

    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        this.baseColor = compound.getInteger("Base");
        this.field_175118_f = compound.getTagList("Patterns", 10);
        this.field_175122_h = null;
        this.field_175123_i = null;
        this.field_175121_j = null;
        this.field_175119_g = true;
    }

    /**
     * Overriden in a sign to provide the text.
     */
    public Packet getDescriptionPacket()
    {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        this.writeToNBT(nbttagcompound);
        return new S35PacketUpdateTileEntity(this.pos, 6, nbttagcompound);
    }

    public int getBaseColor()
    {
        return this.baseColor;
    }

    public static int getBaseColor(ItemStack stack)
    {
        NBTTagCompound nbttagcompound = stack.getSubCompound("BlockEntityTag", false);
        return nbttagcompound != null && nbttagcompound.hasKey("Base") ? nbttagcompound.getInteger("Base") : stack.getMetadata();
    }

    public static int func_175113_c(ItemStack p_175113_0_)
    {
        NBTTagCompound nbttagcompound = p_175113_0_.getSubCompound("BlockEntityTag", false);
        return nbttagcompound != null && nbttagcompound.hasKey("Patterns") ? nbttagcompound.getTagList("Patterns", 10).tagCount() : 0;
    }

    @SideOnly(Side.CLIENT)
    public List func_175114_c()
    {
        this.func_175109_g();
        return this.field_175122_h;
    }

    @SideOnly(Side.CLIENT)
    public List func_175110_d()
    {
        this.func_175109_g();
        return this.field_175123_i;
    }

    @SideOnly(Side.CLIENT)
    public String func_175116_e()
    {
        this.func_175109_g();
        return this.field_175121_j;
    }

    public static void func_175117_e(ItemStack p_175117_0_)
    {
        NBTTagCompound nbttagcompound = p_175117_0_.getSubCompound("BlockEntityTag", false);

        if (nbttagcompound != null && nbttagcompound.hasKey("Patterns", 9))
        {
            NBTTagList nbttaglist = nbttagcompound.getTagList("Patterns", 10);

            if (nbttaglist.tagCount() > 0)
            {
                nbttaglist.removeTag(nbttaglist.tagCount() - 1);

                if (nbttaglist.hasNoTags())
                {
                    p_175117_0_.getTagCompound().removeTag("BlockEntityTag");

                    if (p_175117_0_.getTagCompound().hasNoTags())
                    {
                        p_175117_0_.setTagCompound((NBTTagCompound)null);
                    }
                }
            }
        }
    }

    @SideOnly(Side.CLIENT)
    private void func_175109_g()
    {
        if (this.field_175122_h == null || this.field_175123_i == null || this.field_175121_j == null)
        {
            if (!this.field_175119_g)
            {
                this.field_175121_j = "";
            }
            else
            {
                this.field_175122_h = Lists.newArrayList();
                this.field_175123_i = Lists.newArrayList();
                this.field_175122_h.add(TileEntityBanner.EnumBannerPattern.BASE);
                this.field_175123_i.add(EnumDyeColor.func_176766_a(this.baseColor));
                this.field_175121_j = "b" + this.baseColor;

                if (this.field_175118_f != null)
                {
                    for (int i = 0; i < this.field_175118_f.tagCount(); ++i)
                    {
                        NBTTagCompound nbttagcompound = this.field_175118_f.getCompoundTagAt(i);
                        TileEntityBanner.EnumBannerPattern enumbannerpattern = TileEntityBanner.EnumBannerPattern.func_177268_a(nbttagcompound.getString("Pattern"));

                        if (enumbannerpattern != null)
                        {
                            this.field_175122_h.add(enumbannerpattern);
                            int j = nbttagcompound.getInteger("Color");
                            this.field_175123_i.add(EnumDyeColor.func_176766_a(j));
                            this.field_175121_j = this.field_175121_j + enumbannerpattern.func_177273_b() + j;
                        }
                    }
                }
            }
        }
    }

    public static enum EnumBannerPattern
    {
        BASE("base", "b"),
        SQUARE_BOTTOM_LEFT("square_bottom_left", "bl", "   ", "   ", "#  "),
        SQUARE_BOTTOM_RIGHT("square_bottom_right", "br", "   ", "   ", "  #"),
        SQUARE_TOP_LEFT("square_top_left", "tl", "#  ", "   ", "   "),
        SQUARE_TOP_RIGHT("square_top_right", "tr", "  #", "   ", "   "),
        STRIPE_BOTTOM("stripe_bottom", "bs", "   ", "   ", "###"),
        STRIPE_TOP("stripe_top", "ts", "###", "   ", "   "),
        STRIPE_LEFT("stripe_left", "ls", "#  ", "#  ", "#  "),
        STRIPE_RIGHT("stripe_right", "rs", "  #", "  #", "  #"),
        STRIPE_CENTER("stripe_center", "cs", " # ", " # ", " # "),
        STRIPE_MIDDLE("stripe_middle", "ms", "   ", "###", "   "),
        STRIPE_DOWNRIGHT("stripe_downright", "drs", "#  ", " # ", "  #"),
        STRIPE_DOWNLEFT("stripe_downleft", "dls", "  #", " # ", "#  "),
        STRIPE_SMALL("small_stripes", "ss", "# #", "# #", "   "),
        CROSS("cross", "cr", "# #", " # ", "# #"),
        STRAIGHT_CROSS("straight_cross", "sc", " # ", "###", " # "),
        TRIANGLE_BOTTOM("triangle_bottom", "bt", "   ", " # ", "# #"),
        TRIANGLE_TOP("triangle_top", "tt", "# #", " # ", "   "),
        TRIANGLES_BOTTOM("triangles_bottom", "bts", "   ", "# #", " # "),
        TRIANGLES_TOP("triangles_top", "tts", " # ", "# #", "   "),
        DIAGONAL_LEFT("diagonal_left", "ld", "## ", "#  ", "   "),
        DIAGONAL_RIGHT("diagonal_up_right", "rd", "   ", "  #", " ##"),
        DIAGONAL_LEFT_MIRROR("diagonal_up_left", "lud", "   ", "#  ", "## "),
        DIAGONAL_RIGHT_MIRROR("diagonal_right", "rud", " ##", "  #", "   "),
        CIRCLE_MIDDLE("circle", "mc", "   ", " # ", "   "),
        RHOMBUS_MIDDLE("rhombus", "mr", " # ", "# #", " # "),
        HALF_VERTICAL("half_vertical", "vh", "## ", "## ", "## "),
        HALF_HORIZONTAL("half_horizontal", "hh", "###", "###", "   "),
        HALF_VERTICAL_MIRROR("half_vertical_right", "vhr", " ##", " ##", " ##"),
        HALF_HORIZONTAL_MIRROR("half_horizontal_bottom", "hhb", "   ", "###", "###"),
        BORDER("border", "bo", "###", "# #", "###"),
        CURLY_BORDER("curly_border", "cbo", new ItemStack(Blocks.vine)),
        CREEPER("creeper", "cre", new ItemStack(Items.skull, 1, 4)),
        GRADIENT("gradient", "gra", "# #", " # ", " # "),
        GRADIENT_UP("gradient_up", "gru", " # ", " # ", "# #"),
        BRICKS("bricks", "bri", new ItemStack(Blocks.brick_block)),
        SKULL("skull", "sku", new ItemStack(Items.skull, 1, 1)),
        FLOWER("flower", "flo", new ItemStack(Blocks.red_flower, 1, BlockFlower.EnumFlowerType.OXEYE_DAISY.func_176968_b())),
        MOJANG("mojang", "moj", new ItemStack(Items.golden_apple, 1, 1));
        private String field_177284_N;
        private String field_177285_O;
        private String[] field_177291_P;
        private ItemStack field_177290_Q;

        private static final String __OBFID = "CL_00002043";

        private EnumBannerPattern(String p_i45670_3_, String p_i45670_4_)
        {
            this.field_177291_P = new String[3];
            this.field_177284_N = p_i45670_3_;
            this.field_177285_O = p_i45670_4_;
        }

        private EnumBannerPattern(String p_i45671_3_, String p_i45671_4_, ItemStack p_i45671_5_)
        {
            this(p_i45671_3_, p_i45671_4_);
            this.field_177290_Q = p_i45671_5_;
        }

        private EnumBannerPattern(String p_i45672_3_, String p_i45672_4_, String p_i45672_5_, String p_i45672_6_, String p_i45672_7_)
        {
            this(p_i45672_3_, p_i45672_4_);
            this.field_177291_P[0] = p_i45672_5_;
            this.field_177291_P[1] = p_i45672_6_;
            this.field_177291_P[2] = p_i45672_7_;
        }

        @SideOnly(Side.CLIENT)
        public String func_177271_a()
        {
            return this.field_177284_N;
        }

        public String func_177273_b()
        {
            return this.field_177285_O;
        }

        public String[] func_177267_c()
        {
            return this.field_177291_P;
        }

        public boolean func_177270_d()
        {
            return this.field_177290_Q != null || this.field_177291_P[0] != null;
        }

        public boolean func_177269_e()
        {
            return this.field_177290_Q != null;
        }

        public ItemStack func_177272_f()
        {
            return this.field_177290_Q;
        }

        @SideOnly(Side.CLIENT)
        public static TileEntityBanner.EnumBannerPattern func_177268_a(String p_177268_0_)
        {
            TileEntityBanner.EnumBannerPattern[] aenumbannerpattern = values();
            int i = aenumbannerpattern.length;

            for (int j = 0; j < i; ++j)
            {
                TileEntityBanner.EnumBannerPattern enumbannerpattern = aenumbannerpattern[j];

                if (enumbannerpattern.field_177285_O.equals(p_177268_0_))
                {
                    return enumbannerpattern;
                }
            }

            return null;
        }
    }
}