package com.loganmagnan.eventcore.menusystem.menus;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.menusystem.ItemStackButton;
import com.loganmagnan.eventcore.menusystem.Menu;
import com.loganmagnan.eventcore.menusystem.PlayerMenuUtil;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.loganmagnan.eventcore.utils.CustomLocation;
import com.loganmagnan.eventcore.utils.TimeUtils;
import com.loganmagnan.eventcore.utils.Utils;
import net.wesjd.anvilgui.AnvilGUI;
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 EventManageMenu extends Menu {

    private EventCore main = EventCore.getInstance();

    // To Do: Fix The Anvil Menu

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

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

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

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

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

        if (event.getView().getTitle().equalsIgnoreCase(ColorUtils.getMessageType("&b&lEvent Management"))) {
            switch (event.getCurrentItem().getType()) {
                case NAME_TAG:
                    new AnvilGUI.Builder().onComplete((anvilGUIPlayer, string) -> {
                        this.main.getEventManager().getEvent().setName(string);

                        return AnvilGUI.Response.close();
                    }).onClose(anvilGUIPlayer -> {

                    }).title(ColorUtils.getMessageType("&b&lEvent Management")).text(ColorUtils.getMessageType("Enter it here")).plugin(this.main).open(player);

                    break;
                case COMPASS:
                    switch (event.getClick()) {
                        case LEFT:
                            player.teleport(this.main.getEventManager().getEvent().getSpawnPoint().toBukkitLocation());

                            break;
                        case RIGHT:
                            this.main.getEventManager().getEvent().setSpawnPoint(CustomLocation.fromBukkitLocation(player.getLocation()));

                            break;
                    }

                    break;
                case CLOCK:
                    new AnvilGUI.Builder().onComplete((anvilGUIPlayer, string) -> {
                        if (!Utils.isNumeric(string)) {
                            new EventManageMenu(this.playerMenuUtil).open(player);

                            return AnvilGUI.Response.close();
                        }

                        int duration = Integer.parseInt(string);

                        this.main.getEventManager().getEvent().setDuration(duration);

                        return AnvilGUI.Response.close();
                    }).onClose(anvilGUIPlayer -> {

                    }).title(ColorUtils.getMessageType("&b&lEvent Management")).text(ColorUtils.getMessageType("Enter it here")).plugin(this.main).open(player);

                    break;
                case BOOK:
                    new AnvilGUI.Builder().onComplete((anvilGUIPlayer, string) -> {
                        if (!Utils.isNumeric(string)) {
                            new EventManageMenu(this.playerMenuUtil).open(player);

                            return AnvilGUI.Response.close();
                        }

                        int amountOfTeams = Integer.parseInt(string);

                        this.main.getEventManager().getEvent().setAmountOfTeams(amountOfTeams);

                        return AnvilGUI.Response.close();
                    }).onClose(anvilGUIPlayer -> {

                    }).title(ColorUtils.getMessageType("&b&lEvent Management")).text(ColorUtils.getMessageType("Enter it here")).plugin(this.main).open(player);

                    break;
                case PAPER:
                    new AnvilGUI.Builder().onComplete((anvilGUIPlayer, string) -> {
                        if (!Utils.isNumeric(string)) {
                            new EventManageMenu(this.playerMenuUtil).open(player);

                            return AnvilGUI.Response.close();
                        }

                        int amountPerTeam = Integer.parseInt(string);

                        this.main.getEventManager().getEvent().setAmountPerTeam(amountPerTeam);

                        return AnvilGUI.Response.close();
                    }).onClose(anvilGUIPlayer -> {

                    }).title(ColorUtils.getMessageType("&b&lEvent Management")).text(ColorUtils.getMessageType("Enter it here")).plugin(this.main).open(player);

                    break;
                case ENDER_EYE:
                    switch (event.getClick()) {
                        case LEFT:
                            if (this.main.getEventManager().getEvent().getAmountOfTeams() == this.main.getEventManager().getEvent().getTeamSpawnPoints().size()) {
                                return;
                            }

                            this.main.getEventManager().getEvent().getTeamSpawnPoints().add(CustomLocation.fromBukkitLocation(player.getLocation()));

                            break;
                        case RIGHT:
                            new TeamSpawnPointsMenu(this.playerMenuUtil).open(player);

                            return;
                    }

                    break;
                case PAINTING:
                    this.main.getEventManager().getEvent().setScoreboardEnabled(!this.main.getEventManager().getEvent().isScoreboardEnabled());

                    break;
                case ANVIL:
                    this.main.getEventManager().getEvent().setTimerEnabled(!this.main.getEventManager().getEvent().isTimerEnabled());

                    break;
                case DIAMOND:
                    switch (event.getClick()) {
                        case LEFT:
                            this.main.getEventManager().getEvent().setEventsEnabled(!this.main.getEventManager().getEvent().isEventsEnabled());

                            break;
                        case RIGHT:
                            // To Do: Setup Events Menu

                            return;
                    }

                    break;
                case LIME_WOOL:
                    // To Do: Event Start System

                    break;
                case RED_WOOL:
                    // To Do: Event Stop System

                    break;
                case NETHER_STAR:
                    // To Do: Setup Started Mode
                    // * Show Duration
                    // * Show Each Team's Information

                    break;
            }

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

    @Override
    public void setMenuItems(Player player) {
        List<String> teamSpawnPoints = new ArrayList<String>();
        List<String> teamSpawnPointsLore = new ArrayList<String>();

        Material material = Material.RED_STAINED_GLASS_PANE;

        for (CustomLocation teamSpawnPoint : this.main.getEventManager().getEvent().getTeamSpawnPoints()) {
            teamSpawnPoints.add("&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)");
        }

        if (teamSpawnPoints.size() == 0) {
            teamSpawnPointsLore.add("&7» &3None");
        } else {
            for (String teamSpawnPoint : teamSpawnPoints) {
                teamSpawnPointsLore.add("&7» " + teamSpawnPoint);
            }
        }

        teamSpawnPointsLore.add("");
        teamSpawnPointsLore.add("&fLeft Click: &3Add");
        teamSpawnPointsLore.add("&fRight Click: &3Change");

        ItemStackButton nameItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lName"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» &3" + this.main.getEventManager().getEvent().getName(),
                                "",
                                "&bClick to change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.NAME_TAG,
                0,
                1
        );

        ItemStackButton spawnPointItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lSpawn Point"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» &3" + Math.floor(this.main.getEventManager().getEvent().getSpawnPoint().toBukkitLocation().getX()) + "&7, &3" + Math.floor(this.main.getEventManager().getEvent().getSpawnPoint().toBukkitLocation().getY()) + "&7, &3" + Math.floor(this.main.getEventManager().getEvent().getSpawnPoint().toBukkitLocation().getZ()) + " &7(&f" + this.main.getEventManager().getEvent().getSpawnPoint().toBukkitLocation().getWorld().getName() + "&7)",
                                "",
                                "&fLeft Click: &3Teleport",
                                "&fRight Click: &3Change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.COMPASS,
                0,
                1
        );

        ItemStackButton durationItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lDuration"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» &3" + TimeUtils.formatIntoMMSS(this.main.getEventManager().getEvent().getDuration()),
                                "",
                                "&bClick to change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.CLOCK,
                0,
                1
        );

        ItemStackButton amountOfTeamsItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lAmount of Teams"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» &3" + this.main.getEventManager().getEvent().getAmountOfTeams(),
                                "",
                                "&bClick to change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.BOOK,
                0,
                1
        );

        ItemStackButton amountPerTeamItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lAmount Per Team"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» &3" + this.main.getEventManager().getEvent().getAmountPerTeam(),
                                "",
                                "&bClick to change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.PAPER,
                0,
                1
        );

        ItemStackButton teamSpawnPointsItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lTeam Spawn Points"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        teamSpawnPointsLore
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.ENDER_EYE,
                0,
                1
        );

        ItemStackButton scoreboardEnabledItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lScoreboard Enabled"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» " + (this.main.getEventManager().getEvent().isScoreboardEnabled() ? "&aYes" : "&cNo"),
                                "",
                                "&bClick to change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.PAINTING,
                0,
                1
        );

        ItemStackButton timerEnabledItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lTimer Enabled"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» " + (this.main.getEventManager().getEvent().isTimerEnabled() ? "&aYes" : "&cNo"),
                                "",
                                "&bClick to change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.ANVIL,
                0,
                1
        );

        ItemStackButton eventsEnabledItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lEvents Enabled"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&7» " + (this.main.getEventManager().getEvent().isEventsEnabled() ? "&aYes" : "&cNo"),
                                "",
                                "&b&lEvents",
                                "&7» " + (this.main.getEventManager().getEvent().isBlockPlaceEventEnabled() ? "&aBlockPlaceEvent" : "&cBlockPlaceEvent"),
                                "&7» " + (this.main.getEventManager().getEvent().isBlockBreakEventEnabled() ? "&aBlockBreakEvent" : "&cBlockBreakEvent"),
                                "",
                                "&fLeft Click: " + (this.main.getEventManager().getEvent().isEventsEnabled() ? "&cDisable" : "&aEnable"),
                                "&fRight Click: &3Change"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.DIAMOND,
                0,
                1
        );

        ItemStackButton startItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&c&lNo Permission" : "&b&lStart"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? new ArrayList<String>() :
                        Arrays.asList(
                                "&bClick to start"
                        )
                ),
                this.main.getEventManager().getEvent().isStarted() ? material : Material.LIME_WOOL,
                0,
                1
        );

        ItemStackButton stopItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ? "&b&lStop" : "&c&lNo Permission"),
                ColorUtils.getMessageType(this.main.getEventManager().getEvent().isStarted() ?
                        Arrays.asList(
                                "&bClick to stop"
                        ) : new ArrayList<String>()
                ),
                this.main.getEventManager().getEvent().isStarted() ? Material.RED_WOOL : material,
                0,
                1
        );

        ItemStackButton startedItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType("&b&lStarted"),
                ColorUtils.getMessageType(
                        Arrays.asList(
                                "&7» " + (this.main.getEventManager().getEvent().isStarted() ? "&aYes" : "&cNo"),
                                "",
                                "&bClick for more information"
                        )
                ),
                Material.NETHER_STAR,
                0,
                1
        );

        ItemStack nameItemStack = nameItemStackButton.makeItemStack();
        ItemStack spawnPointItemStack = spawnPointItemStackButton.makeItemStack();
        ItemStack durationItemStack = durationItemStackButton.makeItemStack();
        ItemStack amountOfTeamsItemStack = amountOfTeamsItemStackButton.makeItemStack();
        ItemStack amountPerTeamItemStack = amountPerTeamItemStackButton.makeItemStack();
        ItemStack teamSpawnPointsItemStack = teamSpawnPointsItemStackButton.makeItemStack();
        ItemStack scoreboardEnabledItemStack = scoreboardEnabledItemStackButton.makeItemStack();
        ItemStack timerEnabledItemStack = timerEnabledItemStackButton.makeItemStack();
        ItemStack eventsEnabledItemStack = eventsEnabledItemStackButton.makeItemStack();
        ItemStack startItemStack = startItemStackButton.makeItemStack();
        ItemStack stopItemStack = stopItemStackButton.makeItemStack();
        ItemStack startedItemStack = startedItemStackButton.makeItemStack();

        this.setFillerGlass();
        this.inventory.setItem(10, new ItemStackButton(ColorUtils.getMessageType("&b&lGeneral Information"), new ArrayList<String>(), Material.OAK_SIGN, 0, 1).makeItemStack());
        this.inventory.setItem(11, new ItemStackButton(ColorUtils.getMessageType(""), new ArrayList<String>(), Material.WHITE_STAINED_GLASS_PANE, 0, 1).makeItemStack());
        this.inventory.setItem(12, nameItemStack);
        this.inventory.setItem(13, spawnPointItemStack);
        this.inventory.setItem(14, durationItemStack);
        this.inventory.setItem(19, new ItemStackButton(ColorUtils.getMessageType("&b&lTeams Information"), new ArrayList<String>(), Material.OAK_SIGN, 0, 1).makeItemStack());
        this.inventory.setItem(20, new ItemStackButton(ColorUtils.getMessageType(""), new ArrayList<String>(), Material.WHITE_STAINED_GLASS_PANE, 0, 1).makeItemStack());
        this.inventory.setItem(21, amountOfTeamsItemStack);
        this.inventory.setItem(22, amountPerTeamItemStack);
        this.inventory.setItem(23, teamSpawnPointsItemStack);
        this.inventory.setItem(28, new ItemStackButton(ColorUtils.getMessageType("&b&lToggleable Settings"), new ArrayList<String>(), Material.OAK_SIGN, 0, 1).makeItemStack());
        this.inventory.setItem(29, new ItemStackButton(ColorUtils.getMessageType(""), new ArrayList<String>(), Material.WHITE_STAINED_GLASS_PANE, 0, 1).makeItemStack());
        this.inventory.setItem(30, scoreboardEnabledItemStack);
        this.inventory.setItem(31, timerEnabledItemStack);
        this.inventory.setItem(32, eventsEnabledItemStack);
        this.inventory.setItem(37, new ItemStackButton(ColorUtils.getMessageType("&b&lManagement Settings"), new ArrayList<String>(), Material.OAK_SIGN, 0, 1).makeItemStack());
        this.inventory.setItem(38, new ItemStackButton(ColorUtils.getMessageType(""), new ArrayList<String>(), Material.WHITE_STAINED_GLASS_PANE, 0, 1).makeItemStack());
        this.inventory.setItem(39, startItemStack);
        this.inventory.setItem(40, stopItemStack);
        this.inventory.setItem(41, startedItemStack);
    }
}
