/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2020  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 Axis Aligned Box (12 CAALine)
 */
public class CAABox extends CEntity {

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

    private Location pos1;
    private Location pos2;

    public CAABox(REntityServer server) {
        super(server);
    }

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

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

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

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

    private void updateAndSpawnLines() {
        if (pos1 == null || pos2 == null) return;
        if (entities.isEmpty()) {
            for (int i = 0; i < 12; i++) {
                entities.add(new CAALine(server));
            }
        }

        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));

        List<CAALine> lines = getEntitiesByType(CAALine.class);
        lines.forEach(line -> line.setFrom(null).setTo(null));

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

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

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