/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.lobby.special.easter;

import de.steamwar.lobby.LobbySystem;
import de.steamwar.sql.UserConfig;
import org.bukkit.Bukkit;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.util.Vector;

public class EggClickListener implements Listener {

    public EggClickListener() {
        Bukkit.getPluginManager().registerEvents(this, LobbySystem.getInstance());
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }
        Block block = event.getClickedBlock();
        if (block == null) {
            return;
        }
        Vector vector = block.getLocation().toVector();
        Egg egg = null;
        int index = -1;
        for (Egg egg1 : EggHunt.getEggList()) {
            index++;
            if (egg1.getVector().equals(vector)) {
                egg = egg1;
                break;
            }
        }
        if (egg == null) {
            return;
        }
        Player player = event.getPlayer();
        String found = UserConfig.getConfig(player.getUniqueId(), EggHunt.EGG_HUNT_CONFIG_KEY);
        if (found == null) {
            found = "";
        }
        StringBuilder builder = new StringBuilder(found);
        while (builder.length() <= index) {
            builder.append("0");
        }
        if (builder.charAt(index) == '1') {
            return;
        }

        try {
            player.sendTitle(LobbySystem.getMessage().parse(egg.getDifficulty().getMessage(), player), LobbySystem.getMessage().parse(egg.getMessage(), player), 0, 40, 5);
        } catch (Exception e) {
            player.sendTitle(LobbySystem.getMessage().parse(egg.getDifficulty().getMessage(), player), egg.getMessage(), 0, 40, 5);
        }

        player.spawnParticle(Particle.END_ROD, block.getLocation().add(0.5, 0.5, 0.5), 10, 0.5, 0.5, 0.5, 0.1);
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.25f, 1.0f);

        builder.setCharAt(index, '1');
        UserConfig.updatePlayerConfig(player.getUniqueId(), EggHunt.EGG_HUNT_CONFIG_KEY, builder.toString());
    }
}
