package common.item.tool;

import java.util.List;

import common.color.DyeColor;
import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityFireCharge;
import common.item.Item;
import common.item.ItemStack;
import common.item.material.ItemDye;
import common.model.Model;
import common.model.ModelProvider;
import common.tags.TagObject;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Vec3;
import common.world.World;

public class ItemFireworkCharge extends Item
{
	private static final String[] EXPLOSIONS = new String[] {"Kleine Kugel", "Große Kugel", "Stern", "Pentagramm", "Explosion"};
	
    public int getColorFromItemStack(ItemStack stack, int renderPass)
    {
        if (renderPass != 1)
        {
            return super.getColorFromItemStack(stack, renderPass);
        }
        else
        {
            if (stack.hasTagCompound())
            {
                TagObject nbttagcompound = stack.getTagCompound().getObject("Explosion");

                if (nbttagcompound != null)
                {
                	if(nbttagcompound.hasIntArray("Colors")) {
                		int[] aint = nbttagcompound.getIntArray("Colors");

                        if (aint.length == 1)
                        {
                            return aint[0];
                        }
                        else
                        {
                            int i = 0;
                            int j = 0;
                            int k = 0;

                            for (int l : aint)
                            {
                                i += (l & 16711680) >> 16;
                                j += (l & 65280) >> 8;
                                k += (l & 255) >> 0;
                            }

                            i = i / aint.length;
                            j = j / aint.length;
                            k = k / aint.length;
                            return i << 16 | j << 8 | k;
                        }
                	}
                }
            }
            return 9079434;
        }
    }

    /**
     * allows items to add custom lines of information to the mouseover description
     */
    public void addInformation(ItemStack stack, EntityNPC playerIn, List<String> tooltip)
    {
        if (stack.hasTagCompound())
        {
            TagObject nbttagcompound = stack.getTagCompound().getObject("Explosion");

            if (nbttagcompound != null)
            {
                addExplosionInfo(nbttagcompound, tooltip);
            }
        }
    }

    public static void addExplosionInfo(TagObject nbt, List<String> tooltip)
    {
        byte b0 = nbt.getByte("Type");

        if (b0 >= 0 && b0 <= 4)
        {
            tooltip.add(EXPLOSIONS[b0]);
        }
        else
        {
            tooltip.add("Unbekannte Form");
        }

        int[] aint = nbt.getIntArray("Colors");

        if (aint.length > 0)
        {
            boolean flag = true;
            String s = "";

            for (int i : aint)
            {
                if (!flag)
                {
                    s = s + ", ";
                }

                flag = false;
                boolean flag1 = false;

                for (int j = 0; j < ItemDye.FIREWORK_COLORS.length; ++j)
                {
                    if (i == ItemDye.FIREWORK_COLORS[j])
                    {
                        flag1 = true;
                        s = s + DyeColor.byDyeDamage(j).getDisplay();
                        break;
                    }
                }

                if (!flag1)
                {
                    s = s + "Benutzerdefiniert";
                }
            }

            tooltip.add(s);
        }

        int[] aint1 = nbt.getIntArray("FadeColors");

        if (aint1.length > 0)
        {
            boolean flag2 = true;
            String s1 = "Übergang zu ";

            for (int l : aint1)
            {
                if (!flag2)
                {
                    s1 = s1 + ", ";
                }

                flag2 = false;
                boolean flag5 = false;

                for (int k = 0; k < 16; ++k)
                {
                    if (l == ItemDye.FIREWORK_COLORS[k])
                    {
                        flag5 = true;
                        s1 = s1 + DyeColor.byDyeDamage(k).getDisplay();
                        break;
                    }
                }

                if (!flag5)
                {
                    s1 = s1 + "Benutzerdefiniert";
                }
            }

            tooltip.add(s1);
        }

        boolean flag3 = nbt.getBool("Trail");

        if (flag3)
        {
            tooltip.add("Schweif");
        }

        boolean flag4 = nbt.getBool("Flicker");

        if (flag4)
        {
            tooltip.add("Funkeln");
        }
    }
    
//    public Set<String> getValidTags() {
//    	return Sets.newHashSet("Explosion");
//    }

    public Model getModel(ModelProvider provider, String name) {
    	return provider.getModel(this.getTransform(), "firework_charge", "firework_charge_overlay");
    }
    
    public ItemStack dispenseStack(World world, TileEntity source, Vec3 position, BlockPos blockpos, Facing facing, ItemStack stack) {
        double d0 = position.xCoord + (double)((float)facing.getFrontOffsetX() * 0.3F);
        double d1 = position.yCoord + (double)((float)facing.getFrontOffsetY() * 0.3F);
        double d2 = position.zCoord + (double)((float)facing.getFrontOffsetZ() * 0.3F);
        double d3 = world.rand.gaussian() * 0.05D + (double)facing.getFrontOffsetX();
        double d4 = world.rand.gaussian() * 0.05D + (double)facing.getFrontOffsetY();
        double d5 = world.rand.gaussian() * 0.05D + (double)facing.getFrontOffsetZ();
        world.spawnEntityInWorld(new EntityFireCharge(world, d0, d1, d2, d3, d4, d5));
        stack.splitStack(1);
        return stack;
	}

	public int getDispenseSoundId() {
		return 1009;
	}
}
