/*
 *     This file is a part of the SteamWar software.
 *
 *     Copyright (C) 2024  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 com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.world.World;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

public class WorldEditRenderer {

    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 Material WAND = FlatteningWrapper.impl.getMaterial("WOOD_AXE");

    private final WorldEditPlugin we;

    public WorldEditRenderer() {
        we = WorldEditWrapper.getWorldEditPlugin();

        Bukkit.getScheduler().runTaskTimer(Core.getInstance(), this::render, 20, 20);
    }

    private void render() {
        for(Player player : Bukkit.getOnlinePlayers()) {
            //noinspection deprecation
            if(player.getItemInHand().getType() != WAND)
                continue;

            LocalSession session = we.getSession(player);
            try {
                Clipboard clipboard = session.getClipboard().getClipboard();
                Vector pos = player.getLocation().toVector();
                Region region = clipboard.getRegion();
                Transform transform = session.getClipboard().getTransform();
                Vector a = WorldEditWrapper.impl.applyTransform(WorldEditWrapper.impl.getMinimum(region).subtract(WorldEditWrapper.impl.getOrigin(clipboard)), transform).add(pos);
                Vector b = WorldEditWrapper.impl.applyTransform(WorldEditWrapper.impl.getMaximum(region).subtract(WorldEditWrapper.impl.getOrigin(clipboard)), transform).add(pos);
                drawCuboid(Vector.getMinimum(a, b), Vector.getMaximum(a, b), TrickyParticleWrapper.impl.getVillagerHappy(), player);
            } catch (EmptyClipboardException e) {
                //ignore
            }

            World world = session.getSelectionWorld();
            if(world != null) {
                RegionSelector regionSelector = session.getRegionSelector(world);
                try {
                    Region region = regionSelector.getRegion();
                    drawCuboid(WorldEditWrapper.impl.getMinimum(region), WorldEditWrapper.impl.getMaximum(region), Particle.DRAGON_BREATH, player);
                } catch (IncompleteRegionException e) {
                    //ignore
                }
            }
        }
    }

    private void drawCuboid(Vector min, Vector max, Particle particle, Player owner) {
        max.add(ONES);

        for(double x = min.getBlockX(); x <= max.getBlockX(); x += STEP_SIZE) {
            draw(x, min.getBlockY(), min.getBlockZ(), particle, owner);
            draw(x, min.getBlockY(), max.getBlockZ(), particle, owner);
            draw(x, max.getBlockY(), min.getBlockZ(), particle, owner);
            draw(x, max.getBlockY(), max.getBlockZ(), particle, owner);
        }

        for(double y = min.getBlockY() + STEP_SIZE; y <= max.getBlockY() - STEP_SIZE; y += STEP_SIZE) {
            draw(min.getBlockX(), y, min.getBlockZ(), particle, owner);
            draw(min.getBlockX(), y, max.getBlockZ(), particle, owner);
            draw(max.getBlockX(), y, min.getBlockZ(), particle, owner);
            draw(max.getBlockX(), y, max.getBlockZ(), particle, owner);
        }

        for(double z = min.getBlockZ() + STEP_SIZE; z <= max.getBlockZ() - STEP_SIZE; z += STEP_SIZE) {
            draw(min.getBlockX(), min.getBlockY(), z, particle, owner);
            draw(min.getBlockX(), max.getBlockY(), z, particle, owner);
            draw(max.getBlockX(), min.getBlockY(), z, particle, owner);
            draw(max.getBlockX(), max.getBlockY(), z, particle, owner);
        }
    }

    private void draw(double x, double y, double z, Particle particle, Player owner) {
        for(Player player : Bukkit.getOnlinePlayers()) {
            Location location = player.getLocation();
            double dx = x - location.getX();
            double dy = y - location.getY();
            double dz = z - location.getZ();
            if(dx*dx + dy*dy + dz*dz > SQ_VIEW_DISTANCE)
                continue;

            player.spawnParticle(player == owner ? particle : Particle.TOWN_AURA, x, y, z, 1, 0.0, 0.0, 0.0, 0.0);
        }
    }
}
