/*
 *   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.block.BlockFace;
import org.bukkit.util.Transformation;
import org.joml.Quaternionf;
import org.joml.Vector3f;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CCubedTextDisplay extends CEntity {

    private Map<BlockFace, RTextDisplay> displays = new HashMap<>();

    public CCubedTextDisplay(REntityServer server, Location loc) {
        super(server);
        {
            Location location = loc.clone().add(0.5, 0.4, 1.01);
            RTextDisplay display = new RTextDisplay(server, location);
            entities.add(display);
            displays.put(BlockFace.SOUTH, display);
        }
        {
            Location location = loc.clone().add(0.5, 0.4, -0.01);
            RTextDisplay display = new RTextDisplay(server, location);
            display.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotateY((float) Math.toRadians(180)), new Vector3f(1, 1, 1), new Quaternionf()));
            entities.add(display);
            displays.put(BlockFace.NORTH, display);
        }
        {
            Location location = loc.clone().add(1.01, 0.4, 0.5);
            RTextDisplay display = new RTextDisplay(server, location);
            display.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotateY((float) Math.toRadians(90)), new Vector3f(1, 1, 1), new Quaternionf()));
            entities.add(display);
            displays.put(BlockFace.EAST, display);
        }
        {
            Location location = loc.clone().add(-0.01, 0.4, 0.5);
            RTextDisplay display = new RTextDisplay(server, location);
            display.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotateY((float) Math.toRadians(270)), new Vector3f(1, 1, 1), new Quaternionf()));
            entities.add(display);
            displays.put(BlockFace.WEST, display);
        }
        {
            Location location = loc.clone().add(0.5, 1.01, 0.6);
            RTextDisplay display = new RTextDisplay(server, location);
            display.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotationX((float) Math.toRadians(270)), new Vector3f(1, 1, 1), new Quaternionf()));
            entities.add(display);
            displays.put(BlockFace.UP, display);
        }
        {
            Location location = loc.clone().add(0.5, -0.01, 0.4);
            RTextDisplay display = new RTextDisplay(server, location);
            display.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf().rotationX((float) Math.toRadians(90)), new Vector3f(1, 1, 1), new Quaternionf()));
            entities.add(display);
            displays.put(BlockFace.DOWN, display);
        }
    }

    public void hide(Set<BlockFace> blockFaceSet) {
        for (BlockFace blockFace : BlockFace.values()) {
            RTextDisplay display = displays.get(blockFace);
            if (display == null) continue;
            display.hide(blockFaceSet.contains(blockFace));
        }
        if (displays.values().stream().allMatch(RTextDisplay::isHidden)) {
            displays.values().forEach(rTextDisplay -> rTextDisplay.hide(false));
        }
    }

    @Override
    public void move(Location location) {
    }

    @Override
    public void move(double locX, double locY, double locZ, float pitch, float yaw, byte headYaw) {
    }

    public void setText(String text) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setText(text));
    }

    public void setLineWidth(int lineWidth) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setLineWidth(lineWidth));
    }

    public void setTextOpacity(byte textOpacity) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setTextOpacity(textOpacity));
    }

    public void setShadowed(boolean shadowed) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setShadowed(shadowed));
    }

    public void setSeeThrough(boolean seeThrough) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setSeeThrough(seeThrough));
    }

    public void setBackgroundColor(int color) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setBackgroundColor(color));
    }

    public void setDefaultBackground(boolean defaultBackground) {
        displays.values().forEach(rTextDisplay -> rTextDisplay.setDefaultBackground(defaultBackground));
    }
}
