package client.gui.container;

import org.lwjgl.opengl.GL11;

import client.renderer.GlState;
import client.renderer.ItemRenderer;
import common.inventory.ContainerMerchant;
import common.inventory.InventoryPlayer;
import common.item.ItemStack;
import common.packet.CPacketAction;
import common.village.MerchantRecipe;
import common.village.MerchantRecipeList;
import common.world.World;

public class GuiMerchant extends GuiContainer
{
//    private static final String MERCHANT_GUI_TEXTURE = "textures/gui/trading.png";
    
    private int selectedMerchantRecipe;
    private String chatComponent;

    public GuiMerchant(InventoryPlayer inv, String name, World worldIn)
    {
        super(new ContainerMerchant(inv, null, worldIn));
        this.chatComponent = name != null ? name : "NSC";
    }

    /**
     * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
     * window resizes, the buttonList is cleared beforehand.
     */
//    public void initGui()
//    {
//        super.initGui();
//    }
    
    public void addButtons() {
    	super.addButtons();
    	this.button(120 + 27, 24, 12, 16);
    	this.button(36 - 19, 24, 12, 16);
    	this.button(36 - 1, 24 - 1, 18, 18);
    	this.button(62 - 1, 24 - 1, 18, 18);
    	this.button(120 - 1, 24 - 1, 18, 18);
    }
    
    public void mouseClicked(int mouseX, int mouseY, int mouseButton)
    {
        if (mouseButton == 0)
        {
//            int i = (this.width - this.xSize) / 2;
//            int j = (this.height - this.ySize) / 2;
            if(mouseX >= 147 && mouseX < 147 + 12 && mouseY >= 24 && mouseY < 24 + 16) {
            	this.switchTrade(true);
            	return;
            }
            else if(mouseX >= 17 && mouseX < 17 + 12 && mouseY >= 24 && mouseY < 24 + 16) {
            	this.switchTrade(false);
            	return;
            }
//            this.buttonList.add(new Button(1, i + 120 + 27, j + 24 - 1, 12, 19, ">"));
//            this.buttonList.add(new Button(2, i + 36 - 19, j + 24 - 1, 12, 19, "<"));
            
        }

        super.mouseClicked(mouseX, mouseY, mouseButton);
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY
     */
    public void drawGuiContainerForegroundLayer()
    {
        String s = this.chatComponent;
        this.drawString(s, 8, 6);
        this.drawString("Inventar", 8, this.ySize - 96 + 2);
    }
    
    public void drawOverlays() {
    	super.drawOverlays();
        MerchantRecipeList merchantrecipelist = this.getRecipes();
        if (merchantrecipelist != null && !merchantrecipelist.isEmpty()) {
            int k = this.selectedMerchantRecipe;
            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
            ItemStack itemstack = merchantrecipe.first();
            ItemStack itemstack1 = merchantrecipe.second();
            ItemStack itemstack2 = merchantrecipe.result();
            this.renderItemOverlayIntoGUI(itemstack, 36, 24, null);
            if(itemstack1 != null)
            	this.renderItemOverlayIntoGUI(itemstack1, 62, 24, null);
            this.renderItemOverlayIntoGUI(itemstack2, 120, 24, null);
        }
    }

    /**
     * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
     */
    public void switchTrade(boolean forward)
    {
        boolean flag = false;

        if (forward)
        {
            ++this.selectedMerchantRecipe;
            MerchantRecipeList merchantrecipelist = this.getRecipes();

            if (merchantrecipelist != null && this.selectedMerchantRecipe >= merchantrecipelist.size())
            {
                this.selectedMerchantRecipe = merchantrecipelist.size() - 1;
            }

            flag = true;
        }
        else
        {
            --this.selectedMerchantRecipe;

            if (this.selectedMerchantRecipe < 0)
            {
                this.selectedMerchantRecipe = 0;
            }

            flag = true;
        }

        if (flag)
        {
            ((ContainerMerchant)this.inventorySlots).setCurrentRecipeIndex(this.selectedMerchantRecipe);
//            PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
//            packetbuffer.writeInt(this.selectedMerchantRecipe);
            this.gm.getNetHandler().addToSendQueue(new CPacketAction(CPacketAction.Action.SELECT_TRADE, 
            		this.selectedMerchantRecipe));
        }
    }

//    /**
//     * Args : renderPartialTicks, mouseX, mouseY
//     */
//    protected void drawGuiContainerBackgroundLayer(int mouseX, int mouseY)
//    {
//        GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
//        this.gm.getTextureManager().bindTexture(MERCHANT_GUI_TEXTURE);
//        int i = (this.width - this.xSize) / 2;
//        int j = (this.height - this.ySize) / 2;
////        this.rect(i, j, 0, 0, this.xSize, this.ySize);
////        MerchantRecipeList merchantrecipelist = this.merchant.getRecipes(this.gm.thePlayer);
//
////        if (merchantrecipelist != null && !merchantrecipelist.isEmpty())
////        {
////            int k = this.selectedMerchantRecipe;
////
////            if (k < 0 || k >= merchantrecipelist.size())
////            {
////                return;
////            }
//
////            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
//
////            if (merchantrecipe.isRecipeDisabled())
////            {
////                this.gm.getTextureManager().bindTexture(MERCHANT_GUI_TEXTURE);
////                GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
////                GlState.disableLighting();
////                this.rect(this.guiLeft + 83, this.guiTop + 21, 212, 0, 28, 21);
////                this.rect(this.guiLeft + 83, this.guiTop + 51, 212, 0, 28, 21);
////            }
////        }
//    }

    /**
     * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
     */
    public void drawScreen(int mouseX, int mouseY)
    {
        super.drawScreen(mouseX, mouseY);
        MerchantRecipeList merchantrecipelist = this.getRecipes();

        if (merchantrecipelist != null && !merchantrecipelist.isEmpty())
        {
//            int i = (this.width - this.xSize) / 2;
//            int j = (this.height - this.ySize) / 2;
            int k = this.selectedMerchantRecipe;
            MerchantRecipe merchantrecipe = (MerchantRecipe)merchantrecipelist.get(k);
            ItemStack itemstack = merchantrecipe.first();
            ItemStack itemstack1 = merchantrecipe.second();
            ItemStack itemstack2 = merchantrecipe.result();
            GL11.glPushMatrix();
            ItemRenderer.enableGUIStandardItemLighting();
            GlState.disableLighting();
            GlState.enableRescaleNormal();
            GlState.enableColorMaterial();
            GlState.enableLighting();
            this.itemRender.zLevel = 100.0F;
            this.itemRender.renderItemAndEffectIntoGUI(itemstack, 36, 24);
//            this.itemRender.renderItemOverlays(itemstack, 36, 24);

            if (itemstack1 != null)
            {
                this.itemRender.renderItemAndEffectIntoGUI(itemstack1, 62, 24);
//                this.itemRender.renderItemOverlays(itemstack1, 62, 24);
            }

            this.itemRender.renderItemAndEffectIntoGUI(itemstack2, 120, 24);
//            this.itemRender.renderItemOverlays(itemstack2, 120, 24);
            this.itemRender.zLevel = 0.0F;
            GlState.disableLighting();

            if (this.isPointInRegion(36, 24, 16, 16, mouseX, mouseY) && itemstack != null)
            {
                this.renderToolTip(itemstack, mouseX, mouseY);
            }
            else if (itemstack1 != null && this.isPointInRegion(62, 24, 16, 16, mouseX, mouseY) && itemstack1 != null)
            {
                this.renderToolTip(itemstack1, mouseX, mouseY);
            }
            else if (itemstack2 != null && this.isPointInRegion(120, 24, 16, 16, mouseX, mouseY) && itemstack2 != null)
            {
                this.renderToolTip(itemstack2, mouseX, mouseY);
            }
//            else if (merchantrecipe.isRecipeDisabled() && (this.isPointInRegion(83, 21, 28, 21, mouseX, mouseY) || this.isPointInRegion(83, 51, 28, 21, mouseX, mouseY)))
//            {
//                this.drawCreativeTabHoveringText(I18n.format("merchant.deprecated"), mouseX, mouseY);
//            }

            GL11.glPopMatrix();
            GlState.enableLighting();
            GlState.enableDepth();
            ItemRenderer.enableStandardItemLighting();
//            int i = (this.width - this.xSize) / 2;
//            int j = (this.height - this.ySize) / 2;
//            if(mouseX >= i + 147 && mouseX < i + 147 + 12 && mouseY >= j + 23 && mouseX < j + 23 + 19) {
//            	SKC.highlight(i + 147, j + 23, 12, 19);
//            }
//            else if(mouseX >= i + 17 && mouseX < i + 17 + 12 && mouseY >= j + 23 && mouseX < j + 23 + 19) {
//            	SKC.highlight(i + 17, j + 23, 12, 19);
//            }
        }
    }

    public void setRecipes(MerchantRecipeList recipes)
    {
        ((ContainerMerchant)this.inventorySlots).getMerchantInventory().setRecipes(recipes);
    }

    private MerchantRecipeList getRecipes()
    {
        return ((ContainerMerchant)this.inventorySlots).getMerchantInventory().getRecipes();
    }

//    static class MerchantButton extends Button
//    {
//        private final boolean next;
//
//        public MerchantButton(int buttonID, int x, int y, boolean isNext)
//        {
//            super(buttonID, x, y, 12, 19, "");
//            this.next = isNext;
//        }
//
//        public void drawButton(Game gm, int mouseX, int mouseY)
//        {
//            if (this.visible)
//            {
//                gm.getTextureManager().bindTexture(GuiMerchant.MERCHANT_GUI_TEXTURE);
//                GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
//                boolean flag = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;
//                int i = 0;
//                int j = 176;
//
//                if (!this.enabled)
//                {
//                    j += this.width * 2;
//                }
//                else if (flag)
//                {
//                    j += this.width;
//                }
//
//                if (!this.next)
//                {
//                    i += this.height;
//                }
//
//                this.rect(this.xPosition, this.yPosition, j, i, this.width, this.height);
//            }
//        }
//    }
}
