package common.item.block;

import java.util.List;

import common.block.Block;
import common.block.tile.BlockBannerStanding;
import common.block.tile.BlockStandingSign;
import common.block.tile.BlockWallSign;
import common.color.DyeColor;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.Items;
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.tags.TagObject;
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 Item
{
	private final BlockBannerStanding block;
	
    public ItemBanner(BlockBannerStanding block)
    {
    	this.block = block;
        this.setTab(CheatTab.DECORATION);
    }
    
    public static ItemStack getColoredBanner(DyeColor color) {
    	return Items.banner.getColored(color);
    }
    
    private ItemStack getColored(DyeColor color) {
        TagObject tag = new TagObject();
        TileEntityBanner.setBaseColorAndPatterns(tag, color.getDyeDamage(), null);
        TagObject tile = new TagObject();
        tile.setObject("BlockEntityTag", tag);
        ItemStack stack = new ItemStack(this);
        stack.setTagCompound(tile);
        return stack;
    }

    public Block getBlock()
    {
        return this.block;
    }

    /**
     * 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 (!this.block.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.size;
                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)
    {
        TagObject nbttagcompound = stack.getSubCompound("BlockEntityTag", false);

        if (nbttagcompound != null && nbttagcompound.hasList("Patterns"))
        {
        	List<TagObject> nbttaglist = nbttagcompound.getList("Patterns");

            for (int i = 0; i < nbttaglist.size() && i < 6; ++i)
            {
                TagObject nbttagcompound1 = nbttaglist.get(i);
                DyeColor enumdyecolor = DyeColor.byDyeDamage(nbttagcompound1.getInt("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 color : DyeColor.values())
        {
            subItems.add(this.getColored(color));
        }
    }

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

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

        if (nbttagcompound != null && nbttagcompound.hasInt("Base"))
        {
            enumdyecolor = DyeColor.byDyeDamage(nbttagcompound.getInt("Base"));
        }
        else
        {
            enumdyecolor = DyeColor.WHITE;
        }

        return enumdyecolor;
    }

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

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