package de.simolzimol.mclogger.paper.listeners;

import de.simolzimol.mclogger.paper.PaperLoggerPlugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.weather.ThunderChangeEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.event.world.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Logs world events: weather, thunder, portal creation,
 * tree/mushroom growth, chunk load, etc.
 *
 * @author SimolZimol
 */
public class WorldListener implements Listener {

    private final PaperLoggerPlugin plugin;

    public WorldListener(PaperLoggerPlugin plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onWeather(WeatherChangeEvent event) {
        Map<String, Object> details = new HashMap<>();
        details.put("to_storm", event.toWeatherState());
        plugin.getDb().insertWorldEvent(
                "weather_change",
                event.getWorld().getName(),
                null, null, null,
                details
        );
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onThunder(ThunderChangeEvent event) {
        Map<String, Object> details = new HashMap<>();
        details.put("to_thunder", event.toThunderState());
        plugin.getDb().insertWorldEvent(
                "thunder_change",
                event.getWorld().getName(),
                null, null, null,
                details
        );
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPortal(PortalCreateEvent event) {
        Map<String, Object> details = new HashMap<>();
        details.put("reason", event.getReason().name());
        details.put("blocks", event.getBlocks().size());
        if (event.getEntity() != null) {
            details.put("entity", event.getEntity().getType().name());
        }
        plugin.getDb().insertWorldEvent(
                "portal_create",
                event.getWorld().getName(),
                event.getBlocks().isEmpty() ? null : (double) event.getBlocks().get(0).getLocation().getBlockX(),
                event.getBlocks().isEmpty() ? null : (double) event.getBlocks().get(0).getLocation().getBlockY(),
                event.getBlocks().isEmpty() ? null : (double) event.getBlocks().get(0).getLocation().getBlockZ(),
                details
        );
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onStructureGrow(StructureGrowEvent event) {
        Map<String, Object> details = new HashMap<>();
        details.put("species", event.getSpecies().name());
        details.put("natural", event.isFromBonemeal());
        details.put("player", event.getPlayer() != null ? event.getPlayer().getName() : null);
        plugin.getDb().insertWorldEvent(
                "structure_grow",
                event.getWorld().getName(),
                (double) event.getLocation().getBlockX(),
                (double) event.getLocation().getBlockY(),
                (double) event.getLocation().getBlockZ(),
                details
        );
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onWorldLoad(WorldLoadEvent event) {
        Map<String, Object> details = new HashMap<>();
        details.put("environment", event.getWorld().getEnvironment().name());
        details.put("seed", event.getWorld().getSeed());
        plugin.getDb().insertWorldEvent("world_load", event.getWorld().getName(), null, null, null, details);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onWorldUnload(WorldUnloadEvent event) {
        plugin.getDb().insertWorldEvent("world_unload", event.getWorld().getName(), null, null, null, null);
    }
}
