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

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

import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.server.core.asset.type.soundevent.config.SoundEventLayer;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.AddReason;
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.ambience.AmbiencePlugin;
import com.hypixel.hytale.server.core.modules.entity.component.HiddenFromAdventurePlayers;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.entity.nameplate.Nameplate;
import com.hypixel.hytale.builtin.ambience.components.AmbientEmitterComponent;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.command.system.exceptions.GeneralCommandException;
import com.hypixel.hytale.server.core.Message;
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.system.RequiredArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.soundevent.config.SoundEvent;
import com.hypixel.hytale.server.core.command.system.arguments.types.AssetArgumentType;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class AmbienceEmitterAddCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final AssetArgumentType<SoundEvent, ?> SOUND_EVENT_ASSET_TYPE;
    @Nonnull
    private final RequiredArg<SoundEvent> soundEventArg;
    
    public AmbienceEmitterAddCommand() {
        super("add", "server.commands.ambience.emitter.add.desc");
        this.soundEventArg = this.withRequiredArg("soundEvent", "server.commands.ambience.emitter.add.arg.soundEvent.desc", (ArgumentType<SoundEvent>)AmbienceEmitterAddCommand.SOUND_EVENT_ASSET_TYPE);
    }
    
    @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) {
        if (!context.isPlayer()) {
            throw new GeneralCommandException(Message.translation("server.commands.errors.playerOnly"));
        }
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Holder<EntityStore> holder = EntityStore.REGISTRY.newHolder();
        final SoundEvent soundEvent = this.soundEventArg.get(context);
        boolean looping = false;
        for (final SoundEventLayer layer : soundEvent.getLayers()) {
            if (layer.isLooping()) {
                looping = true;
                break;
            }
        }
        if (!looping) {
            context.sendMessage(Message.translation("server.commands.ambience.emitter.add.notLooping").param("soundEventId", soundEvent.getId()));
            return;
        }
        final AmbientEmitterComponent emitterComponent = new AmbientEmitterComponent();
        emitterComponent.setSoundEventId(soundEvent.getId());
        holder.addComponent(AmbientEmitterComponent.getComponentType(), emitterComponent);
        final TransformComponent emitterTransform = transformComponent.clone();
        holder.addComponent(TransformComponent.getComponentType(), emitterTransform);
        holder.addComponent(Nameplate.getComponentType(), new Nameplate(soundEvent.getId()));
        holder.ensureComponent(UUIDComponent.getComponentType());
        holder.ensureComponent(HiddenFromAdventurePlayers.getComponentType());
        final Model model = AmbiencePlugin.get().getAmbientEmitterModel();
        holder.addComponent(ModelComponent.getComponentType(), new ModelComponent(model));
        holder.addComponent(PersistentModel.getComponentType(), new PersistentModel(model.toReference()));
        final Ref<EntityStore> emitterRef = store.addEntity(holder, AddReason.SPAWN);
        if (emitterRef == null || !emitterRef.isValid()) {
            context.sendMessage(Message.translation("server.commands.ambience.emitter.add.failed").param("soundEventId", soundEvent.getId()));
            return;
        }
        context.sendMessage(Message.translation("server.commands.ambience.emitter.add.added").param("soundEventId", soundEvent.getId()));
    }
    
    static {
        SOUND_EVENT_ASSET_TYPE = new AssetArgumentType<SoundEvent, Object>("server.commands.ambience.emitter.add.arg.soundEvent.name", SoundEvent.class, "server.commands.ambience.emitter.add.arg.soundEvent.usage");
    }
}
