package de.slpnetwork.managers;

import com.hypixel.hytale.common.util.ArrayUtil;
import com.hypixel.hytale.protocol.Direction;
import com.hypixel.hytale.protocol.Position;
import com.hypixel.hytale.protocol.Transform;
import com.hypixel.hytale.protocol.packets.worldmap.MapMarker;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.entity.entities.player.data.PlayerWorldData;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.world.worldmap.WorldMapManager;
import de.slpnetwork.ManyWaypoints;

import java.util.Map;
import java.util.UUID;

public class WaypointManager {
    private ManyWaypoints plugin;

    public WaypointManager (ManyWaypoints plugin) {
        this.plugin = plugin;
    }

    /**
     * handles adding a waypoint to the players map and storage
     * @param player the player reference of the waypoint owner
     * @param transformComponent the players position as transform
     * @param markerName the name of the waypoint
     */
    public void AddWaypoint(Player player, TransformComponent transformComponent, String markerName, PlayerWorldData perWorldData, MapMarker[] worldMapMarkers) {
        WorldMapManager wm = player.getWorld().getWorldMapManager();
        Map<String, MapMarker> pois = wm.getPointsOfInterest();

        Transform markerTransform = new Transform();

        markerTransform.position = new Position(transformComponent.getPosition().getX(),transformComponent.getPosition().getY(),transformComponent.getPosition().getZ());
        markerTransform.orientation = new Direction();

        MapMarker marker = new MapMarker();
        marker.id = UUID.randomUUID().toString();
        marker.name = markerName;
        marker.transform = markerTransform;
        marker.markerImage = "manywaypoints:ui/map/wp_icon";

        perWorldData.setWorldMapMarkers((MapMarker[]) ArrayUtil.append(worldMapMarkers, marker));
        // pois.putIfAbsent(markerName, marker); this might not be required
    }

    /**
     * handles removing a waypoint from the players map and storage
     * @param player the player reference of the waypoint owner
     * @param transformComponent the players position as transform
     * @param markerName the name of the waypoint
     */
    public void RemoveWaypoint(Player player,TransformComponent transformComponent, String markerName, PlayerWorldData perWorldData, MapMarker[] worldMapMarkers) {
        WorldMapManager wm = player.getWorld().getWorldMapManager();
        Map<String, MapMarker> pois = wm.getPointsOfInterest();

        // TODO: implement removing waypoints code reference WorldMap.class:383 this function seems to remove MapMarkers from the Map

        pois.remove(markerName);
    }

    /**
     * looks up a waypoint for a player in storage if and shows or hides it if found
     * @param player the player reference of the waypoint owner
     * @param name the name of the waypoint
     */
    public void ToggleWaypoint(Player player, String name) {}
}
