/*
 *   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.features.detonator;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage;
import de.steamwar.bausystem.features.detonator.storage.ItemStorage;
import de.steamwar.linkage.Linked;
import org.bukkit.Location;
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.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;

import java.util.HashSet;
import java.util.Set;

@Linked
public class DetonatorListener implements Listener {

    static final Set<Player> HAS_UPDATED = new HashSet<>();

    static void addLocationToDetonator(Location location, Player p) {
        Detoblock detoblock = Detonator.getBlock(location.getBlock());
        if (detoblock == Detoblock.INVALID) {
            SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_INVALID_BLOCK", p));
            return;
        }
        DetonatorStorage detonator = new ItemStorage(p);
        if (!p.isSneaking()) {
            detonator.clear();
        }
        if (detonator.getLocations().contains(location)) {
            detonator.removeLocation(location);
            SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_LOC_REMOVE", p, BauSystem.MESSAGE.parse(detoblock.getName(), p)));
        } else {
            detonator.addLocation(location);
            SWUtils.sendToActionbar(p, BauSystem.MESSAGE.parse("DETONATOR_LOC_ADD", p, BauSystem.MESSAGE.parse(detoblock.getName(), p)));
        }
        detonator.write();
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
        Player p = event.getPlayer();
        if (Detonator.isDetonator(p.getInventory().getItemInMainHand())) {
            event.setCancelled(true);
            addLocationToDetonator(event.getBlock().getLocation(), p);
            HAS_UPDATED.add(event.getPlayer());
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
        if (!Detonator.isDetonator(event.getItem())) {
            return;
        }

        if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            event.setCancelled(true);
            DetonatorStorage detonator = new ItemStorage(event.getPlayer());
            Detonator.activateDetonator(detonator);
            HAS_UPDATED.add(event.getPlayer());
        }
    }

    @EventHandler(ignoreCancelled = true)
    public void onPlayerMove(PlayerMoveEvent event) {
        if (!Permission.BUILD.hasPermission(event.getPlayer()) ||!Detonator.isDetonator(event.getPlayer().getInventory().getItemInMainHand())) {
            if (Detonator.hasActiveDetonatorShow(event.getPlayer())) {
                Detonator.hideDetonator(event.getPlayer());
            }
        } else {
            if (!Detonator.hasActiveDetonatorShow(event.getPlayer())) {
                Detonator.showDetonator(event.getPlayer(), new ItemStorage(event.getPlayer()).getLocations());
            }
        }

        if (HAS_UPDATED.contains(event.getPlayer())) {
            HAS_UPDATED.remove(event.getPlayer());
            if (Detonator.hasActiveDetonatorShow(event.getPlayer())) {
                Detonator.hideDetonator(event.getPlayer());
                Detonator.showDetonator(event.getPlayer(), new ItemStorage(event.getPlayer()).getLocations());
            }
        }
    }

    @EventHandler
    public void onPlayerItemHeld(PlayerItemHeldEvent event) {
        if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
        if (Detonator.isDetonator(event.getPlayer().getInventory().getItemInMainHand())) {
            HAS_UPDATED.add(event.getPlayer());
        }
    }

    @EventHandler
    public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
        if(!Permission.BUILD.hasPermission(event.getPlayer())) return;
        if (Detonator.isDetonator(event.getMainHandItem()) || Detonator.isDetonator(event.getOffHandItem())) {
            HAS_UPDATED.add(event.getPlayer());
        }
    }
}
