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

import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.vecmath.Vector3f;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class BlockPart
{
    public final Vector3f field_178241_a;
    public final Vector3f field_178239_b;
    public final Map field_178240_c;
    public final BlockPartRotation field_178237_d;
    public final boolean field_178238_e;
    private static final String __OBFID = "CL_00002511";

    public BlockPart(Vector3f p_i46231_1_, Vector3f p_i46231_2_, Map p_i46231_3_, BlockPartRotation p_i46231_4_, boolean p_i46231_5_)
    {
        this.field_178241_a = p_i46231_1_;
        this.field_178239_b = p_i46231_2_;
        this.field_178240_c = p_i46231_3_;
        this.field_178237_d = p_i46231_4_;
        this.field_178238_e = p_i46231_5_;
        this.func_178235_a();
    }

    private void func_178235_a()
    {
        Iterator iterator = this.field_178240_c.entrySet().iterator();

        while (iterator.hasNext())
        {
            Entry entry = (Entry)iterator.next();
            float[] afloat = this.func_178236_a((EnumFacing)entry.getKey());
            ((BlockPartFace)entry.getValue()).field_178243_e.func_178349_a(afloat);
        }
    }

    private float[] func_178236_a(EnumFacing p_178236_1_)
    {
        float[] afloat;

        switch (BlockPart.SwitchEnumFacing.field_178234_a[p_178236_1_.ordinal()])
        {
            case 1:
            case 2:
                afloat = new float[] {this.field_178241_a.x, this.field_178241_a.z, this.field_178239_b.x, this.field_178239_b.z};
                break;
            case 3:
            case 4:
                afloat = new float[] {this.field_178241_a.x, 16.0F - this.field_178239_b.y, this.field_178239_b.x, 16.0F - this.field_178241_a.y};
                break;
            case 5:
            case 6:
                afloat = new float[] {this.field_178241_a.z, 16.0F - this.field_178239_b.y, this.field_178239_b.z, 16.0F - this.field_178241_a.y};
                break;
            default:
                throw new NullPointerException();
        }

        return afloat;
    }

    @SideOnly(Side.CLIENT)
    static class Deserializer implements JsonDeserializer
        {
            private static final String __OBFID = "CL_00002509";

            public BlockPart func_178254_a(JsonElement p_178254_1_, Type p_178254_2_, JsonDeserializationContext p_178254_3_)
            {
                JsonObject jsonobject = p_178254_1_.getAsJsonObject();
                Vector3f vector3f = this.func_178249_e(jsonobject);
                Vector3f vector3f1 = this.func_178247_d(jsonobject);
                BlockPartRotation blockpartrotation = this.func_178256_a(jsonobject);
                Map map = this.func_178250_a(p_178254_3_, jsonobject);

                if (jsonobject.has("shade") && !JsonUtils.func_180199_c(jsonobject, "shade"))
                {
                    throw new JsonParseException("Expected shade to be a Boolean");
                }
                else
                {
                    boolean flag = JsonUtils.getJsonObjectBooleanFieldValueOrDefault(jsonobject, "shade", true);
                    return new BlockPart(vector3f, vector3f1, map, blockpartrotation, flag);
                }
            }

            private BlockPartRotation func_178256_a(JsonObject p_178256_1_)
            {
                BlockPartRotation blockpartrotation = null;

                if (p_178256_1_.has("rotation"))
                {
                    JsonObject jsonobject1 = JsonUtils.getJsonObject(p_178256_1_, "rotation");
                    Vector3f vector3f = this.func_178251_a(jsonobject1, "origin");
                    vector3f.scale(0.0625F);
                    EnumFacing.Axis axis = this.func_178252_c(jsonobject1);
                    float f = this.func_178255_b(jsonobject1);
                    boolean flag = JsonUtils.getJsonObjectBooleanFieldValueOrDefault(jsonobject1, "rescale", false);
                    blockpartrotation = new BlockPartRotation(vector3f, axis, f, flag);
                }

                return blockpartrotation;
            }

            private float func_178255_b(JsonObject p_178255_1_)
            {
                float f = JsonUtils.getJsonObjectFloatFieldValue(p_178255_1_, "angle");

                if (f != 0.0F && MathHelper.abs(f) != 22.5F && MathHelper.abs(f) != 45.0F)
                {
                    throw new JsonParseException("Invalid rotation " + f + " found, only -45/-22.5/0/22.5/45 allowed");
                }
                else
                {
                    return f;
                }
            }

            private EnumFacing.Axis func_178252_c(JsonObject p_178252_1_)
            {
                String s = JsonUtils.getJsonObjectStringFieldValue(p_178252_1_, "axis");
                EnumFacing.Axis axis = EnumFacing.Axis.byName(s.toLowerCase());

                if (axis == null)
                {
                    throw new JsonParseException("Invalid rotation axis: " + s);
                }
                else
                {
                    return axis;
                }
            }

            private Map func_178250_a(JsonDeserializationContext p_178250_1_, JsonObject p_178250_2_)
            {
                Map map = this.func_178253_b(p_178250_1_, p_178250_2_);

                if (map.isEmpty())
                {
                    throw new JsonParseException("Expected between 1 and 6 unique faces, got 0");
                }
                else
                {
                    return map;
                }
            }

            private Map func_178253_b(JsonDeserializationContext p_178253_1_, JsonObject p_178253_2_)
            {
                EnumMap enummap = Maps.newEnumMap(EnumFacing.class);
                JsonObject jsonobject1 = JsonUtils.getJsonObject(p_178253_2_, "faces");
                Iterator iterator = jsonobject1.entrySet().iterator();

                while (iterator.hasNext())
                {
                    Entry entry = (Entry)iterator.next();
                    EnumFacing enumfacing = this.func_178248_a((String)entry.getKey());
                    enummap.put(enumfacing, (BlockPartFace)p_178253_1_.deserialize((JsonElement)entry.getValue(), BlockPartFace.class));
                }

                return enummap;
            }

            private EnumFacing func_178248_a(String p_178248_1_)
            {
                EnumFacing enumfacing = EnumFacing.byName(p_178248_1_);

                if (enumfacing == null)
                {
                    throw new JsonParseException("Unknown facing: " + p_178248_1_);
                }
                else
                {
                    return enumfacing;
                }
            }

            private Vector3f func_178247_d(JsonObject p_178247_1_)
            {
                Vector3f vector3f = this.func_178251_a(p_178247_1_, "to");

                if (vector3f.x >= -16.0F && vector3f.y >= -16.0F && vector3f.z >= -16.0F && vector3f.x <= 32.0F && vector3f.y <= 32.0F && vector3f.z <= 32.0F)
                {
                    return vector3f;
                }
                else
                {
                    throw new JsonParseException("\'to\' specifier exceeds the allowed boundaries: " + vector3f);
                }
            }

            private Vector3f func_178249_e(JsonObject p_178249_1_)
            {
                Vector3f vector3f = this.func_178251_a(p_178249_1_, "from");

                if (vector3f.x >= -16.0F && vector3f.y >= -16.0F && vector3f.z >= -16.0F && vector3f.x <= 32.0F && vector3f.y <= 32.0F && vector3f.z <= 32.0F)
                {
                    return vector3f;
                }
                else
                {
                    throw new JsonParseException("\'from\' specifier exceeds the allowed boundaries: " + vector3f);
                }
            }

            private Vector3f func_178251_a(JsonObject p_178251_1_, String p_178251_2_)
            {
                JsonArray jsonarray = JsonUtils.getJsonObjectJsonArrayField(p_178251_1_, p_178251_2_);

                if (jsonarray.size() != 3)
                {
                    throw new JsonParseException("Expected 3 " + p_178251_2_ + " values, found: " + jsonarray.size());
                }
                else
                {
                    float[] afloat = new float[3];

                    for (int i = 0; i < afloat.length; ++i)
                    {
                        afloat[i] = JsonUtils.getJsonElementFloatValue(jsonarray.get(i), p_178251_2_ + "[" + i + "]");
                    }

                    return new Vector3f(afloat);
                }
            }

            public Object deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_)
            {
                return this.func_178254_a(p_deserialize_1_, p_deserialize_2_, p_deserialize_3_);
            }
        }

    @SideOnly(Side.CLIENT)

    static final class SwitchEnumFacing
        {
            static final int[] field_178234_a = new int[EnumFacing.values().length];
            private static final String __OBFID = "CL_00002510";

            static
            {
                try
                {
                    field_178234_a[EnumFacing.DOWN.ordinal()] = 1;
                }
                catch (NoSuchFieldError var6)
                {
                    ;
                }

                try
                {
                    field_178234_a[EnumFacing.UP.ordinal()] = 2;
                }
                catch (NoSuchFieldError var5)
                {
                    ;
                }

                try
                {
                    field_178234_a[EnumFacing.NORTH.ordinal()] = 3;
                }
                catch (NoSuchFieldError var4)
                {
                    ;
                }

                try
                {
                    field_178234_a[EnumFacing.SOUTH.ordinal()] = 4;
                }
                catch (NoSuchFieldError var3)
                {
                    ;
                }

                try
                {
                    field_178234_a[EnumFacing.WEST.ordinal()] = 5;
                }
                catch (NoSuchFieldError var2)
                {
                    ;
                }

                try
                {
                    field_178234_a[EnumFacing.EAST.ordinal()] = 6;
                }
                catch (NoSuchFieldError var1)
                {
                    ;
                }
            }
        }
}