package com.loganmagnan.eventcore.menusystem.menus;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.menusystem.PaginatedMenu;
import com.loganmagnan.eventcore.menusystem.PlayerMenuUtil;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.PlayerHeadUtils;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;

public class StaffOnlineMenu extends PaginatedMenu {

    private EventCore main = EventCore.getInstance();

    private List<Player> players = new ArrayList<Player>();

    public StaffOnlineMenu(PlayerMenuUtil playerMenuUtil) {
        super(playerMenuUtil);

        for (Player playerOnline : this.main.getServer().getOnlinePlayers()) {
            if (!playerOnline.hasPermission("eventcore.command.staffmode")) {
                continue;
            }

            this.players.add(playerOnline);
        }
    }

    @Override
    public String getMenuName() {
        return ColorUtils.getMessageType("&b&lStaff Online");
    }

    @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&lStaff Online"))) {
            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.players.size())) {
                        this.page++;
                        this.open(player);
                    } else {
                        player.sendMessage(ColorUtils.getMessageType("&aYou're on the last page"));
                    }

                    break;
            }

            Player target = this.main.getServer().getPlayer(ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName()));

            if (target == null) {
                return;
            }

            player.closeInventory();
            player.teleport(target);
            player.sendMessage(ColorUtils.getMessageType("&aTeleported to &a&l" + target.getName()));
        }
    }

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

        if (this.players.isEmpty()) {
            return;
        }

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

            if (this.index >= this.players.size()) {
                break;
            }

            if (this.players.get(this.index) == null) {
                continue;
            }

            Player playerOnline = this.players.get(this.index);

            if (playerOnline == null) {
                continue;
            }

            List<String> lore = new ArrayList<String>();
            lore.add(ColorUtils.getMessageType("&fStaff Mode: " + (this.main.getStaffManager().isPlayerInStaffMode(playerOnline) ? "&aEnabled" : "&cDisabled")));
            lore.add(ColorUtils.getMessageType("&fVanish: " + (this.main.getStaffManager().isPlayerVanished(playerOnline) ? "&aEnabled" : "&cDisabled")));
            lore.add(ColorUtils.getMessageType(""));
            lore.add(ColorUtils.getMessageType("&bClick to teleport"));

            PlayerHeadUtils playerHeadUtils = new PlayerHeadUtils();
            playerHeadUtils.name(playerOnline.getDisplayName());
            playerHeadUtils.lore(lore);
            playerHeadUtils.owner(playerOnline.getName());

            ItemStack itemStack = playerHeadUtils.makeItemStack();

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