/*
 *   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.bausystem.world;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.FlatteningWrapper;
import de.steamwar.bausystem.Permission;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public class DetonatorListener implements Listener {

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getItem() == null) return;
        if (event.getItem().isSimilar(Detonator.WAND)) {
            Player player = event.getPlayer();
            if (Welt.noPermission(player, Permission.WORLD)) {
                player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den Detonator nutzen");
                return;
            }
            ItemStack item = event.getItem();
            event.setCancelled(true);
            switch (event.getAction()) {
                case LEFT_CLICK_BLOCK:
                    Detoloader detoloader = FlatteningWrapper.impl.onPlayerInteractLoader(event);

                    if (detoloader == null) {
                        return;
                    } else if (detoloader.activation == -1) {
                        print(player, detoloader.getBlock(), detoloader.addBack ? Detonator.getDetonator(player, player.getInventory().getItemInMainHand()).getLocs().size() : 0);
                        return;
                    }

                    if (event.getPlayer().isSneaking()) {
                        player.getInventory().setItemInMainHand(Detonator.toggleLocation(item, player, detoloader, event.getClickedBlock().getLocation()));
                    } else {
                        if (detoloader.getActivation() == 0) {
                            player.getInventory().setItemInMainHand(Detonator.setLocation(player, item, new Detoloader.DetonatorActivation(event.getClickedBlock().getLocation())));
                        } else {
                            player.getInventory().setItemInMainHand(Detonator.setLocation(player, item, new Detoloader.DetonatorActivation(detoloader.activation, event.getClickedBlock().getLocation())));
                        }
                        print(player, detoloader.addBack ? "§e" + detoloader.getBlock() + " gesetzt" :
                                detoloader.getBlock(), detoloader.addBack ? Detonator.getDetonator(player, player.getInventory().getItemInMainHand()).getLocs().size() : 0);
                    }
                    break;
                case RIGHT_CLICK_AIR:
                case RIGHT_CLICK_BLOCK:
                    Detonator.execute(player);
                    break;
            }
        }
    }

    public static void print(Player player, String message, int size) {
        if (size == 0) {
            player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message));
        } else {
            player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message + " §8" + size));
        }
    }
}
