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

import de.steamwar.entity.RArmorStand;
import de.steamwar.lobby.LobbySystem;
import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Hologram implements ConfigurationSerializable {

    private static final Map<String, Hologram> holograms = new HashMap<>();

    public static List<Hologram> getHolograms() {
        return new ArrayList<>(holograms.values());
    }
    public static Hologram getHologram(String id) {
        return holograms.get(id);
    }

    private final String id;
    private final Location location;
    private final RArmorStand entity;

    public Hologram(Map<String, Object> map) {
        this((String) map.get("id"), (Location) map.get("location"), (String) map.get("text"), false);
    }

    public Hologram(String id, Location location, String text, boolean debugHologram) {
        this.id = id;
        this.location = location;
        this.entity = new RArmorStand(LobbySystem.getEntityServer(debugHologram), location, RArmorStand.Size.MARKER);

        entity.setInvisible(true);
        entity.setDisplayName(text);

        if(id != null)
            holograms.put(id, this);
    }

    public void updateText(String text) {
        entity.setDisplayName(text);
    }

    @Override
    public Map<String, Object> serialize() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("location", location);
        map.put("text", entity.getDisplayName());
        return map;
    }

    public void delete() {
        entity.die();
        if(id != null)
            holograms.remove(id);
    }

    @Override
    public String toString() {
        return id + " " + entity.getDisplayName();
    }
}
