package client.renderer;

import java.util.List;
import java.util.Map;

import client.renderer.blockmodel.IBakedModel;
import client.renderer.blockmodel.ModelManager;
import client.renderer.texture.TextureAtlasSprite;
import common.collect.Lists;
import common.collect.Maps;
import common.init.ItemRegistry;
import common.item.Item;
import common.item.ItemStack;
import common.model.ItemMeshDefinition;

public class ItemModelMesher
{
//    private final Map<Integer, ResourceLocation> simpleShapes = Maps.<Integer, ResourceLocation>newHashMap();
    private final Map<Integer, IBakedModel> simpleShapesCache = Maps.<Integer, IBakedModel>newHashMap();
    private final Map<Item, ItemMeshDefinition> shapers = Maps.<Item, ItemMeshDefinition>newHashMap();
    private final ModelManager modelManager;

    public ItemModelMesher(ModelManager modelManager)
    {
        this.modelManager = modelManager;
    	for(Item item : ItemRegistry.REGISTRY) {
    		ItemMeshDefinition mesher = item.getMesher();
    		if(mesher != null)
    			this.shapers.put(item, mesher);
    	}
    }

    public TextureAtlasSprite getParticleIcon(Item item)
    {
        return this.getParticleIcon(item, 0);
    }

    public TextureAtlasSprite getParticleIcon(Item item, int meta)
    {
        return this.getItemModel(new ItemStack(item, 1, meta)).getBaseTexture();
    }

    public IBakedModel getItemModel(ItemStack stack)
    {
        Item item = stack.getItem();
        IBakedModel ibakedmodel = this.simpleShapesCache.get(Integer.valueOf(this.getIndex(item, 
        		stack.isItemStackDamageable() ? 0 : stack.getMetadata())));

        if (ibakedmodel == null)
        {
            ItemMeshDefinition itemmeshdefinition = this.shapers.get(item);

            if (itemmeshdefinition != null)
            {
                ibakedmodel = this.modelManager.getModel(itemmeshdefinition.getModelLocation(stack));
            }
        }

        if (ibakedmodel == null)
        {
            ibakedmodel = this.modelManager.getMissingModel();
        }

        return ibakedmodel;
    }

    private int getIndex(Item item, int meta)
    {
        return ItemRegistry.getIdFromItem(item) << 16 | meta;
    }

//    public void register(Item item, int meta, ResourceLocation location)
//    {
//        this.simpleShapes.put(Integer.valueOf(this.getIndex(item, meta)), location);
//        this.simpleShapesCache.put(Integer.valueOf(this.getIndex(item, meta)), this.modelManager.getModel(location));
//    }

//    public void register(Item item, ItemMeshDefinition definition)
//    {
//        this.shapers.put(item, definition);
//    }

//    public void registerItem(Item itm, int subType, String identifier)
//    {
//        this.register(itm, subType, new ResourceLocation(identifier, "inventory"));
////        String id = ItemRegistry.getNameFromItem(itm).toString();
////        if(id.equals(identifier)) {
//////        	throw new IllegalArgumentException("Duplikat: " + identifier);
////        	Log.DATA.info("MATCHING: " + ItemRegistry.getNameFromItem(itm) + ":" + subType);
////        }
////        else {
////        	Log.DATA.info("MISMATCH: " + ItemRegistry.getNameFromItem(itm) + ":" + subType + ": " + identifier);
////        }
//    }

//    public void registerBlock(Block blk, int subType, String identifier)
//    {
//        this.registerItem(ItemRegistry.getItemFromBlock(blk), subType, identifier);
//    }
//
//    public void registerBlock(Block blk, String identifier)
//    {
//        this.registerBlock(blk, 0, identifier);
//    }
//
//    public void registerItem(Item itm, String identifier)
//    {
//        this.registerItem(itm, 0, identifier);
//    }

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

    public void rebuildCache()
    {
        this.simpleShapesCache.clear();
        List<ItemStack> stacks = Lists.newArrayList();
        for(Item item : ItemRegistry.REGISTRY) {
        	if(this.shapers.containsKey(item))
        		continue;
        	item.getRenderItems(item, stacks);
        	for(ItemStack stack : stacks) {
        		this.simpleShapesCache.put(this.getIndex(item, stack.getMetadata()), 
        				this.modelManager.getModel("item/" +
						ItemRegistry.getNameFromItem(item).toString() + "#" + stack.getMetadata() + '#' + "inventory"));
        	}
        	stacks.clear();
        }

//        for (Entry<Integer, ResourceLocation> entry : this.simpleShapes.entrySet())
//        {
//            this.simpleShapesCache.put(entry.getKey(), this.modelManager.getModel((ResourceLocation)entry.getValue()));
//        }
    }
}
