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.Utils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;

public class ChatManageMenu extends Menu {

    private EventCore main = EventCore.getInstance();

    private int delayAmount = 0;

    public ChatManageMenu(PlayerMenuUtil playerMenuUtil, int delayAmount) {
        super(playerMenuUtil);

        this.delayAmount = delayAmount;
    }

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

    @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&lChat Management"))) {
            switch (event.getCurrentItem().getType()) {
                case DIAMOND_SWORD:
                    this.main.getChatManager().toggleChat(player);

                    break;
                case SOUL_SAND:
                    switch (event.getClick()) {
                        case LEFT:
                            this.delayAmount++;

                            break;
                        case RIGHT:
                            if (this.delayAmount <= 0) {
                                return;
                            }

                            this.delayAmount--;

                            break;
                        case SHIFT_LEFT:
                        case SHIFT_RIGHT:
                            this.main.getChatManager().slowChat(player, this.delayAmount);

                            break;
                    }

                    break;
                case GLASS:
                    this.main.getChatManager().clearChat(player);

                    break;
            }

            new ChatManageMenu(this.playerMenuUtil, this.delayAmount).open(player);
        }
    }

    @Override
    public void setMenuItems(Player player) {
        ItemStackButton muteChatItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType("&b&lToggle Chat"),
                ColorUtils.getMessageType(
                        Arrays.asList(
                                "&fMuted: " + (this.main.getChatManager().isMuted() ? "&aYes" : "&cNo"),
                                "&fToggled By: &3" + (this.main.getChatManager().getToggledBy() == null ? "None" : this.main.getServer().getOfflinePlayer(this.main.getChatManager().getToggledBy()).getName()),
                                "&fLast Toggled: &3" + (this.main.getChatManager().getLastToggledTime() == 0 ? "Never" : Utils.getTimeAsAString(this.main.getChatManager().getLastToggledTime())),
                                "",
                                "&bClick to mute the chat"
                        )
                ),
                Material.DIAMOND_SWORD,
                0,
                1
        );

        ItemStackButton slowChatItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType("&b&lSlow Chat"),
                ColorUtils.getMessageType(
                        Arrays.asList(
                                "&fDelay Amount: &3" + this.main.getChatManager().getDelayAmount(),
                                "&fSlowed By: &3" + (this.main.getChatManager().getSlowedBy() == null ? "None" : this.main.getServer().getOfflinePlayer(this.main.getChatManager().getSlowedBy()).getName()),
                                "&fLast Slowed: &3" + (this.main.getChatManager().getLastSlowedTime() == 0 ? "Never" : Utils.getTimeAsAString(this.main.getChatManager().getLastSlowedTime())),
                                "",
                                "&bShift click to slow the chat for &3" + (this.delayAmount + " &3second" + (this.delayAmount > 1 ? "s" : "")),
                                "",
                                "&fLeft Click: &3+1",
                                "&fRight Click: &3-1"
                        )
                ),
                Material.SOUL_SAND,
                0,
                1
        );

        ItemStackButton clearChatItemStackButton = new ItemStackButton(
                ColorUtils.getMessageType("&b&lClear Chat"),
                ColorUtils.getMessageType(
                        Arrays.asList(
                                "&fCleared By: &3" + (this.main.getChatManager().getClearedBy() == null ? "None" : this.main.getServer().getOfflinePlayer(this.main.getChatManager().getClearedBy()).getName()),
                                "&fLast Cleared: &3" + (this.main.getChatManager().getLastClearedTime() == 0 ? "Never" : Utils.getTimeAsAString(this.main.getChatManager().getLastClearedTime())),
                                "",
                                "&bClick to clear the chat"
                        )
                ),
                Material.GLASS,
                0,
                1
        );

        ItemStack muteChatItemStack = muteChatItemStackButton.makeItemStack();
        ItemStack slowChatItemStack = slowChatItemStackButton.makeItemStack();
        ItemStack clearChatItemStack = clearChatItemStackButton.makeItemStack();

        this.setFillerGlass();
        this.inventory.setItem(11, muteChatItemStack);
        this.inventory.setItem(13, slowChatItemStack);
        this.inventory.setItem(15, clearChatItemStack);
    }
}
