package client.renderer;

import java.util.Map;

import client.renderer.blockmodel.IBakedModel;
import client.renderer.blockmodel.ModelManager;
import client.renderer.texture.Sprite;
import common.collect.Maps;
import common.init.ItemRegistry;
import common.item.Item;
import common.item.ItemStack;

public class ItemModelMesher {
	private final Map<Integer, IBakedModel> models = Maps.<Integer, IBakedModel>newHashMap();
	private final ModelManager manager;

	public ItemModelMesher(ModelManager manager) {
		this.manager = manager;
	}

	public Sprite getParticleIcon(Item item) {
		return this.getItemModel(new ItemStack(item)).getBaseTexture();
	}

	public IBakedModel getItemModel(ItemStack stack) {
		Item item = stack.getItem();
		IBakedModel model = this.models.get(ItemRegistry.getId(item));
		if(model == null)
			model = this.manager.getMissingModel();
		return model;
	}

	public ModelManager getModelManager() {
		return this.manager;
	}

	public void rebuildCache() {
		this.models.clear();
		for(Item item : ItemRegistry.items()) {
			this.models.put(ItemRegistry.getId(item), this.manager.getModel("item/" + ItemRegistry.getName(item).toString()));
		}
	}
}
