package net.minecraft.src;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import net.lax1dude.eaglercraft.TextureLocation;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;

public class DefaultResourcePack implements ResourcePack
{
    public static final Set defaultResourceDomains = ImmutableSet.of("minecraft");
    private final Map mapResourceFiles = Maps.newHashMap();
    private final File fileAssets;

    public DefaultResourcePack(File par1File)
    {
        this.fileAssets = par1File;
        this.readAssetsDir(this.fileAssets);
    }

    public InputStream getInputStream(TextureLocation par1TextureLocation) throws IOException
    {
        InputStream var2 = this.getResourceStream(par1TextureLocation);

        if (var2 != null)
        {
            return var2;
        }
        else
        {
            File var3 = (File)this.mapResourceFiles.get(par1TextureLocation.toString());

            if (var3 != null)
            {
                return new FileInputStream(var3);
            }
            else
            {
                throw new FileNotFoundException("Something is fuck: " + par1TextureLocation.toString() + "I have no idea what the hell this is"); // FIX THIS SHIT
            }
        }
    }

    private InputStream getResourceStream(TextureLocation par1TextureLocation)
    {
        return DefaultResourcePack.class.getResourceAsStream("/assets/minecraft/" + par1TextureLocation);
    }

    public void addResourceFile(String par1Str, File par2File)
    {
        this.mapResourceFiles.put((new TextureLocation(par1Str)).toString(), par2File);
    }

    public boolean resourceExists(TextureLocation par1TextureLocation)
    {
        return this.getResourceStream(par1TextureLocation) != null || this.mapResourceFiles.containsKey(par1TextureLocation.toString());
    }

    public Set getResourceDomains()
    {
        return defaultResourceDomains;
    }

    public void readAssetsDir(File par1File)
    {
        if (par1File.isDirectory())
        {
            File[] var2 = par1File.listFiles();
            int var3 = var2.length;

            for (int var4 = 0; var4 < var3; ++var4)
            {
                File var5 = var2[var4];
                this.readAssetsDir(var5);
            }
        }
        else
        {
            this.addResourceFile(AbstractResourcePack.getRelativeName(this.fileAssets, par1File), par1File);
        }
    }

    public MetadataSection getPackMetadata(MetadataSerializer par1MetadataSerializer, String par2Str) throws IOException
    {
        return AbstractResourcePack.readMetadata(par1MetadataSerializer, DefaultResourcePack.class.getResourceAsStream("/" + (new TextureLocation("pack.mcmeta"))), par2Str);
    }

    public BufferedImage getPackImage() throws IOException
    {
        return ImageIO.read(DefaultResourcePack.class.getResourceAsStream("/" + (new TextureLocation("pack.png"))));
    }

    public String getPackName()
    {
        return "Default";
    }
}
