package nl.sbdeveloper.themeparkplus.listeners;

import club.minnced.discord.webhook.send.WebhookEmbed;
import club.minnced.discord.webhook.send.WebhookEmbedBuilder;
import club.minnced.discord.webhook.send.WebhookMessage;
import club.minnced.discord.webhook.send.WebhookMessageBuilder;
import nl.iobyte.themepark.api.attraction.enums.Status;
import nl.iobyte.themepark.api.attraction.objects.Attraction;
import nl.iobyte.themepark.api.event.attraction.AttractionPreStatusChangeEvent;
import nl.iobyte.themepark.api.event.attraction.AttractionStatusChangeEvent;
import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
import nl.sbdeveloper.themeparkplus.api.PlusAPI;
import nl.sbdeveloper.themeparkplus.api.objects.MalfunctionReport;
import nl.sbdeveloper.themeparkplus.util.ConfigUtil;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

/**
 * Status change listener for malfunction menu and discord webhook
 */
public class StatusChangeListener implements Listener {
    @EventHandler
    public void onStatusChange(AttractionPreStatusChangeEvent e) {
        Attraction att = e.getAttraction();

        if (!PlusAPI.isReported(att.getID()) && e.getCurrent() == Status.MALFUNCTION && e.getPlayer() != null) {
            if (ThemeParkPlus.getSConfig().getFile().getBoolean("Malfunction.AllowCommandReport")) {
                MalfunctionReport malfunctionReport = new MalfunctionReport(att.getID(), e.getPlayer().getUniqueId());
                PlusAPI.addReport(malfunctionReport);
            } else {
                e.setCancelled(true);
                e.getPlayer().sendMessage(ConfigUtil.getMessage("Malfunction.NoCommand"));
            }
        }

        if (PlusAPI.isReported(att.getID()) && e.getCurrent() != Status.MALFUNCTION && e.getPlayer() != null) {
            if (ThemeParkPlus.getSConfig().getFile().getBoolean("Malfunction.AllowCommandFix")) {
                PlusAPI.removeReport(att.getID());
            } else {
                e.setCancelled(true);
                e.getPlayer().sendMessage(ConfigUtil.getMessage("Malfunction.NoCommand"));
            }
        }
    }

    @EventHandler
    public void onStatusChangeTP(AttractionStatusChangeEvent e) {
        String title = ThemeParkPlus.getSConfig().getFile().getString("DiscordWebhook.Embed.Title");
        if (title == null) return;

        String rideName = ChatColor.stripColor(ConfigUtil.__(e.getAttraction().getName()));
        String regionName = ChatColor.stripColor(ConfigUtil.__(e.getAttraction().getRegionID()));
        String statusAfter = ChatColor.stripColor(ConfigUtil.__(e.getCurrent().getColor() + e.getCurrent().getName()));

        title = title.replaceAll("%RideName%", rideName);
        title = title.replaceAll("%Region%", regionName);
        String copy = ThemeParkPlus.getSConfig().getFile().getString("DiscordWebhook.Embed.Copyright");
        String copyimg = ThemeParkPlus.getSConfig().getFile().getString("DiscordWebhook.Embed.CopyrightImage");
        Integer color = ThemeParkPlus.getSConfig().getFile().getInt("DiscordWebhook.Embed.Colors." + e.getCurrent().name());
        if (copy == null || copyimg == null) return;

        WebhookEmbed embed = new WebhookEmbedBuilder()
                .setTitle(new WebhookEmbed.EmbedTitle(title, null))
                .setFooter(new WebhookEmbed.EmbedFooter(copy, copyimg))
                .setColor(color)
                .addField(new WebhookEmbed.EmbedField(false, rideName, statusAfter))
                .build();

        WebhookMessage message = new WebhookMessageBuilder()
                .addEmbeds(embed)
                .build();

        ThemeParkPlus.getWebhookClient().send(message);
    }

    @EventHandler
    public void onStatusChangePPMC(nl.iobyte.parkplus.generic.event.attraction.AttractionStatusChangeEvent e) {
        String title = ThemeParkPlus.getSConfig().getFile().getString("DiscordWebhook.Embed.Title");
        if (title == null) return;

        String rideName = ChatColor.stripColor(ConfigUtil.__(e.getAttraction().getName()));
        String regionName = ChatColor.stripColor(ConfigUtil.__(e.getAttraction().getRegionID()));
        String statusAfter = ChatColor.stripColor(ConfigUtil.__(e.getCurrent().getColor() + e.getCurrent().getName()));

        title = title.replaceAll("%RideName%", rideName);
        title = title.replaceAll("%Region%", regionName);
        String copy = ThemeParkPlus.getSConfig().getFile().getString("DiscordWebhook.Embed.Copyright");
        String copyimg = ThemeParkPlus.getSConfig().getFile().getString("DiscordWebhook.Embed.CopyrightImage");
        Integer color = ThemeParkPlus.getSConfig().getFile().getInt("DiscordWebhook.Embed.Colors." + e.getCurrent().name());
        if (copy == null || copyimg == null) return;

        WebhookEmbed embed = new WebhookEmbedBuilder()
                .setTitle(new WebhookEmbed.EmbedTitle(title, null))
                .setFooter(new WebhookEmbed.EmbedFooter(copy, copyimg))
                .setColor(color)
                .addField(new WebhookEmbed.EmbedField(false, rideName, statusAfter))
                .build();

        WebhookMessage message = new WebhookMessageBuilder()
                .addEmbeds(embed)
                .build();

        ThemeParkPlus.getWebhookClient().send(message);
    }
}
