package hmggirlfront.gui;

import java.awt.Color;
import java.io.IOException;

import org.lwjgl.opengl.GL11;

import hmggirlfront.block.tile.TileEntityM_Restoring;
import hmggirlfront.items.ItemDollGunBase;
import hmggirlfront.network.GFMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GFBlockRestoringGuiInventory extends GuiContainer
{
    private static final ResourceLocation FURNACE_GUI_TEXTURES = new ResourceLocation("hmggirlfront:textures/gui/m_restoring.png");
    /** The player inventory bound to this GUI. */
    private final InventoryPlayer playerInventory;
    private final IInventory tileFurnace;
    private final TileEntityM_Restoring tile;
    
    private GuiButton go_craft;
    
    public GFBlockRestoringGuiInventory(InventoryPlayer playerInv, IInventory furnaceInv, TileEntityM_Restoring t)
    {
        super(new GFBlockRestoringContainerInventory(playerInv, furnaceInv));
        this.playerInventory = playerInv;
        this.tileFurnace = furnaceInv;
        this.tile = t;
    }
    public void initGui()
    {
    	super.initGui();
    	this.go_craft = this.addButton(new GuiButton(0, this.width / 2 - 10, this.height / 2 - 20, 40, 20, I18n.format("Restoring")));
    }
    protected void actionPerformed(GuiButton button) throws IOException
    {
    	 if (button.id == 0)
         {
    		 tile.go_craft = true;
    		 GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(411, tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ()));
         }
    }
    
    /**
     * Draws the screen and all the components in it.
     */
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
    {
        this.drawDefaultBackground();
        super.drawScreen(mouseX, mouseY, partialTicks);
        this.renderHoveredToolTip(mouseX, mouseY);
    }

    
    
    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items)
     */
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
    {
        String s = this.tileFurnace.getDisplayName().getUnformattedText();
        this.fontRenderer.drawString(s, this.xSize / 2 - this.fontRenderer.getStringWidth(s) / 2, 6, 4210752);
        this.fontRenderer.drawString(this.playerInventory.getDisplayName().getUnformattedText(), 8, this.ySize - 96 + 2, 4210752);
    }

    /**
     * Draws the background layer of this container (behind the items).
     */
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
    {
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(FURNACE_GUI_TEXTURES);
        int i = (this.width - this.xSize) / 2;
        int j = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
        //this.drawTexturedModalRect(i, j, 0, 0, 220, this.ySize);

        if (TileEntityM_Restoring.isBurning(this.tileFurnace))
        {
            int k = this.getBurnLeftScaled(13);
            this.drawTexturedModalRect(i + 56, j + 36 + 12 - k, 176, 12 - k, 14, k + 1);
        }

        int l = this.getCookProgressScaled(24);
        this.drawTexturedModalRect(i + 79, j + 34, 176, 14, l + 1, 16);
        
        FontRenderer fontReader = mc.fontRenderer;
		mc.fontRenderer.setUnicodeFlag(mc.isUnicode());
		GL11.glPushMatrix();
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA,
				GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE,
				GlStateManager.DestFactor.ZERO);
		mc.renderEngine.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
		{
			if(tile != null){
				int ct = 0;
				
				ItemStack gun = (ItemStack)tile.furnaceItemStacks.get(0);
				ItemStack core = (ItemStack)tile.furnaceItemStacks.get(1);
				if(gun.getItem() instanceof ItemDollGunBase) {
					ItemDollGunBase dollgun = (ItemDollGunBase) gun.getItem();
					int core_kazu = (dollgun.doll_d_core + 1)*2;
					String d1 = String.format("%1$3d", core_kazu);
					int color = Color.RED.getRGB();
					if(!core.isEmpty()) {
						if(core.getCount() >= core_kazu) {
							color = 0X00FF00;
						}
					}
					fontReader.drawString("NEED CORE:" + d1,  i + 80, j + 50, color);
				}
				
			}
		}
		GL11.glPopMatrix();
    }

    private int getCookProgressScaled(int pixels)
    {
        int i = this.tileFurnace.getField(2);
        int j = this.tileFurnace.getField(3);
        return j != 0 && i != 0 ? i * pixels / j : 0;
    }

    private int getBurnLeftScaled(int pixels)
    {
        int i = this.tileFurnace.getField(1);

        if (i == 0)
        {
            i = 200;
        }

        return this.tileFurnace.getField(0) * pixels / i;
    }
}