package hmggirlfront.gui;
 
import java.awt.Color;
import java.io.IOException;

import org.lwjgl.opengl.GL11;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
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.gui.inventory.GuiInventory;
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.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
 
public class GFGuiInventoryItem extends GuiContainer
{
    //private static final ResourceLocation texture = new ResourceLocation("textures/gui/container/generic_54.png");
    private static final ResourceLocation texture = new ResourceLocation("hmggirlfront:textures/gui/dollgui.png");
    /** The mouse x-position recorded during the last rendered frame. */
    private float mousePosx;
    /** The mouse y-position recorded during the last renderered frame. */
    private float mousePosY;
    private static EntityPlayer player;
    private static EntityDoll doll;
    
    private GuiButton skill;
 
    public GFGuiInventoryItem(EntityPlayer p, EntityDoll d)
    {
        super(new GFContainerInventory());
        //this.ySize = 222;
        player = p;
        doll = d;
    }
    
    public void initGui()
    {
    	super.initGui();
    	String on = "OFF";
    	if(!doll.doll_skill_can) {
    		on = "ON";
    	}
    	this.skill = this.addButton(new GuiButton(0, this.width / 2 + 65, this.height / 2 - 13, 30, 18, I18n.format("ON/OFF")));
    }
    
	/*
        ChestとかInventoryとか文字を描画する
     */
    @Override
    protected void drawGuiContainerForegroundLayer(int x, int p_146979_2_)
    {
    	
        //描画する文字, X, Y, 色
        //this.fontRenderer.drawString("AR Attachment", 8, 6, 4210752);
        //this.fontRenderer.drawString("Inventory", 8, this.ySize - 96 + 2, 4210752);
        if(doll != null) {
        	int k = (this.width - this.xSize) / 2;
            int l = (this.height - this.ySize) / 2;
        	
        	//this.fontRenderer.drawString(String.format("%1$3d", doll.getRemain_A()), 50, this.ySize - 100 + 2, 4210752);
        }
    }
 
    protected void actionPerformed(GuiButton button) throws IOException
    {
    	 if (button.id == 0)
         {
    		 if(doll.doll_skill_can) {
    			 doll.doll_skill_can = false;
    		 }else {
    			 doll.doll_skill_can = true;
    		 }
    		 GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(408, doll.getEntityId()));
         }
    }
    
    /*
        背景の描画
     */
    @Override
    protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(texture);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
        if(doll != null){
        	GL11.glPushMatrix();
        	GL11.glScalef(3F, 3F, 3F);
        	GuiInventory.drawEntityOnScreen((k+ 51)/3,( l + 140)/3, 17, (float)(k + 51) - this.mousePosx, (float)(l + 75 - 50) - this.mousePosY, doll);
        	GL11.glPopMatrix();
        }
        
        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(doll != null){
			RenderItem renderitem = mc.getRenderItem();
			{
				GL11.glPushMatrix();
				GL11.glScalef(2F, 2F, 2F);
				fontReader.drawString(doll.getCustomNameTag(), (k + 115)/2, (l + 18)/2, 0xFFFFFF);
				GL11.glPopMatrix();
				{
					String d1 = String.format("%1$3d", (int)doll.getHealth());
					String d2 = String.format("%1$3d", (int)doll.getMaxHealth());
					fontReader.drawString("HP : "  + d1 + " / " + d2,  k + 98, l + 56, 0xFFFFFF);
				}
				{
					String d2 = String.format("%1$3d", doll.getArmID_L());
					fontReader.drawString("Lv :"  + d2,  k + 60, l + 18, 0xFFFFFF);
					if(doll.getPledge() == 1){//誓約
						ItemStack item = new ItemStack(mod_GirlFront.idoll_pledge);
						if (!item.isEmpty()) {
							GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.4F);
							renderitem.renderItemIntoGUI(item, k + 140, l + 90);
							GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
						}
					}
				}
				{
					String d2 = doll.getOwner().getName();
					fontReader.drawString("Owner :"  + d2,  k + 60, l + 8, 0xFFFFFF);
				}
				{
					String d1 = String.format("%1$3d", (int)doll.getArmID_S());
					String d2 = String.format("%1$3d", doll.getArmID_L() * 5);
					fontReader.drawString("Exp : "  + d1 + " / " + d2,  k + 98, l + 62, 0xFFFFFF);
				}
				{
					String d1 = String.format("%1$3d", (int)doll.evasion_base);
					String d1range = "DEX :"  + d1;
					fontReader.drawString(d1range,  k + 100, l + 132, 0xFFFFFF);
					int base = (int)(doll.evasion - doll.evasion_base);
					String d2 = String.format("%1$2d", Math.abs(base));
					int color = Color.GREEN.getRGB();
					String plus = "+" ;
					if(base < 0) {
						color = Color.RED.getRGB();
						plus = "-" ;
					}
					fontReader.drawString(plus + d2,  k + 120, l + 141, color);
				}
				{
					String d1 = String.format("%1$3d", (int)doll.power);
					String d1range = "STR :"  + d1;
					fontReader.drawString(d1range,  k + 100, l + 114, 0xFFFFFF);
					int base = (int)(doll.power - doll.power_base);
					String d2 = String.format("%1$2d", Math.abs(base));
					int color = Color.GREEN.getRGB();
					String plus = "+" ;
					if(base < 0) {
						color = Color.RED.getRGB();
						plus = "-" ;
					}
					fontReader.drawString(plus + d2 + doll.bullet_type_am,  k + 120, l + 123, color);
				}
				{
					String d1 = String.format("%1$.2f",doll.bure);
					String d1range = new String("ACC :"  + d1);
					fontReader.drawString(d1range,  k + 140, l + 132, 0xFFFFFF);//Accuracy
					int base = (int)(doll.bure - doll.bure_base);
					String d2 = String.format("%1$2d", Math.abs(base));
					int color = Color.RED.getRGB();
					String plus = "+" ;
					if(base < 0) {
						color = Color.GREEN.getRGB();
						plus = "-" ;
					}else if(base == 0){
						color = Color.GREEN.getRGB();
						plus = "+" ;
					}
					fontReader.drawString(plus + d2,  k + 160, l + 141, color);
				}
				{
					String d1 = String.format("%1$3d", (int)doll.rpm);
					fontReader.drawString("RPM :"  + d1,  k + 140, l + 114, 0xFFFFFF);//Ｒｏｕｎｄｓ ｐｅｒ ｍｉｎｕｔｅ
				}
				{
					if(doll.doll_armor > 0) {
						String d1 = String.format("%1$3d", (int)doll.doll_armorbase);
						fontReader.drawString("ARMOR :"  + d1,  k + 100, l + 150, 0xFFFFFF);//
						int ar = doll.doll_armor - doll.doll_armorbase;
						if(ar > 0) {
							String d2 = String.format("%1$3d", (int)ar);
							fontReader.drawString("+"  + d2,  k + 120, l + 156, Color.GREEN.getRGB());//
						}
					}
				}
			}
			{
				ItemStack item = new ItemStack(doll.bullet_type);
				if (!item.isEmpty()) {
					GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.4F);
					renderitem.renderItemIntoGUI(item, k + 98, l + 72);
					GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
					String d2 = String.format("%1$3d", doll.getRemain_S());
					fontReader.drawString(d2,  k + 115, l + 80, 0xFFFFFF);
				}
			}
			{
				ItemStack item = new ItemStack(mod_GirlFront.idoll_food);
				if (!item.isEmpty()) {
					GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.4F);
					renderitem.renderItemIntoGUI(item, k + 98, l + 90);
					GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
					String d2 = String.format("%1$3d", doll.getRemain_A());
					fontReader.drawString(d2,  k + 115, l + 98, 0xFFFFFF);
				}
			}
			
			{//head
				ItemStack item =  doll.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
				if (!item.isEmpty()) {
					renderitem.renderItemIntoGUI(item, k + 98, l + 36);
				}
			}
			{//chest
				ItemStack item =  doll.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
				if (!item.isEmpty()) {
					renderitem.renderItemIntoGUI(item, k + 116, l + 36);
				}
			}
			{//legs
				ItemStack item =  doll.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
				if (!item.isEmpty()) {
					renderitem.renderItemIntoGUI(item, k + 134, l + 36);
				}
			}
			{//feet
				ItemStack item =  doll.getItemStackFromSlot(EntityEquipmentSlot.FEET);
				if (!item.isEmpty()) {
					renderitem.renderItemIntoGUI(item, k + 152, l + 36);
				}
			}
			{
				ItemStack item = new ItemStack((Item)null);
				if(doll.doll_skill == 1){
					item = new ItemStack(mod_GirlFront.idoll_grenade_n);
				}
				if(doll.doll_skill == 2){
					item = new ItemStack(mod_GirlFront.idoll_grenade_f);
				}
				if(doll.doll_skill == 3){
					item = new ItemStack(mod_GirlFront.idoll_grenade_s);
				}
				if(doll.doll_skill == 4){
					item = new ItemStack(mod_GirlFront.idoll_grenade_fb);
				}
				if(doll.doll_skill == 10){
					item = new ItemStack(mod_GirlFront.idoll_bullet_40);
				}
				if (!item.isEmpty()) {
					GL11.glColor4f(1.0F, 1.0F, 1.0F, 0.4F);
					renderitem.renderItemIntoGUI(item, k + 134, l + 72);
					GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
				}
			}
		}
		GL11.glPopMatrix();
    }
    
    /**
     * Draws the screen and all the components in it.
     */
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
    {
        this.drawDefaultBackground();
        this.mousePosx = (float)mouseX;
        this.mousePosY = (float)mouseY;
        super.drawScreen(mouseX, mouseY, partialTicks);
        this.renderHoveredToolTip(mouseX, mouseY);
    }
}