package common.item.tool;

import java.util.Collection;
import java.util.Map;

import common.collect.Maps;
import common.color.TextColor;
import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityDie;
import common.init.SoundEvent;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.model.Model;
import common.model.ModelProvider;
import common.model.Transforms;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Pair;
import common.util.Vec3;
import common.world.World;

public class ItemDie extends Item
{
	public static final Pair<Integer, TextColor>[] DIE_SIDES = new Pair[] {
		new Pair(4, TextColor.DGREEN), 
		new Pair(6, TextColor.NEON), 
		new Pair(8, TextColor.DMAGENTA), 
		new Pair(10, TextColor.MAGENTA), 
		new Pair(12, TextColor.DRED), 
		new Pair(20, TextColor.ORANGE)
	};
	public static final Map<Integer, ItemDie> DICE = Maps.newTreeMap();
	
	private final int sides;
	private final TextColor color;
	
	public static final ItemDie getBySides(int sides) {
		ItemDie die = DICE.get(sides);
		return die == null ? DICE.get(6) : die;
	}
	
	public static Collection<ItemDie> getDieItems() {
		return DICE.values();
	}
	
    public ItemDie(int sides, TextColor color)
    {
    	this.sides = sides;
    	this.color = color;
        this.setTab(CheatTab.TOOLS);
        DICE.put(sides, this);
    }
    
    public int getSides() {
    	return this.sides;
    }
    
    public TextColor getDieColor() {
    	return this.color;
    }
    
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn)
    {
//        if (!playerIn.creative)
//        {
        --itemStackIn.size;
//        }

        worldIn.playSoundAtEntity(playerIn, SoundEvent.THROW, 0.5F);

        if (!worldIn.client)
        {
            worldIn.spawnEntityInWorld(new EntityDie(worldIn, playerIn, this.sides, false)); // playerIn.creative));
        }

//        playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);
        return itemStackIn;
    }
    
    public String getDisplay(ItemStack stack)
    {
        return super.getDisplay(stack) + " W" + this.sides;
    }

    public Transforms getTransform() {
    	return Transforms.DICE;
    }

    public Model getModel(ModelProvider provider, String name) {
    	return provider.getModel(provider.getModel("items/die_d" + this.sides + "_side").add().nswe()
    			.du("items/die_d" + this.sides + "_top"), this.getTransform());
    }
    
	public ItemStack dispenseStack(World world, TileEntity source, Vec3 position, BlockPos blockpos, Facing facing, ItemStack stack) {
		return this.dispenseProjectile(world, facing, stack, new EntityDie(world, position.xCoord, position.yCoord, position.zCoord, this.sides), 0.275f, 30.0f);
	}

	public int getDispenseSoundId() {
		return 1002;
	}
}
