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

import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

public class WorldEditRendererFallback {

    private static final int VIEW_DISTANCE = 64;
    private static final int SQ_VIEW_DISTANCE = VIEW_DISTANCE * VIEW_DISTANCE;

    private static final double STEP_SIZE = 0.5;
    private static final Vector ONES = new Vector(1, 1, 1);
    private static final Vector STEPS = new Vector(STEP_SIZE, STEP_SIZE, STEP_SIZE);

    public void draw(Player player, boolean scheduled, boolean clipboard, Vector min, Vector max) {
        if (!scheduled) return;

        max = max.clone().add(ONES);
        drawLine(player, clipboard, new Vector(min.getX(), min.getY(), min.getZ()), new Vector(max.getX(), min.getY(), min.getZ()));
        drawLine(player, clipboard, new Vector(min.getX(), max.getY(), min.getZ()), new Vector(max.getX(), max.getY(), min.getZ()));
        drawLine(player, clipboard, new Vector(min.getX(), min.getY(), max.getZ()), new Vector(max.getX(), min.getY(), max.getZ()));
        drawLine(player, clipboard, new Vector(min.getX(), max.getY(), max.getZ()), new Vector(max.getX(), max.getY(), max.getZ()));

        drawLine(player, clipboard, new Vector(min.getX(), min.getY(), min.getZ()), new Vector(min.getX(), max.getY(), min.getZ()));
        drawLine(player, clipboard, new Vector(max.getX(), min.getY(), min.getZ()), new Vector(max.getX(), max.getY(), min.getZ()));
        drawLine(player, clipboard, new Vector(min.getX(), min.getY(), max.getZ()), new Vector(min.getX(), max.getY(), max.getZ()));
        drawLine(player, clipboard, new Vector(max.getX(), min.getY(), max.getZ()), new Vector(max.getX(), max.getY(), max.getZ()));

        drawLine(player, clipboard, new Vector(min.getX(), min.getY(), min.getZ()), new Vector(min.getX(), min.getY(), max.getZ()));
        drawLine(player, clipboard, new Vector(max.getX(), min.getY(), min.getZ()), new Vector(max.getX(), min.getY(), max.getZ()));
        drawLine(player, clipboard, new Vector(min.getX(), max.getY(), min.getZ()), new Vector(min.getX(), max.getY(), max.getZ()));
        drawLine(player, clipboard, new Vector(max.getX(), max.getY(), min.getZ()), new Vector(max.getX(), max.getY(), max.getZ()));
    }

    private void drawLine(Player player, boolean clipboard, Vector min, Vector max) {
        Particle particle;
        if (clipboard) {
            particle = Particle.HAPPY_VILLAGER;
        } else {
            particle = Particle.DRAGON_BREATH;
        }

        Vector stepSize = max.clone().subtract(min).normalize().multiply(STEPS);
        while (min.getX() <= max.getX() && min.getY() <= max.getY() && min.getZ() <= max.getZ()) {
            Location location = player.getLocation();
            double dx = min.getX() - location.getX();
            double dy = min.getY() - location.getY();
            double dz = min.getZ() - location.getZ();
            if (dx * dx + dy * dy + dz * dz > SQ_VIEW_DISTANCE) {
                min.add(stepSize);
                continue;
            }

            player.spawnParticle(particle, min.getX(), min.getY(), min.getZ(), 1, 0.0, 0.0, 0.0, 0.0);
            min.add(stepSize);
        }
    }
}
