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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.core.SWPlayer;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
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.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

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

@Linked
public class ObserverTracerListener implements Listener {

    static Set<Player> enabled = new HashSet<>();

    public ObserverTracerListener() {
        Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
            SWPlayer.allWithSingleComponent(ObserverTracer.class).forEach(pair -> {
                if (pair.getPlayer().getGameMode() != GameMode.SPECTATOR) return;
                pair.getComponent().show();
            });
        }, 15L, 15L);
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (!Permission.BUILD.hasPermission(event.getPlayer())) return;
        if (!enabled.contains(event.getPlayer())) return;
        if (event.getClickedBlock() == null) return;
        ObserverTracer observerTracer = SWPlayer.of(event.getPlayer()).getComponent(ObserverTracer.class).orElse(null);
        if (observerTracer != null) {
            Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
                if (!observerTracer.trace()) {
                    createNew(event);
                }
                SWPlayer.allWithSingleComponent(ObserverTracer.class).forEach(pair -> {
                    pair.getComponent().trace();
                });
            }, 1L);
        } else {
            createNew(event);
        }
    }

    private void createNew(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }
        if (event.getClickedBlock().getType() == Material.OBSERVER) {
            ObserverTracer observerTracer = new ObserverTracer(event.getPlayer(), event.getClickedBlock());
            observerTracer.trace();
            SWPlayer.of(event.getPlayer()).setComponent(observerTracer);
        }
    }

    @EventHandler
    public void onBauMemberUpdate(BauMemberUpdateEvent event) {
        event.getNewBuilder().forEach(player -> SWPlayer.of(player).removeComponent(ObserverTracer.class));
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        enabled.add(event.getPlayer());
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        enabled.remove(event.getPlayer());
    }
}
