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

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.util.Vector;

import java.util.List;

/**
 * Compound Box (12 CLine)
 */
public class CWireframe extends CEntity {

    public static final float DEFAULT_WIDTH = 1 / 16f;
    private float width = DEFAULT_WIDTH;

    private Location pos1;
    private Location pos2;

    public CWireframe(REntityServer server) {
        super(server);
        for (int i = 0; i < 12; i++) {
            entities.add(new CLine(server));
        }
    }

    public CWireframe setPos1And2(Location pos1, Location pos2) {
        this.pos1 = pos1;
        this.pos2 = pos2;
        updateAndSpawnLines();
        return this;
    }

    public CWireframe setPos1(Location pos1) {
        this.pos1 = pos1;
        updateAndSpawnLines();
        return this;
    }

    public CWireframe setPos2(Location pos2) {
        this.pos2 = pos2;
        updateAndSpawnLines();
        return this;
    }

    public CWireframe setWidth(float width) {
        this.width = width;
        updateAndSpawnLines();
        getEntitiesByType(CLine.class).forEach(haaLine -> {
            haaLine.setWidth(width);
        });
        return this;
    }

    public CWireframe setBlock(BlockData blockData) {
        getEntitiesByType(CLine.class).forEach(haaLine -> {
            haaLine.setBlock(blockData);
        });
        return this;
    }

    private void updateAndSpawnLines() {
        List<CLine> lines = getEntitiesByType(CLine.class);
        if (pos1 == null || pos2 == null) {
            lines.forEach(line -> line.setFromAndTo(null, null));
            return;
        }

        World world = pos1.getWorld();
        Vector min = Vector.getMinimum(pos1.toVector(), pos2.toVector());
        Vector max = Vector.getMaximum(pos1.toVector(), pos2.toVector())
                .add(new Vector(1 - width, 1 - width, 1 - width));

        lines.get(0).setFromAndTo(new Vector(min.getX(), min.getY(), min.getZ()).toLocation(world), new Vector(max.getX() + width, min.getY(), min.getZ()).toLocation(world));
        lines.get(1).setFromAndTo(new Vector(min.getX(), max.getY(), min.getZ()).toLocation(world), new Vector(max.getX() + width, max.getY(), min.getZ()).toLocation(world));
        lines.get(2).setFromAndTo(new Vector(min.getX(), min.getY(), max.getZ()).toLocation(world), new Vector(max.getX() + width, min.getY(), max.getZ()).toLocation(world));
        lines.get(3).setFromAndTo(new Vector(min.getX(), max.getY(), max.getZ()).toLocation(world), new Vector(max.getX() + width, max.getY(), max.getZ()).toLocation(world));

        lines.get(4).setFromAndTo(new Vector(min.getX(), min.getY(), min.getZ()).toLocation(world), new Vector(min.getX(), max.getY() + width, min.getZ()).toLocation(world));
        lines.get(5).setFromAndTo(new Vector(max.getX(), min.getY(), min.getZ()).toLocation(world), new Vector(max.getX(), max.getY() + width, min.getZ()).toLocation(world));
        lines.get(6).setFromAndTo(new Vector(min.getX(), min.getY(), max.getZ()).toLocation(world), new Vector(min.getX(), max.getY() + width, max.getZ()).toLocation(world));
        lines.get(7).setFromAndTo(new Vector(max.getX(), min.getY(), max.getZ()).toLocation(world), new Vector(max.getX(), max.getY() + width, max.getZ()).toLocation(world));

        lines.get(8).setFromAndTo(new Vector(min.getX(), min.getY(), min.getZ()).toLocation(world), new Vector(min.getX(), min.getY(), max.getZ() + width).toLocation(world));
        lines.get(9).setFromAndTo(new Vector(max.getX(), min.getY(), min.getZ()).toLocation(world), new Vector(max.getX(), min.getY(), max.getZ() + width).toLocation(world));
        lines.get(10).setFromAndTo(new Vector(min.getX(), max.getY(), min.getZ()).toLocation(world), new Vector(min.getX(), max.getY(), max.getZ() + width).toLocation(world));
        lines.get(11).setFromAndTo(new Vector(max.getX(), max.getY(), min.getZ()).toLocation(world), new Vector(max.getX(), max.getY(), max.getZ() + width).toLocation(world));
    }
}
