package client.renderer.entity;

import java.util.Set;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.model.ModelHorse;
import client.renderer.texture.LayeredTexture;
import common.collect.Sets;
import common.entity.animal.EntityHorse;

public class RenderHorse extends RenderLiving<EntityHorse>
{
    private static final Set<String> loaded = Sets.<String>newHashSet();
    private static final String whiteHorseTextures = "textures/creature/horse_white.png";
    private static final String muleTextures = "textures/creature/mule.png";
    private static final String donkeyTextures = "textures/creature/donkey.png";

    public RenderHorse(RenderManager rendermanagerIn, ModelHorse model)
    {
        super(rendermanagerIn, model);
    }

    /**
     * Allows the render to do any OpenGL state modifications necessary before the model is rendered. Args:
     * entityLiving, partialTickTime
     */
    protected void preRenderCallback(EntityHorse entitylivingbaseIn, float partialTickTime)
    {
        float f = 1.0F;
        int i = entitylivingbaseIn.getHorseType();

        if (i == 1)
        {
            f *= 0.87F;
        }
        else if (i == 2)
        {
            f *= 0.92F;
        }

        GL11.glScalef(f, f, f);
        super.preRenderCallback(entitylivingbaseIn, partialTickTime);
    }

    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected String getEntityTexture(EntityHorse entity)
    {
        if (!entity.hasSpecificTexture())
        {
            switch (entity.getHorseType())
            {
                case 0:
                default:
                    return whiteHorseTextures;

                case 1:
                    return donkeyTextures;

                case 2:
                    return muleTextures;
            }
        }
        else
        {
            return this.loadHorseTexture(entity);
        }
    }

    private String loadHorseTexture(EntityHorse horse)
    {
        String s = horse.getHorseTexture();

        if (!horse.hasValidTexture())
        {
            return null;
        }
        else
        {
            if (!loaded.contains(s))
            {
                Client.CLIENT.getTextureManager().loadTexture(s, new LayeredTexture(horse.getVariantTexturePaths()));
                loaded.add(s);
            }

            return s;
        }
    }
}
