package common.item;

import java.util.List;

import common.block.BlockStandingSign;
import common.block.BlockWallSign;
import common.color.DyeColor;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.model.ItemMeshDefinition;
import common.model.Model;
import common.model.ModelProvider;
import common.nbt.NBTTagCompound;
import common.nbt.NBTTagList;
import common.tileentity.TileEntity;
import common.tileentity.TileEntityBanner;
import common.util.BlockPos;
import common.util.ExtMath;
import common.util.Facing;
import common.world.World;

public class ItemBanner extends ItemBlock
{
    public ItemBanner()
    {
        super(Blocks.banner);
        this.setTab(CheatTab.tabDeco);
        this.setHasSubtypes(true);
        this.setMaxDamage(0);
    }

    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        if (side == Facing.DOWN)
        {
            return false;
        }
        else if (!worldIn.getState(pos).getBlock().getMaterial().isSolid())
        {
            return false;
        }
        else
        {
            pos = pos.offset(side);

            if (!playerIn.canPlayerEdit(pos, side, stack))
            {
                return false;
            }
            else if (!Blocks.banner.canPlaceBlockAt(worldIn, pos))
            {
                return false;
            }
            else if (worldIn.client)
            {
                return true;
            }
            else
            {
                if (side == Facing.UP)
                {
                    int i = ExtMath.floord((double)((playerIn.rotYaw + 180.0F) * 16.0F / 360.0F) + 0.5D) & 15;
                    worldIn.setState(pos, Blocks.banner.getState().withProperty(BlockStandingSign.ROTATION, Integer.valueOf(i)), 3);
                }
                else
                {
                    worldIn.setState(pos, Blocks.wall_banner.getState().withProperty(BlockWallSign.FACING, side), 3);
                }

                --stack.stackSize;
                TileEntity tileentity = worldIn.getTileEntity(pos);

                if (tileentity instanceof TileEntityBanner)
                {
                    ((TileEntityBanner)tileentity).setItemValues(stack);
                }

                return true;
            }
        }
    }

    public String getDisplay(ItemStack stack)
    {
//        String s = "item.banner.";
        DyeColor enumdyecolor = this.getBaseColor(stack);
//        s = s +  + ".name";
        return enumdyecolor.getSubject(0) + " " + super.getDisplay(stack);
    }

    /**
     * allows items to add custom lines of information to the mouseover description
     */
    public void addInformation(ItemStack stack, EntityNPC playerIn, List<String> tooltip)
    {
        NBTTagCompound nbttagcompound = stack.getSubCompound("BlockEntityTag", false);

        if (nbttagcompound != null && nbttagcompound.hasKey("Patterns"))
        {
            NBTTagList nbttaglist = nbttagcompound.getTagList("Patterns", 10);

            for (int i = 0; i < nbttaglist.tagCount() && i < 6; ++i)
            {
                NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
                DyeColor enumdyecolor = DyeColor.byDyeDamage(nbttagcompound1.getInteger("Color"));
                TileEntityBanner.EnumBannerPattern pattern = TileEntityBanner.EnumBannerPattern.getPatternByID(nbttagcompound1.getString("Pattern"));

                if (pattern != null)
                {
                    tooltip.add(String.format(pattern.getDisplay(), pattern.isColorLowercase() ? 
                    		enumdyecolor.getSubject(pattern.getDisplayType()).toLowerCase() : enumdyecolor.getSubject(pattern.getDisplayType())));
                }
            }
        }
    }

    public int getColorFromItemStack(ItemStack stack, int renderPass)
    {
        if (renderPass == 0)
        {
            return 16777215;
        }
        else
        {
            DyeColor enumdyecolor = this.getBaseColor(stack);
            return enumdyecolor.getColor();
        }
    }

    /**
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
     */
    public void getSubItems(Item itemIn, CheatTab tab, List<ItemStack> subItems)
    {
        for (DyeColor enumdyecolor : DyeColor.values())
        {
            NBTTagCompound nbttagcompound = new NBTTagCompound();
            TileEntityBanner.setBaseColorAndPatterns(nbttagcompound, enumdyecolor.getDyeDamage(), (NBTTagList)null);
            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
            nbttagcompound1.setTag("BlockEntityTag", nbttagcompound);
            ItemStack itemstack = new ItemStack(itemIn, 1, enumdyecolor.getDyeDamage());
            itemstack.setTagCompound(nbttagcompound1);
            subItems.add(itemstack);
        }
    }

    /**
     * gets the tab this item is displayed on
     */
    public CheatTab getTab()
    {
        return CheatTab.tabDeco;
    }

    private DyeColor getBaseColor(ItemStack stack)
    {
        NBTTagCompound nbttagcompound = stack.getSubCompound("BlockEntityTag", false);
        DyeColor enumdyecolor = null;

        if (nbttagcompound != null && nbttagcompound.hasKey("Base"))
        {
            enumdyecolor = DyeColor.byDyeDamage(nbttagcompound.getInteger("Base"));
        }
        else
        {
            enumdyecolor = DyeColor.byDyeDamage(stack.getMetadata());
        }

        return enumdyecolor;
    }
    
//    protected boolean validateNbt(NBTTagCompound tag) {
//    	if(tag.hasKey("BlockEntityTag")) {
//    		if(!tag.hasKey("BlockEntityTag", 10)) {
//        		return false;
//        	}
//    		NBTTagCompound etag = tag.getCompoundTag("BlockEntityTag");
//            if(etag.hasKey("Patterns")) {
//            	if(!etag.hasKey("Patterns", 9)) {
//            		return false;
//            	}
//                NBTTagList patterns = etag.getTagList("Patterns", 10);
//                if(patterns.tagCount() > 16) {
//                	NBTTagList npatterns = new NBTTagList();
//                	for(int z = 0; z < 16; z++) {
//                		npatterns.appendTag(patterns.get(z));
//                	}
//                	etag.setTag("Patterns", npatterns);
//                }
//            }
//        	if(etag.hasKey("Base")) {
//    			if(!etag.hasKey("Base", 3)) {
//        			return false;
//        		}
//    		}
//    	}
//    	return true;
//    }
    
    public ItemMeshDefinition getMesher() {
    	return new ItemMeshDefinition()
        {
    		public String getModelLocation(ItemStack stack)
            {
                return "item/banner#0" + '#' + "inventory";
            }
        };
    }
    
    public void getRenderItems(Item itemIn, List<ItemStack> subItems) {
    	subItems.add(new ItemStack(itemIn, 1, 0));
    }

    public Model getModel(ModelProvider provider, String name, int meta) {
    	return provider.getModel(provider.getEntityModel(), this.getTransform());
    }
}
