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.enchantment.Enchantment;
import common.init.Blocks;
import common.inventory.ContainerEnchantment;
import common.entity.npc.EntityNPC;
import common.rng.Random;
import common.util.Pair;
import common.util.Color;
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 Random nameRand = new Random();
	private final Random random = new Random();
	private final ContainerEnchantment enchantment;
	private final ActButton[] buttons = new ActButton[3];
	private final Label[] labels = new Label[3];
	private final Label[] mana = new Label[3];

	public GuiEnchant(EntityNPC inv, World world) {
		super(new ContainerEnchantment(inv, world), inv);
		this.enchantment = (ContainerEnchantment)this.inventorySlots;
	}

	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(Color.DARK_GRAY + name);
					this.mana[l].setText(Color.DARK_RED + "Mana-Level " + mana);
				}
				else {
					this.buttons[l].enabled = true;
					this.labels[l].setText(Color.GREEN + name);
					this.mana[l].setText(Color.DARK_GREEN + "Mana-Level " + mana + Color.GRAY + ", " + Color.YELLOW + "-" + (l + 1) + " Mana");
				}
			}
		}
	}

	public void addElements() {
		this.label(Blocks.enchanting_table.getDisplay(), 8, 16);
		this.label("Inventar", 8, 90 - 8);
		for(int l = 0; l < 3; ++l) {
			int i1 = 60;
			int j1 = i1 + 2;
			this.labels[l] = this.label("", j1, 26 + 19 * l);
			this.mana[l] = this.label("", j1, 26 + 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(Color.WHITE + s + " . . . ?");

				if(this.gm.player.getManaPoints() < k) {
					sb.append((sb.length() != 0 ? "\n" : "") + Color.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" : "") + Color.LIGHT_GRAY.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;
	}
}
