package net.minecraft.src;

import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import net.lax1dude.eaglercraft.TextureLocation;
import org.apache.commons.io.IOUtils;

public class SimpleResource implements Resource
{
    private final Map mapMetadataSections = Maps.newHashMap();
    private final TextureLocation srTextureLocation;
    private final InputStream resourceInputStream;
    private final InputStream mcmetaInputStream;
    private final MetadataSerializer srMetadataSerializer;
    private boolean mcmetaJsonChecked;
    private JsonObject mcmetaJson;

    public SimpleResource(TextureLocation par1TextureLocation, InputStream par2InputStream, InputStream par3InputStream, MetadataSerializer par4MetadataSerializer)
    {
        this.srTextureLocation = par1TextureLocation;
        this.resourceInputStream = par2InputStream;
        this.mcmetaInputStream = par3InputStream;
        this.srMetadataSerializer = par4MetadataSerializer;
    }

    public InputStream getInputStream()
    {
        return this.resourceInputStream;
    }

    public boolean hasMetadata()
    {
        return this.mcmetaInputStream != null;
    }

    public MetadataSection getMetadata(String par1Str)
    {
        if (!this.hasMetadata())
        {
            return null;
        }
        else
        {
            if (this.mcmetaJson == null && !this.mcmetaJsonChecked)
            {
                this.mcmetaJsonChecked = true;
                BufferedReader var2 = null;

                try
                {
                    var2 = new BufferedReader(new InputStreamReader(this.mcmetaInputStream));
                    this.mcmetaJson = (new JsonParser()).parse(var2).getAsJsonObject();
                }
                finally
                {
                    IOUtils.closeQuietly(var2);
                }
            }

            MetadataSection var6 = (MetadataSection)this.mapMetadataSections.get(par1Str);

            if (var6 == null)
            {
                var6 = this.srMetadataSerializer.parseMetadataSection(par1Str, this.mcmetaJson);
            }

            return var6;
        }
    }

    public boolean equals(Object par1Obj)
    {
        if (this == par1Obj)
        {
            return true;
        }
        else if (par1Obj instanceof SimpleResource)
        {
            SimpleResource var2 = (SimpleResource)par1Obj;
            return this.srTextureLocation != null ? this.srTextureLocation.equals(var2.srTextureLocation) : var2.srTextureLocation == null;
        }
        else
        {
            return false;
        }
    }

    public int hashCode()
    {
        return this.srTextureLocation == null ? 0 : this.srTextureLocation.hashCode();
    }
}
