package com.loganmagnan.eventcore.menusystem.menus;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.menusystem.ItemStackButton;
import com.loganmagnan.eventcore.menusystem.PaginatedMenu;
import com.loganmagnan.eventcore.menusystem.PlayerMenuUtil;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.CustomLocation;
import com.loganmagnan.eventcore.utils.PlayerHeadUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TeamSpawnPointsMenu extends PaginatedMenu {

    private EventCore main = EventCore.getInstance();

    public TeamSpawnPointsMenu(PlayerMenuUtil playerMenuUtil) {
        super(playerMenuUtil);
    }

    @Override
    public String getMenuName() {
        return ColorUtils.getMessageType("&b&lTeam Spawn Points");
    }

    @Override
    public int getSlots() {
        return 27;
    }

    @Override
    public void handleMenu(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();

        if (event.getCurrentItem() == null) {
            return;
        }

        if (event.getView().getTitle().equalsIgnoreCase(ColorUtils.getMessageType("&b&lTeam Spawn Points"))) {
            ItemStack itemStack = event.getCurrentItem();

            switch (ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName())) {
                case "Previous Page":
                    if (this.page == 0) {
                        player.sendMessage(ColorUtils.getMessageType("&aYou're on the first page"));
                    } else {
                        this.page--;
                        this.open(player);
                    }

                    break;
                case "Close":
                    player.closeInventory();

                    break;
                case "Next Page":
                    if (!((this.index++) >= this.main.getEventManager().getEvent().getTeamSpawnPoints().size())) {
                        this.page++;
                        this.open(player);
                    } else {
                        player.sendMessage(ColorUtils.getMessageType("&aYou're on the last page"));
                    }

                    break;
            }

            CustomLocation teamSpawnPoint = this.main.getEventManager().getEvent().getTeamSpawnPoints().get(Integer.parseInt(ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).substring(ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).length() - 1, ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).length())) - 1);

            switch (event.getClick()) {
                case LEFT:
                    this.main.getEventManager().getEvent().getTeamSpawnPoints().remove(teamSpawnPoint);

                    break;
                case RIGHT:
                    this.main.getEventManager().getEvent().getTeamSpawnPoints().remove(teamSpawnPoint);
                    this.main.getEventManager().getEvent().getTeamSpawnPoints().add(Integer.parseInt(ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).substring(ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).length() - 1, ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()).length())) - 1, CustomLocation.fromBukkitLocation(player.getLocation()));

                    break;
            }

            new TeamSpawnPointsMenu(this.playerMenuUtil).open(player);
        }
    }

    @Override
    public void setMenuItems(Player player) {
        this.addMenuBorder();

        int number = 1;

        if (this.main.getEventManager().getEvent().getTeamSpawnPoints().isEmpty()) {
            return;
        }

        for (int i = 0; i < this.getMaxItemsPerPage(); i++) {
            this.index = this.getMaxItemsPerPage() * this.page + i;

            if (this.index >= this.main.getEventManager().getEvent().getTeamSpawnPoints().size()) {
                break;
            }

            if (this.main.getEventManager().getEvent().getTeamSpawnPoints().get(this.index) == null) {
                continue;
            }

            CustomLocation teamSpawnPoint = this.main.getEventManager().getEvent().getTeamSpawnPoints().get(this.index);

            if (teamSpawnPoint == null) {
                continue;
            }

            ItemStackButton teamSpawnPointItemStackButton = new ItemStackButton(
                    ColorUtils.getMessageType("&b&lTeam Spawn Point " + number),
                    ColorUtils.getMessageType(
                            Arrays.asList(
                                    "&7» &3" + (Math.floor(teamSpawnPoint.toBukkitLocation().getX()) + "&7, &3" + Math.floor(teamSpawnPoint.toBukkitLocation().getY()) + "&7, &3" + Math.floor(teamSpawnPoint.toBukkitLocation().getZ()) + " &7(&f" + teamSpawnPoint.toBukkitLocation().getWorld().getName() + "&7)"),
                                    "",
                                    "&fLeft Click: &3Remove",
                                    "&fRight Click: &3Change"
                            )
                    ),
                    Material.ENDER_EYE,
                    0,
                    1
            );

            number++;

            ItemStack teamSpawnPointItemStack = teamSpawnPointItemStackButton.makeItemStack();

            this.inventory.addItem(teamSpawnPointItemStack);
        }
    }
}
