package hmggirlfront.gui;

import java.io.IOException;

import org.lwjgl.opengl.GL11;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.tile.TileEntityM_Crafting;
import hmggirlfront.items.ItemDollCore;
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.RenderItem;
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.Item;
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 GFBlockGuiInventory extends GuiContainer
{
    private static final ResourceLocation FURNACE_GUI_TEXTURES = new ResourceLocation("hmggirlfront:textures/gui/m_crafting.png");
    /** The player inventory bound to this GUI. */
    private final InventoryPlayer playerInventory;
    private final IInventory tileFurnace;
    private final TileEntityM_Crafting tile;

    private GuiButton open;
    private GuiButton setitem;
    
    private GuiButton go;
    
    public GFBlockGuiInventory(InventoryPlayer playerInv, IInventory furnaceInv, TileEntityM_Crafting t)
    {
        super(new GFBlockContainerInventory(playerInv, furnaceInv));
        this.playerInventory = playerInv;
        this.tileFurnace = furnaceInv;
        this.tile = t;
    }
    public void initGui()
    {
    	super.initGui();
    	this.open = this.addButton(new GuiButton(0, this.width / 2 + 90, this.height / 2 - 80, 20, 15, I18n.format("next")));
    	this.setitem = this.addButton(new GuiButton(1, this.width / 2 + 90, this.height / 2 + 40, 20, 15, I18n.format("SET")));
    	
    	//this.go = this.addButton(new GuiButton(2, this.width / 2 + 0, this.height / 2 + 0, 20, 15, I18n.format("GO")));
    }
    /**
     * 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);
    }

    protected void actionPerformed(GuiButton button) throws IOException
    {
    	 if (button.id == 0)
         {
    		 if(tile.gui_id < 6) {
    			 ++tile.gui_id;
    		 }else {
    			 tile.gui_id = 0;
    		 }
    		 GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(401, tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ()));
         }
    	 if (button.id == 1)
         {
    		 GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(404, tile.getPos().getX(), tile.getPos().getY(), tile.getPos().getZ()));
    		 int hp = 1;
    		 int bullet = 1;
    		 int food = 1;
    		 int parts = 1;
    		 if (tile.gui_id == 1) {
    			 hp = 10;
        		 bullet = 10;
        		 food = 10;
        		 parts = 1;
    		 }
    		 if (tile.gui_id == 2) {
    			 hp = 10;
        		 bullet = 10;
        		 food = 10;
        		 parts = 10;
    		 }
    		 if (tile.gui_id == 3) {
    			 hp = 10;
        		 bullet = 10;
        		 food = 10;
        		 parts = 22;
    		 }
    		 if (tile.gui_id == 4) {
    			 hp = 41;
        		 bullet = 51;
        		 food = 1;
        		 parts = 1;
    		 }
    		 if (tile.gui_id == 5) {
    			 hp = 41;
        		 bullet = 31;
        		 food = 1;
        		 parts = 1;
    		 }
    		 if (tile.gui_id == 6) {
    			 hp = 41;
        		 bullet = 51;
        		 food = 1;
        		 parts = 64;
    		 }
    		 for(int is = 0; is < playerInventory.mainInventory.size(); ++is){
    			 ItemStack itemi = playerInventory.mainInventory.get(is);
    			 if(!itemi.isEmpty()) {
    				 if(itemi.getItem() == mod_GirlFront.idoll_hp) {
    					 if(tile.furnaceItemStacks.get(0).getCount() < hp && tile.furnaceItemStacks.get(0).getCount() == 0) {
    						 if(itemi.getCount() < hp) {
    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
    							 tile.furnaceItemStacks.set(0, itemi);
    						 }else {
    							 itemi.shrink(hp);
    							 tile.furnaceItemStacks.set(0, new ItemStack(mod_GirlFront.idoll_hp, hp));
    						 }
    					 }
    				 }
    				 if(itemi.getItem() == mod_GirlFront.idoll_bullet) {
    					 if(tile.furnaceItemStacks.get(1).getCount() < bullet && tile.furnaceItemStacks.get(1).getCount() == 0) {
    						 if(itemi.getCount() < bullet) {
    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
    							 tile.furnaceItemStacks.set(1, itemi);
    						 }else {
    							 itemi.shrink(bullet);
    							 tile.furnaceItemStacks.set(1, new ItemStack(mod_GirlFront.idoll_bullet, bullet));
    						 }
    					 }
    				 }
    				 if(itemi.getItem() == mod_GirlFront.idoll_food) {
    					 if(tile.furnaceItemStacks.get(2).getCount() < food && tile.furnaceItemStacks.get(2).getCount() == 0) {
    						 if(itemi.getCount() < food) {
    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
    							 tile.furnaceItemStacks.set(2, itemi);
    						 }else {
    							 itemi.shrink(food);
    							 tile.furnaceItemStacks.set(2, new ItemStack(mod_GirlFront.idoll_food, food));
    						 }
    					 }
    				 }
    				 if(itemi.getItem() == mod_GirlFront.idoll_parts) {
    					 if(tile.furnaceItemStacks.get(3).getCount() < parts && tile.furnaceItemStacks.get(3).getCount() == 0) {
    						 if(itemi.getCount() < parts) {
    							 playerInventory.mainInventory.set(is, new ItemStack((Item)null));
    							 tile.furnaceItemStacks.set(3, itemi);
    						 }else {
    							 itemi.shrink(parts);
    							 tile.furnaceItemStacks.set(3, new ItemStack(mod_GirlFront.idoll_parts, parts));
    						 }
    					 }
    				 }
    			 }
    		 }
         }
    	 
    	 /*if (button.id == 2)
         {
    		 
         }*/
    }
    
    /**
     * 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_Crafting.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 off = (ItemStack)tile.furnaceItemStacks.get(7);
				if(off.getItem() instanceof ItemDollCore) {
	        		ItemDollCore core = (ItemDollCore) off.getItem();
	        		ct = core.craft_time;
	        	}
				double count = tile.getClientCrafttime(tile);
				int time = (ct - (int)count) / 20;
				int hour = time / 3600;
			    int min = (time%3600) / 60;
			    int sec = time % 60;
				String d1 = String.format("%1$3d", hour);
				String d2 = String.format("%1$3d", min);
				String d3 = String.format("%1$3d", sec);
				if(ct > 0) {
					//fontReader.drawString("Time:    "  + d1,  i + 127, j + 58, 4210752);
					fontReader.drawString("Time",  i + 135, j + 58, 4210752);
					fontReader.drawString(d1 + ":" + d2 + ":" + d3,  i + 127, j + 66, 4210752);
				}
				else {
					fontReader.drawString("Ready",  i + 135, j + 58, 4210752);
				}
			}
		}
		GL11.glPopMatrix();
		
		{
			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);
			RenderItem renderitem = mc.getRenderItem();
			int x = (this.width - this.xSize) / 2;
			int y = (this.height - this.ySize) / 2;
			ItemStack hp = new ItemStack(mod_GirlFront.idoll_hp);
			ItemStack bullet = new ItemStack(mod_GirlFront.idoll_bullet);
			ItemStack food = new ItemStack(mod_GirlFront.idoll_food);
			ItemStack parts = new ItemStack(mod_GirlFront.idoll_parts);
			if(!hp.isEmpty() && !hp.isEmpty() && !hp.isEmpty() && !hp.isEmpty()) {
				renderitem.renderItemIntoGUI(hp, x + 180, y + 40);
				renderitem.renderItemIntoGUI(bullet, x + 180, y + 60);
				renderitem.renderItemIntoGUI(food, x + 180, y + 80);
				renderitem.renderItemIntoGUI(parts, x + 180, y + 100);
				if (tile.gui_id == 0) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_m1911);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("1", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 110, 0xFFFFFF);
				}
				if (tile.gui_id == 1) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_mp40);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("10", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 110, 0xFFFFFF);
				}
				if (tile.gui_id == 2) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_m4a1);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("10", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 110, 0xFFFFFF);
				}
				if (tile.gui_id == 3) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_supersass);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("10", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("10", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("22", x + 200, y + 110, 0xFFFFFF);
				}
				if (tile.gui_id == 4) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_gr_mg4);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("41", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("51", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 110, 0xFFFFFF);
				}
				if (tile.gui_id == 5) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_m1014);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("41", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("31", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 110, 0xFFFFFF);
				}
				if (tile.gui_id == 6) {
					{
						ItemStack gun = new ItemStack(mod_GirlFront.core_m79);
						if(!gun.isEmpty())renderitem.renderItemIntoGUI(gun, x + 180, y + 20);
					}
					fontReader.drawString("41", x + 200, y + 50, 0xFFFFFF);
					fontReader.drawString("51", x + 200, y + 70, 0xFFFFFF);
					fontReader.drawString("1", x + 200, y + 90, 0xFFFFFF);
					fontReader.drawString("64", x + 200, y + 110, 0xFFFFFF);
				}
			}
			
			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;
    }
}