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

package com.hypixel.hytale.server.core.command.commands.world.entity;

import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.server.core.command.system.CommandSender;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.AddReason;
import java.util.UUID;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.codec.validation.Validators;
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.DefaultArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.EntityWrappedArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class EntityCloneCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_GENERAL_NO_ENTITY_IN_VIEW;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ENTITY_CLONE_CLONED;
    @Nonnull
    private final EntityWrappedArg entityArg;
    @Nonnull
    private final DefaultArg<Integer> countArg;
    
    public EntityCloneCommand() {
        super("clone", "server.commands.entity.clone.desc");
        this.entityArg = this.withOptionalArg("entity", "server.commands.entity.clone.entity.desc", ArgTypes.ENTITY_ID);
        this.countArg = this.withDefaultArg("count", "server.commands.entity.clone.count.desc", ArgTypes.INTEGER, 1, "server.commands.entity.clone.count.default").addValidator((Validator<Integer>)Validators.greaterThan((DataType)0));
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final Ref<EntityStore> entityRef = this.entityArg.get(store, context);
        if (entityRef == null || !entityRef.isValid()) {
            context.sendMessage(EntityCloneCommand.MESSAGE_GENERAL_NO_ENTITY_IN_VIEW);
            return;
        }
        final CommandSender sender = context.sender();
        final int count = this.countArg.get(context);
        for (int i = 0; i < count; ++i) {
            final Holder<EntityStore> copy = store.copyEntity(entityRef);
            copy.replaceComponent(UUIDComponent.getComponentType(), new UUIDComponent(UUID.randomUUID()));
            store.addEntity(copy, AddReason.SPAWN);
        }
        if (count == 1) {
            sender.sendMessage(EntityCloneCommand.MESSAGE_COMMANDS_ENTITY_CLONE_CLONED);
        }
        else {
            sender.sendMessage(Message.translation("server.commands.entity.clone.cloned.multiple").param("count", count));
        }
    }
    
    public static void cloneEntity(@Nonnull final CommandSender sender, @Nonnull final Ref<EntityStore> entityReference, @Nonnull final Store<EntityStore> store) {
        final Holder<EntityStore> copy = store.copyEntity(entityReference);
        if (copy.getComponent(UUIDComponent.getComponentType()) != null) {
            copy.replaceComponent(UUIDComponent.getComponentType(), new UUIDComponent(UUID.randomUUID()));
        }
        store.addEntity(copy, AddReason.SPAWN);
    }
    
    static {
        MESSAGE_GENERAL_NO_ENTITY_IN_VIEW = Message.translation("server.general.noEntityInView");
        MESSAGE_COMMANDS_ENTITY_CLONE_CLONED = Message.translation("server.commands.entity.clone.cloned");
    }
}
