package client.gui.container;

import client.gui.element.ActButton;
import client.gui.element.ButtonCallback;
import client.gui.element.Label;
import client.gui.element.PressType;
import common.color.TextColor;
import common.enchantment.Enchantment;
import common.inventory.ContainerEnchantment;
import common.inventory.InventoryPlayer;
import common.rng.Random;
import common.util.Pair;
import common.world.World;

public class GuiEnchant extends GuiContainer implements ButtonCallback {
	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(" ");

	private final InventoryPlayer playerInv;
	private final Random nameRand = new Random();
	private final Random random = new Random();
	private final ContainerEnchantment enchantment;
	private final String title;
	private final ActButton[] buttons = new ActButton[3];
	private final Label[] labels = new Label[3];
	private final Label[] mana = new Label[3];

	public GuiEnchant(InventoryPlayer inv, World world, String title) {
		super(new ContainerEnchantment(inv, world));
		this.playerInv = inv;
		this.enchantment = (ContainerEnchantment)this.inventorySlots;
		this.title = title;
	}

	public void updateScreen() {
		super.updateScreen();
		this.nameRand.setSeed((long)this.enchantment.seed);

		for(int l = 0; l < 3; ++l) {
			String name = this.getRandomName();
			int mana = this.enchantment.mana[l];

			if(mana == 0) {
				this.buttons[l].enabled = false;
				this.labels[l].setText("");
				this.mana[l].setText("");
			}
			else {
				if(this.gm.player.getManaPoints() < mana) {
					this.buttons[l].enabled = false;
					this.labels[l].setText(TextColor.DGRAY + name);
					this.mana[l].setText(TextColor.DRED + "Mana-Level " + mana);
				}
				else {
					this.buttons[l].enabled = true;
					this.labels[l].setText(TextColor.GREEN + name);
					this.mana[l].setText(TextColor.DGREEN + "Mana-Level " + mana + TextColor.GRAY + ", " + TextColor.YELLOW + "-" + (l + 1) + " Mana");
				}
			}
		}
	}

	public void addElements() {
		this.label(this.title, 12, 5);
		this.label(this.playerInv.getCommandName(), 8, this.ySize - 96 + 2);
		for(int l = 0; l < 3; ++l) {
			int i1 = 60;
			int j1 = i1 + 2;
			this.labels[l] = this.label("", j1, 16 + 19 * l);
			this.mana[l] = this.label("", j1, 16 + 19 * l + 7);
		}
	}

	public void use(ActButton btn, PressType type) {
		for(int k = 0; k < 3; ++k) {
			if(btn == this.buttons[k] && this.enchantment.enchantItem(this.gm.player, k))
				this.gm.controller.sendEnchantPacket(this.enchantment.windowId, k);
		}
	}

	public void addButtons() {
		super.addButtons();
		for(int l = 0; l < 3; ++l) {
			this.buttons[l] = this.button(60, 14 + 19 * l, 108, 19, this, "");
		}
	}

	public void drawScreen(int mouseX, int mouseY) {
		super.drawScreen(mouseX, mouseY);

		for(int j = 0; j < 3; ++j) {
			int k = this.enchantment.mana[j];
			Pair<Enchantment, Integer> l = this.enchantment.ench[j];
			int i1 = j + 1;

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

				String s = l.first().getFormattedName(l.second());
				sb.append(TextColor.WHITE + s + " . . . ?");

				if(this.gm.player.getManaPoints() < k) {
					sb.append((sb.length() != 0 ? "\n" : "") + TextColor.RED + String.format("%d Mana erforderlich", this.enchantment.mana[j]));
				}
				else {
					String 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;
	}
}
