/*
 *   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.bausystem.features.warp;

import de.steamwar.bausystem.region.RegionSystem;
import de.steamwar.bausystem.worlddata.WorldData;
import lombok.Getter;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import yapion.hierarchy.types.YAPIONObject;

import java.util.*;

@Getter
public class Warp {

    private static Map<String, Warp> warpMap = new HashMap<>();

    public static void enable() {
        Warp worldSpawn = new Warp("WorldSpawn");
        worldSpawn.setLocation(RegionSystem.INSTANCE.getWorldSpawn());
        worldSpawn.setMat(Material.NETHER_STAR);
        warpMap.put("WorldSpawn", worldSpawn);
    }

    private String name;
    private Location location;
    private Material mat;

    private Warp(String name) {
        this.name = name;
    }

    public Warp(String name, Player player) {
        this.name = name;
        this.location = player.getLocation();
        this.mat = Material.COMPASS;
        warpMap.put(name, this);
    }

    public Warp(String name, YAPIONObject object) {
        this.name = name;
        double x = object.getPlainValue("x");
        double y = object.getPlainValue("y");
        double z = object.getPlainValue("z");
        float yaw = object.getPlainValue("yaw");
        float pitch = object.getPlainValue("pitch");
        location = new Location(Bukkit.getWorlds().get(0), x, y, z, yaw, pitch);
        mat = Material.getMaterial(object.getPlainValue("material"));
        warpMap.put(name, this);
    }

    public YAPIONObject output() {
        YAPIONObject yapionObject = new YAPIONObject();
        yapionObject.add("x", location.getX());
        yapionObject.add("y", location.getY());
        yapionObject.add("z", location.getZ());
        yapionObject.add("yaw", location.getYaw());
        yapionObject.add("pitch", location.getPitch());
        yapionObject.add("material", mat.toString());
        return yapionObject;
    }

    public static Set<String> getWarpNames() {
        return warpMap.keySet();
    }

    public static List<Warp> getWarps() {
        return new ArrayList<>(warpMap.values());
    }

    public static Warp getWarp(String name) {
        return warpMap.get(name);
    }

    public void setMat(Material mat) {
        this.mat = mat;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public void delete() {
        warpMap.remove(name);
        WorldData.getWarpData().remove(name);
    }

    public void teleport(Player player) {
        player.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
        player.playSound(location, Sound.ENTITY_ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1, 1);
    }
}
