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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.region.RegionSystem;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.bausystem.utils.bossbar.BauSystemBossbar;
import de.steamwar.bausystem.utils.bossbar.BossBarService;
import de.steamwar.linkage.Linked;
import de.steamwar.network.packets.PacketHandler;
import de.steamwar.network.packets.server.BaumemberUpdatePacket;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

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

@Linked
public class BauMemberUpdate extends PacketHandler implements Listener {

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

    @Handler
    public void baumemberUpdate(BaumemberUpdatePacket baumemberUpdatePacket) {
        baumemberUpdate();
    }

    public static void baumemberUpdate() {
        Set<Player> newSpectator = new HashSet<>();
        Set<Player> newBuilder = new HashSet<>();
        Bukkit.getOnlinePlayers().forEach(player -> {
            if (Permission.SPECTATOR.hasPermission(player)) {
                if (!SPECTATORS.contains(player)) {
                    SPECTATORS.add(player);
                    newSpectator.add(player);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, -1, 1, false, false, false));
                    showSpectatorNotice(player);
                }
            } else {
                if (SPECTATORS.contains(player)) {
                    SPECTATORS.remove(player);
                    newBuilder.add(player);
                    player.removePotionEffect(PotionEffectType.GLOWING);
                    BossBarService.instance.remove(player, RegionSystem.INSTANCE.getGlobalRegion(), "spectator");
                }
            }
        });
        Bukkit.getPluginManager().callEvent(new BauMemberUpdateEvent(newSpectator, newBuilder));
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        if (!Permission.BUILD.hasPermission(event.getPlayer())) {
            SPECTATORS.add(event.getPlayer());
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, -1, 1, false, false, false));
            showSpectatorNotice(event.getPlayer());
        } else {
            event.getPlayer().removePotionEffect(PotionEffectType.GLOWING);
        }
    }

    private static void showSpectatorNotice(Player player) {
        BauSystemBossbar bossbar = BossBarService.instance.get(player, RegionSystem.INSTANCE.getGlobalRegion(), "spectator");
        bossbar.setTitle(BauSystem.MESSAGE.parse("SPECTATOR", player));
        bossbar.setColor(BarColor.WHITE);
        bossbar.setStyle(BarStyle.SOLID);
        bossbar.setProgress(1.0);
    }

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

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        event.setDeathMessage(null);
        if (SPECTATORS.contains(event.getEntity())) {
            event.getDrops().clear();
        }
    }

    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
            if (SPECTATORS.contains(event.getPlayer())) {
                event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, -1, 1, false, false, false));
            }
        }, 1);
    }

    @EventHandler
    public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
        Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
            if (SPECTATORS.contains(event.getPlayer())) {
                event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, -1, 1, false, false, false));
            }
        }, 1);
    }
}
