// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.parkour.commands;

import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.math.vector.Vector3d;
import java.util.UUID;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.server.core.modules.entity.component.HiddenFromAdventurePlayers;
import com.hypixel.hytale.server.core.modules.entity.component.Intangible;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.entity.nameplate.Nameplate;
import com.hypixel.hytale.server.core.modules.entity.component.PersistentModel;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.builtin.parkour.ParkourCheckpoint;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.builtin.parkour.ParkourPlugin;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class CheckpointAddCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHECKPOINT_ADD_FAILED;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHECKPOINT_ADD_SUCCESS;
    @Nonnull
    private final RequiredArg<Integer> indexArg;
    
    public CheckpointAddCommand() {
        super("add", "server.commands.checkpoint.add.desc");
        this.indexArg = this.withRequiredArg("index", "server.commands.checkpoint.add.index.desc", ArgTypes.INTEGER);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
        final Integer index = this.indexArg.get(context);
        final Int2ObjectMap<UUID> checkpointUUIDMap = ParkourPlugin.get().getCheckpointUUIDMap();
        if (checkpointUUIDMap.containsKey(index)) {
            context.sendMessage(CheckpointAddCommand.MESSAGE_COMMANDS_CHECKPOINT_ADD_FAILED);
            return;
        }
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final Vector3f rotation = transformComponent.getRotation();
        final Holder<EntityStore> holder = EntityStore.REGISTRY.newHolder();
        holder.addComponent(ParkourCheckpoint.getComponentType(), new ParkourCheckpoint(index));
        final Model model = ParkourPlugin.get().getParkourCheckpointModel();
        holder.addComponent(ModelComponent.getComponentType(), new ModelComponent(model));
        holder.addComponent(PersistentModel.getComponentType(), new PersistentModel(model.toReference()));
        holder.addComponent(Nameplate.getComponentType(), new Nameplate(Integer.toString(index)));
        final TransformComponent transform = new TransformComponent(position, rotation);
        holder.addComponent(TransformComponent.getComponentType(), transform);
        holder.ensureComponent(UUIDComponent.getComponentType());
        holder.ensureComponent(Intangible.getComponentType());
        holder.ensureComponent(HiddenFromAdventurePlayers.getComponentType());
        store.addEntity(holder, AddReason.SPAWN);
        context.sendMessage(CheckpointAddCommand.MESSAGE_COMMANDS_CHECKPOINT_ADD_SUCCESS);
    }
    
    static {
        MESSAGE_COMMANDS_CHECKPOINT_ADD_FAILED = Message.translation("server.commands.checkpoint.add.failed");
        MESSAGE_COMMANDS_CHECKPOINT_ADD_SUCCESS = Message.translation("server.commands.checkpoint.add.success");
    }
}
