/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2023  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.team.boundary;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.core.TrickyParticleWrapper;
import de.steamwar.core.TrickyTrialsWrapper;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

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

@Linked
public class BoundaryViewer implements Listener {

    public static Set<Player> viewers = new HashSet<>();

    {
        Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
            if (viewers.isEmpty()) {
                return;
            }
            viewers.forEach(player -> {
                Region region = Region.getRegion(player.getLocation());
                if (region.isGlobal()) return;
                showRegion(region, player);
                if (region.getLinkedRegion() != null) {
                    showRegion(region.getLinkedRegion(), player);
                }
            });
        }, 20, 20);
    }

    private void showRegion(Region region, Player player) {
        drawCuboid(player, TrickyParticleWrapper.impl.getVillagerHappy(), region.getMinPoint(), region.getMaxPoint());
        if (region.hasType(RegionType.TESTBLOCK)) {
            drawCuboid(player, Particle.END_ROD, region.getMinPointTestblockExtension(), region.getMaxPointTestblockExtension());
        }
        if (region.hasType(RegionType.BUILD)) {
            drawCuboid(player, Particle.END_ROD, region.getMinPointBuildExtension(), region.getMaxPointBuildExtension());
        }
    }

    private void drawCuboid(Player player, Particle particle, Point min, Point max) {
        for (int z = min.getZ(); z <= max.getZ() + 1; z++) {
            player.spawnParticle(particle, min.getX(), min.getY(), z, 1, 0, 0, 0, 0);
            player.spawnParticle(particle, min.getX(), max.getY() + 1, z, 1, 0, 0, 0, 0);
            player.spawnParticle(particle, max.getX() + 1, min.getY(), z, 1, 0, 0, 0, 0);
            player.spawnParticle(particle, max.getX() + 1, max.getY() + 1, z, 1, 0, 0, 0, 0);
        }
        for (int x = min.getX(); x <= max.getX() + 1; x++) {
            player.spawnParticle(particle, x, min.getY(), min.getZ(), 1, 0, 0, 0, 0);
            player.spawnParticle(particle, x, min.getY(), max.getZ() + 1, 1, 0, 0, 0, 0);
            player.spawnParticle(particle, x, max.getY() + 1, min.getZ(), 1, 0, 0, 0, 0);
            player.spawnParticle(particle, x, max.getY() + 1, max.getZ() + 1, 1, 0, 0, 0, 0);
        }
        for (int y = min.getY(); y <= max.getY() + 1; y++) {
            player.spawnParticle(particle, min.getX(), y, min.getZ(), 1, 0, 0, 0, 0);
            player.spawnParticle(particle, min.getX(), y, max.getZ() + 1, 1, 0, 0, 0, 0);
            player.spawnParticle(particle, max.getX() + 1, y, min.getZ(), 1, 0, 0, 0, 0);
            player.spawnParticle(particle, max.getX() + 1, y, max.getZ() + 1, 1, 0, 0, 0, 0);
        }
    }

    private void drawPoints(Player player, Particle particle, Point... points) {
        for (Point point : points) {
            player.spawnParticle(particle, point.toLocation(player), 1, 0, 0, 0, 0);
        }
    }

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