package de.slpnetwork.commands;

import com.hypixel.hytale.common.util.ArrayUtil;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.vector.Vector3d;
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.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.ParseResult;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;
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.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.server.core.universe.world.worldmap.WorldMapManager;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import de.slpnetwork.managers.WaypointManager;
import de.slpnetwork.utils.StringArgument;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import static de.slpnetwork.utils.MessageUtil.SendMessage;

public class WayPointCommand extends AbstractPlayerCommand {

    private final RequiredArg<String> actionArg;
    private final RequiredArg<String> markerNameArg;
    private WaypointManager plugin;

    public WayPointCommand(WaypointManager plugin, String name, String description) {
        super(name, description);
        this.plugin = plugin;

        actionArg = withRequiredArg("action", "should the server add or remove a waypoint", new StringArgument("action", "add | remove", "defines the command action"));
        markerNameArg = withRequiredArg("action", "should the server add or remove a waypoint", new StringArgument("markerName", "yourMarker", "name of the map marker in one word "));
    }

    @Override
    protected void execute(@Nonnull CommandContext commandContext, @Nonnull Store<EntityStore> store, @Nonnull Ref<EntityStore> ref, @Nonnull PlayerRef playerRef, @Nonnull World world) {

        Player player = store.getComponent(ref, Player.getComponentType());

        PlayerWorldData perWorldData = player.getPlayerConfigData().getPerWorldData(world.getName());
        MapMarker[] worldMapMarkers = perWorldData.getWorldMapMarkers();

        String markerName = commandContext.get(markerNameArg);
        String action = commandContext.get(actionArg);

        switch (action) {
            case "add":
                TransformComponent transformComponent = (TransformComponent)store.getComponent(ref, TransformComponent.getComponentType());
                assert transformComponent != null;


                SendMessage(player, "created marker: '" + markerName + "' on the map");
                break;
            case "remove":
                SendMessage(player, "removed marker: '" + markerName + "' from the map");
                break;
            case "toggle":
                // TODO: this needs storage to function therefor nothing can be done here yet
                break;
            default:
                SendMessage(player, "please used /waypoint <add | remove> <markername> ");
                break;
        }
    }
}
