package rip.tilly.bedwars.menus.arena.buttons;

import lombok.AllArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import rip.tilly.bedwars.BedWars;
import rip.tilly.bedwars.game.arena.Arena;
import rip.tilly.bedwars.menus.arena.ArenaCopyMenu;
import rip.tilly.bedwars.menus.arena.ArenaGenerationMenu;
import rip.tilly.bedwars.utils.CC;
import rip.tilly.bedwars.utils.ItemBuilder;
import rip.tilly.bedwars.utils.menu.Button;

import java.util.Arrays;

@AllArgsConstructor
public class ArenaButton extends Button {

	private final Arena arena;

	@Override
	public ItemStack getButtonItem(Player player) {
		return new ItemBuilder(Material.valueOf(arena.getIcon()))
				.durability(arena.getIconData())
				.name("&b" + arena.getName())
				.lore(CC.translate(
						Arrays.asList(
								" ",
								"&bArena Information&7:",
								" &9&l▸ &fState: " + (arena.isEnabled() ? "&aEnabled" : "&cDisabled"),
								" &9&l▸ &fType: &b" + (arena.getAvailableArenas().size() == 0 ? "Shared" : "Standalone"),
								" &9&l▸ &fCopies: &b" + (arena.getCopiedArenas().size() == 0 ? "Not Required!" : arena.getCopiedArenas().size()),
								" &9&l▸ &fAvailable: &b" + (arena.getAvailableArenas().size() == 0 ? +1 : arena.getAvailableArenas().size()),
								" ",
								(arena.getCopiedArenas().size() == 0 ? "&4&l&mMIDDLE-CLICK &4&mto see arena copies" : "&6&lMIDDLE-CLICK &6to see arena copies"),
								"&a&lLEFT-CLICK &ato teleport to arena",
								"&b&lRIGHT CLICK &bto generate standalone arenas")
						)
				)
				.hideFlags()
				.build();
	}

	@Override
	public void clicked(Player player, int slot, ClickType clickType, int hotbarButton) {
		playSuccess(player);
		switch (clickType) {
			case LEFT:
				player.teleport(arena.getA().toBukkitLocation());
				break;
			case RIGHT:
				Bukkit.getScheduler().runTaskLaterAsynchronously(BedWars.getInstance(), () -> new ArenaGenerationMenu(arena).openMenu(player), 1L);
				break;
			case MIDDLE:
				if (arena.getCopiedArenas().size() >= 1) {
					Bukkit.getScheduler().runTaskLaterAsynchronously(BedWars.getInstance(), () -> new ArenaCopyMenu(arena).openMenu(player), 1L);
				}
				break;
		}

		player.closeInventory();
	}
}
