package client.renderer.entity;

import client.renderer.Frustum;
import common.entity.Entity;
import common.util.BlockPos;
import common.util.BoundingBox;

public abstract class Render<T extends Entity>
{
    protected final RenderManager manager;

    protected Render(RenderManager manager)
    {
        this.manager = manager;
    }

    public boolean shouldRender(T entity, double camX, double camY, double camZ)
    {
        BoundingBox bb = entity.getEntityBoundingBox();
        if (bb.hasNaN() || bb.getAverageEdgeLength() == 0.0D)
            bb = new BoundingBox(entity.posX - 2.0D, entity.posY - 2.0D, entity.posZ - 2.0D, entity.posX + 2.0D, entity.posY + 2.0D, entity.posZ + 2.0D);
        return entity.isInRangeToRender3d(camX, camY, camZ) && (entity.noFrustumCheck || Frustum.isInFrustum(bb));
    }

    public void doRender(T entity, double x, double y, double z, float partial)
    {
        this.renderName(entity, x, y, z);
    }

    protected void renderName(T entity, double x, double y, double z)
    {
    	String name = entity.getLabel();
    	if(name != null && !name.isEmpty())
    		this.manager.renderLabel(entity, name, x, y, z, 64);
    }

    protected abstract String getEntityTexture(T entity);

    public final boolean bindEntityTexture(T entity)
    {
    	String tex = this.getEntityTexture(entity);
        if(tex == null)
            return false;
        this.bindTexture(tex);
        return true;
    }

    public final void bindTexture(String tex)
    {
        this.manager.renderEngine.bindTexture(tex);
    }

    public final RenderManager getRenderManager()
    {
        return this.manager;
    }

	protected static float getBrightness(Entity entity) {
		BlockPos pos = new BlockPos(entity.posX, entity.posY + (double)entity.getEyeHeight(), entity.posZ);
		return entity.worldObj.isBlockLoaded(pos) ? entity.worldObj.getLightBrightness(pos) : 0.0F;
	}
}
