package client.gui.container;

import common.color.TextColor;
import common.enchantment.Enchantment;
import common.inventory.ContainerEnchantment;
import common.inventory.InventoryPlayer;
import common.rng.Random;
import common.world.World;

public class GuiEnchant extends GuiContainer
{
    /** The ResourceLocation containing the Enchantment GUI texture location */
//    private static final String ENCHANTMENT_TABLE_GUI_TEXTURE = "textures/gui/enchanting_table.png";
    private static final String[] NAMES = "the elder scrolls klaatu berata niktu xyzzy bless curse light darkness fire air earth water hot dry cold wet ignite snuff embiggen twist shorten stretch fiddle destroy imbue galvanize enchant free limited range of towards inside sphere cube self other ball mental physical grow shrink demon elemental spirit animal creature beast humanoid undead fresh stale ".split(" ");

    /** The player inventory currently bound to this GuiEnchantment instance. */
    private final InventoryPlayer playerInventory;
    private final Random nameRand = new Random();
    private final Random random = new Random();
    private final ContainerEnchantment container;
    private final String title;
    
//    public int field_147073_u;
//    public float field_147071_v;
//    public float field_147069_w;
//    public float field_147082_x;
//    public float field_147081_y;
//    public float field_147080_z;
//    public float field_147076_A;
//    ItemStack field_147077_B;
    
    public GuiEnchant(InventoryPlayer inventory, World worldIn, String title)
    {
        super(new ContainerEnchantment(inventory, worldIn));
        this.playerInventory = inventory;
        this.container = (ContainerEnchantment)this.inventorySlots;
        this.title = title;
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY
     */
    public void drawGuiContainerForegroundLayer()
    {
        this.drawString(this.title, 12, 5);
        this.drawString(this.playerInventory.getCommandName(), 8, this.ySize - 96 + 2);
    }

//    /**
//     * Called from the main game loop to update the screen.
//     */
//    public void updateScreen()
//    {
//        super.updateScreen();
//        this.updateAnimation();
//    }

    /**
     * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
     */
    public void mouseClicked(int mouseX, int mouseY, int mouseButton)
    {
        super.mouseClicked(mouseX, mouseY, mouseButton);
//        int i = (this.width - this.xSize) / 2;
//        int j = (this.height - this.ySize) / 2;

        for (int k = 0; k < 3; ++k)
        {
            int l = mouseX - 60;
            int i1 = mouseY - (14 + 19 * k);

            if (l >= 0 && i1 >= 0 && l < 108 && i1 < 19 && this.container.enchantItem(this.gm.player, k))
            {
                this.gm.controller.sendEnchantPacket(this.container.windowId, k);
            }
        }
    }
    
    public void addButtons() {
    	super.addButtons();
    	this.button(60, 14 + 19 * 0, 108, 19);
    	this.button(60, 14 + 19 * 1, 108, 19);
    	this.button(60, 14 + 19 * 2, 108, 19);
    }

    /**
     * Args : renderPartialTicks, mouseX, mouseY
     */
    public void drawGuiContainerBackgroundLayer()
    {
        this.nameRand.setSeed((long)this.container.seed);

        for (int l = 0; l < 3; ++l)
        {
            int i1 = 60;
            int j1 = i1 + 20;
            String s = this.getRandomName();
            int l1 = this.container.mana[l];

            if (l1 == 0)
            {
                this.rect(i1, 14 + 19 * l, 108, 19, 0x404040);
            }
            else
            {
                String s1 = "" + l1;
                int i2 = 6839882;

                if (/* (k < l + 1 || */ this.gm.player.getManaPoints() < l1) // && !this.gm.thePlayer.creative)
                {
                    this.rect(i1, 14 + 19 * l, 108, 19, 0x400000);
                    this.rect(i1 + 1, 15 + 19 * l, 16, 16, 0x200000);
                    this.drawString(s, j1, 16 + 19 * l); // , /*k1,*/ (i2 & 16711422) >> 1);
                    i2 = 4226832;
                }
                else
                {
//                    int j2 = SKC.getMouseX() - this.guiLeft - 60;
//                    int k2 = SKC.getMouseY() - this.guiTop - (14 + 19 * l);

//                    if (j2 >= 0 && k2 >= 0 && j2 < 108 && k2 < 19)
//                    {
//                        this.rect(i1, 14 + 19 * l, 108, 19, 0x20ff20);
//                        i2 = 16777088;
//                    }
//                    else
//                    {
                    this.rect(i1, 14 + 19 * l, 108, 19, 0x20ff20);
//                    }

                    this.rect(i1 + 1, 15 + 19 * l, 16, 16, 0x008000);
                    this.drawString(s, j1, 16 + 19 * l);
                    i2 = 8453920;
                }
                
                this.drawString(s1, j1 + 86 - this.getStringWidth(s1), 16 + 19 * l + 7);
            }
        }
    }

    /**
     * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
     */
    public void drawScreen(int mouseX, int mouseY)
    {
        super.drawScreen(mouseX, mouseY);
//        boolean flag = this.gm.thePlayer.creative;
//        int i = this.container.getLapisAmount();

        for (int j = 0; j < 3; ++j)
        {
            int k = this.container.mana[j];
            int l = this.container.ids[j];
            int i1 = j + 1;

            if (this.isPointInRegion(60, 14 + 19 * j, 108, 17, mouseX, mouseY) && k > 0 && l >= 0)
            {
                StringBuilder sb = new StringBuilder();

                if (l >= 0 && Enchantment.getEnchantmentById(l & 255) != null)
                {
                    String s = Enchantment.getEnchantmentById(l & 255).getFormattedName((l & 65280) >> 8);
                    sb.append(TextColor.WHITE + s + " . . . ?");
                }

//                if (!flag)
//                {
//                if (l >= 0 && sb.length() != 0)
//                {
//                	sb.append("\n");
//                }

                if (this.gm.player.getManaPoints() < k)
                {
                	sb.append((sb.length() != 0 ? "\n" : "") + TextColor.RED + String.format("%d Mana erforderlich", this.container.mana[j]));
                }
                else
                {
                    String s1 = "";

//                        if (i1 == 1)
//                        {
//                            s1 = I18n.format("container.enchant.lapis.one");
//                        }
//                        else
//                        {
//                            s1 = I18n.format("container.enchant.lapis.many", i1);
//                        }
//
//                        if (i >= i1)
//                        {
//                            list.add(ChatFormat.GRAY.toString() + "" + s1);
//                        }
//                        else
//                        {
//                            list.add(ChatFormat.RED.toString() + "" + s1);
//                        }

                    if (i1 == 1)
                    {
                        s1 = "1 Manapunkt";
                    }
                    else
                    {
                        s1 = String.format("%d Manapunkte", i1);
                    }

                    sb.append((sb.length() != 0 ? "\n" : "") + TextColor.LGRAY.toString() + "" + s1);
                }
//                }

                this.hover(sb.toString(), mouseX, mouseY);
                break;
            }
        }
    }
    
    private String getRandomName()
    {
        int i = this.nameRand.zrange(2) + 3;
        String s = "";

        for (int j = 0; j < i; ++j)
        {
            if (j > 0)
            {
                s = s + " ";
            }

            String name = NAMES[this.nameRand.zrange(NAMES.length)];
            for(int z = 0; z < name.length(); z++) {
            	s += (char)(name.charAt(z) - 0x61 + 0x80);
            }
        }

        return s;
    }

//    private void updateAnimation()
//    {
//        ItemStack itemstack = this.inventorySlots.getSlot(0).getStack();
//
//        if (!ItemStack.areItemStacksEqual(itemstack, this.field_147077_B))
//        {
//            this.field_147077_B = itemstack;
//
//            while (true)
//            {
//                this.field_147082_x += (float)(this.random.zrange(4) - this.random.zrange(4));
//
//                if (this.field_147071_v > this.field_147082_x + 1.0F || this.field_147071_v < this.field_147082_x - 1.0F)
//                {
//                    break;
//                }
//            }
//        }
//
//        ++this.field_147073_u;
//        this.field_147069_w = this.field_147071_v;
//        this.field_147076_A = this.field_147080_z;
//        boolean flag = false;
//
//        for (int i = 0; i < 3; ++i)
//        {
//            if (this.container.enchantLevels[i] != 0)
//            {
//                flag = true;
//            }
//        }
//
//        if (flag)
//        {
//            this.field_147080_z += 0.2F;
//        }
//        else
//        {
//            this.field_147080_z -= 0.2F;
//        }
//
//        this.field_147080_z = ExtMath.clampf(this.field_147080_z, 0.0F, 1.0F);
//        float f1 = (this.field_147082_x - this.field_147071_v) * 0.4F;
//        float f = 0.2F;
//        f1 = ExtMath.clampf(f1, -f, f);
//        this.field_147081_y += (f1 - this.field_147081_y) * 0.9F;
//        this.field_147071_v += this.field_147081_y;
//    }
}
