package net.minecraft.client.renderer.block.model;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.vecmath.Vector3f;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ItemModelGenerator
{
    public static final List LAYERS = Lists.newArrayList(new String[] {"layer0", "layer1", "layer2", "layer3", "layer4"});
    private static final String __OBFID = "CL_00002488";

    public ModelBlock func_178392_a(TextureMap p_178392_1_, ModelBlock p_178392_2_)
    {
        HashMap hashmap = Maps.newHashMap();
        ArrayList arraylist = Lists.newArrayList();

        for (int i = 0; i < LAYERS.size(); ++i)
        {
            String s = (String)LAYERS.get(i);

            if (!p_178392_2_.isTexturePresent(s))
            {
                break;
            }

            String s1 = p_178392_2_.resolveTextureName(s);
            hashmap.put(s, s1);
            TextureAtlasSprite textureatlassprite = p_178392_1_.getAtlasSprite((new ResourceLocation(s1)).toString());
            arraylist.addAll(this.func_178394_a(i, s, textureatlassprite));
        }

        if (arraylist.isEmpty())
        {
            return null;
        }
        else
        {
            hashmap.put("particle", p_178392_2_.isTexturePresent("particle") ? p_178392_2_.resolveTextureName("particle") : (String)hashmap.get("layer0"));
            return new ModelBlock(arraylist, hashmap, false, false, new ItemCameraTransforms(p_178392_2_.getThirdPersonTransform(), p_178392_2_.getFirstPersonTransform(), p_178392_2_.getHeadTransform(), p_178392_2_.getInGuiTransform()));
        }
    }

    private List func_178394_a(int p_178394_1_, String p_178394_2_, TextureAtlasSprite p_178394_3_)
    {
        HashMap hashmap = Maps.newHashMap();
        hashmap.put(EnumFacing.SOUTH, new BlockPartFace((EnumFacing)null, p_178394_1_, p_178394_2_, new BlockFaceUV(new float[] {0.0F, 0.0F, 16.0F, 16.0F}, 0)));
        hashmap.put(EnumFacing.NORTH, new BlockPartFace((EnumFacing)null, p_178394_1_, p_178394_2_, new BlockFaceUV(new float[] {16.0F, 0.0F, 0.0F, 16.0F}, 0)));
        ArrayList arraylist = Lists.newArrayList();
        arraylist.add(new BlockPart(new Vector3f(0.0F, 0.0F, 7.5F), new Vector3f(16.0F, 16.0F, 8.5F), hashmap, (BlockPartRotation)null, true));
        arraylist.addAll(this.func_178397_a(p_178394_3_, p_178394_2_, p_178394_1_));
        return arraylist;
    }

    private List func_178397_a(TextureAtlasSprite p_178397_1_, String p_178397_2_, int p_178397_3_)
    {
        float f = (float)p_178397_1_.getIconWidth();
        float f1 = (float)p_178397_1_.getIconHeight();
        ArrayList arraylist = Lists.newArrayList();
        Iterator iterator = this.func_178393_a(p_178397_1_).iterator();

        while (iterator.hasNext())
        {
            ItemModelGenerator.Span span = (ItemModelGenerator.Span)iterator.next();
            float f2 = 0.0F;
            float f3 = 0.0F;
            float f4 = 0.0F;
            float f5 = 0.0F;
            float f6 = 0.0F;
            float f7 = 0.0F;
            float f8 = 0.0F;
            float f9 = 0.0F;
            float f10 = 0.0F;
            float f11 = 0.0F;
            float f12 = (float)span.func_178385_b();
            float f13 = (float)span.func_178384_c();
            float f14 = (float)span.func_178381_d();
            ItemModelGenerator.SpanFacing spanfacing = span.func_178383_a();

            switch (ItemModelGenerator.SwitchSpanFacing.field_178390_a[spanfacing.ordinal()])
            {
                case 1:
                    f6 = f12;
                    f2 = f12;
                    f4 = f7 = f13 + 1.0F;
                    f8 = f14;
                    f3 = f14;
                    f9 = f14;
                    f5 = f14;
                    f10 = 16.0F / f;
                    f11 = 16.0F / (f1 - 1.0F);
                    break;
                case 2:
                    f9 = f14;
                    f8 = f14;
                    f6 = f12;
                    f2 = f12;
                    f4 = f7 = f13 + 1.0F;
                    f3 = f14 + 1.0F;
                    f5 = f14 + 1.0F;
                    f10 = 16.0F / f;
                    f11 = 16.0F / (f1 - 1.0F);
                    break;
                case 3:
                    f6 = f14;
                    f2 = f14;
                    f7 = f14;
                    f4 = f14;
                    f9 = f12;
                    f3 = f12;
                    f5 = f8 = f13 + 1.0F;
                    f10 = 16.0F / (f - 1.0F);
                    f11 = 16.0F / f1;
                    break;
                case 4:
                    f7 = f14;
                    f6 = f14;
                    f2 = f14 + 1.0F;
                    f4 = f14 + 1.0F;
                    f9 = f12;
                    f3 = f12;
                    f5 = f8 = f13 + 1.0F;
                    f10 = 16.0F / (f - 1.0F);
                    f11 = 16.0F / f1;
            }

            float f15 = 16.0F / f;
            float f16 = 16.0F / f1;
            f2 *= f15;
            f4 *= f15;
            f3 *= f16;
            f5 *= f16;
            f3 = 16.0F - f3;
            f5 = 16.0F - f5;
            f6 *= f10;
            f7 *= f10;
            f8 *= f11;
            f9 *= f11;
            HashMap hashmap = Maps.newHashMap();
            hashmap.put(spanfacing.func_178367_a(), new BlockPartFace((EnumFacing)null, p_178397_3_, p_178397_2_, new BlockFaceUV(new float[] {f6, f8, f7, f9}, 0)));

            switch (ItemModelGenerator.SwitchSpanFacing.field_178390_a[spanfacing.ordinal()])
            {
                case 1:
                    arraylist.add(new BlockPart(new Vector3f(f2, f3, 7.5F), new Vector3f(f4, f3, 8.5F), hashmap, (BlockPartRotation)null, true));
                    break;
                case 2:
                    arraylist.add(new BlockPart(new Vector3f(f2, f5, 7.5F), new Vector3f(f4, f5, 8.5F), hashmap, (BlockPartRotation)null, true));
                    break;
                case 3:
                    arraylist.add(new BlockPart(new Vector3f(f2, f3, 7.5F), new Vector3f(f2, f5, 8.5F), hashmap, (BlockPartRotation)null, true));
                    break;
                case 4:
                    arraylist.add(new BlockPart(new Vector3f(f4, f3, 7.5F), new Vector3f(f4, f5, 8.5F), hashmap, (BlockPartRotation)null, true));
            }
        }

        return arraylist;
    }

    private List func_178393_a(TextureAtlasSprite p_178393_1_)
    {
        int i = p_178393_1_.getIconWidth();
        int j = p_178393_1_.getIconHeight();
        ArrayList arraylist = Lists.newArrayList();

        for (int k = 0; k < p_178393_1_.getFrameCount(); ++k)
        {
            int[] aint = p_178393_1_.getFrameTextureData(k)[0];

            for (int l = 0; l < j; ++l)
            {
                for (int i1 = 0; i1 < i; ++i1)
                {
                    boolean flag = !this.func_178391_a(aint, i1, l, i, j);
                    this.func_178396_a(ItemModelGenerator.SpanFacing.UP, arraylist, aint, i1, l, i, j, flag);
                    this.func_178396_a(ItemModelGenerator.SpanFacing.DOWN, arraylist, aint, i1, l, i, j, flag);
                    this.func_178396_a(ItemModelGenerator.SpanFacing.LEFT, arraylist, aint, i1, l, i, j, flag);
                    this.func_178396_a(ItemModelGenerator.SpanFacing.RIGHT, arraylist, aint, i1, l, i, j, flag);
                }
            }
        }

        return arraylist;
    }

    private void func_178396_a(ItemModelGenerator.SpanFacing p_178396_1_, List p_178396_2_, int[] p_178396_3_, int p_178396_4_, int p_178396_5_, int p_178396_6_, int p_178396_7_, boolean p_178396_8_)
    {
        boolean flag1 = this.func_178391_a(p_178396_3_, p_178396_4_ + p_178396_1_.func_178372_b(), p_178396_5_ + p_178396_1_.func_178371_c(), p_178396_6_, p_178396_7_) && p_178396_8_;

        if (flag1)
        {
            this.func_178395_a(p_178396_2_, p_178396_1_, p_178396_4_, p_178396_5_);
        }
    }

    private void func_178395_a(List p_178395_1_, ItemModelGenerator.SpanFacing p_178395_2_, int p_178395_3_, int p_178395_4_)
    {
        ItemModelGenerator.Span span = null;
        Iterator iterator = p_178395_1_.iterator();

        while (iterator.hasNext())
        {
            ItemModelGenerator.Span span1 = (ItemModelGenerator.Span)iterator.next();

            if (span1.func_178383_a() == p_178395_2_)
            {
                int k = p_178395_2_.func_178369_d() ? p_178395_4_ : p_178395_3_;

                if (span1.func_178381_d() == k)
                {
                    span = span1;
                    break;
                }
            }
        }

        int l = p_178395_2_.func_178369_d() ? p_178395_4_ : p_178395_3_;
        int i1 = p_178395_2_.func_178369_d() ? p_178395_3_ : p_178395_4_;

        if (span == null)
        {
            p_178395_1_.add(new ItemModelGenerator.Span(p_178395_2_, i1, l));
        }
        else
        {
            span.func_178382_a(i1);
        }
    }

    private boolean func_178391_a(int[] p_178391_1_, int p_178391_2_, int p_178391_3_, int p_178391_4_, int p_178391_5_)
    {
        return p_178391_2_ >= 0 && p_178391_3_ >= 0 && p_178391_2_ < p_178391_4_ && p_178391_3_ < p_178391_5_ ? (p_178391_1_[p_178391_3_ * p_178391_4_ + p_178391_2_] >> 24 & 255) == 0 : true;
    }

    @SideOnly(Side.CLIENT)
    static class Span
        {
            private final ItemModelGenerator.SpanFacing field_178389_a;
            private int field_178387_b;
            private int field_178388_c;
            private final int field_178386_d;
            private static final String __OBFID = "CL_00002486";

            public Span(ItemModelGenerator.SpanFacing p_i46216_1_, int p_i46216_2_, int p_i46216_3_)
            {
                this.field_178389_a = p_i46216_1_;
                this.field_178387_b = p_i46216_2_;
                this.field_178388_c = p_i46216_2_;
                this.field_178386_d = p_i46216_3_;
            }

            public void func_178382_a(int p_178382_1_)
            {
                if (p_178382_1_ < this.field_178387_b)
                {
                    this.field_178387_b = p_178382_1_;
                }
                else if (p_178382_1_ > this.field_178388_c)
                {
                    this.field_178388_c = p_178382_1_;
                }
            }

            public ItemModelGenerator.SpanFacing func_178383_a()
            {
                return this.field_178389_a;
            }

            public int func_178385_b()
            {
                return this.field_178387_b;
            }

            public int func_178384_c()
            {
                return this.field_178388_c;
            }

            public int func_178381_d()
            {
                return this.field_178386_d;
            }
        }

    @SideOnly(Side.CLIENT)
    static enum SpanFacing
    {
        UP(EnumFacing.UP, 0, -1),
        DOWN(EnumFacing.DOWN, 0, 1),
        LEFT(EnumFacing.EAST, -1, 0),
        RIGHT(EnumFacing.WEST, 1, 0);
        private final EnumFacing field_178376_e;
        private final int field_178373_f;
        private final int field_178374_g;

        private static final String __OBFID = "CL_00002485";

        private SpanFacing(EnumFacing p_i46215_3_, int p_i46215_4_, int p_i46215_5_)
        {
            this.field_178376_e = p_i46215_3_;
            this.field_178373_f = p_i46215_4_;
            this.field_178374_g = p_i46215_5_;
        }

        public EnumFacing func_178367_a()
        {
            return this.field_178376_e;
        }

        public int func_178372_b()
        {
            return this.field_178373_f;
        }

        public int func_178371_c()
        {
            return this.field_178374_g;
        }

        private boolean func_178369_d()
        {
            return this == DOWN || this == UP;
        }
    }

    @SideOnly(Side.CLIENT)

    static final class SwitchSpanFacing
        {
            static final int[] field_178390_a = new int[ItemModelGenerator.SpanFacing.values().length];
            private static final String __OBFID = "CL_00002487";

            static
            {
                try
                {
                    field_178390_a[ItemModelGenerator.SpanFacing.UP.ordinal()] = 1;
                }
                catch (NoSuchFieldError var4)
                {
                    ;
                }

                try
                {
                    field_178390_a[ItemModelGenerator.SpanFacing.DOWN.ordinal()] = 2;
                }
                catch (NoSuchFieldError var3)
                {
                    ;
                }

                try
                {
                    field_178390_a[ItemModelGenerator.SpanFacing.LEFT.ordinal()] = 3;
                }
                catch (NoSuchFieldError var2)
                {
                    ;
                }

                try
                {
                    field_178390_a[ItemModelGenerator.SpanFacing.RIGHT.ordinal()] = 4;
                }
                catch (NoSuchFieldError var1)
                {
                    ;
                }
            }
        }
}