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

package com.hypixel.hytale.server.core.command.commands.player.effect;

import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.asset.type.entityeffect.config.OverlapBehavior;
import com.hypixel.hytale.server.core.entity.effect.EffectControllerComponent;
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.AbstractCommand;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.entityeffect.config.EntityEffect;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class PlayerEffectApplyCommand extends AbstractPlayerCommand
{
    private static final float DEFAULT_DURATION = 100.0f;
    @Nonnull
    private final RequiredArg<EntityEffect> effectArg;
    @Nonnull
    private final DefaultArg<Float> durationArg;
    
    public PlayerEffectApplyCommand() {
        super("apply", "server.commands.player.effect.apply.desc");
        this.effectArg = this.withRequiredArg("effect", "server.commands.player.effect.apply.effect.desc", ArgTypes.EFFECT_ASSET);
        this.durationArg = this.withDefaultArg("duration", "server.commands.player.effect.apply.duration.desc", ArgTypes.FLOAT, 100.0f, "server.commands.entity.effect.duration").addValidator((Validator<Float>)Validators.greaterThan((DataType)0.0f));
        this.requirePermission(HytalePermissions.fromCommand("player.effect.apply.self"));
        this.addUsageVariant(new PlayerEffectApplyOtherCommand());
    }
    
    @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 EffectControllerComponent effectControllerComponent = store.getComponent(ref, EffectControllerComponent.getComponentType());
        assert effectControllerComponent != null;
        final EntityEffect effect = this.effectArg.get(context);
        final Float duration = this.durationArg.get(context);
        effectControllerComponent.addEffect(ref, effect, duration, OverlapBehavior.OVERWRITE, store);
        context.sendMessage(Message.translation("server.commands.player.effect.apply.success.self").param("effect", effect.getId()).param("duration", duration));
    }
    
    private static class PlayerEffectApplyOtherCommand extends CommandBase
    {
        private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
        @Nonnull
        private final RequiredArg<PlayerRef> playerArg;
        @Nonnull
        private final RequiredArg<EntityEffect> effectArg;
        @Nonnull
        private final DefaultArg<Float> durationArg;
        static final /* synthetic */ boolean $assertionsDisabled;
        
        PlayerEffectApplyOtherCommand() {
            super("server.commands.player.effect.apply.other.desc");
            this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            this.effectArg = this.withRequiredArg("effect", "server.commands.player.effect.apply.effect.desc", ArgTypes.EFFECT_ASSET);
            this.durationArg = this.withDefaultArg("duration", "server.commands.player.effect.apply.duration.desc", ArgTypes.FLOAT, 100.0f, "server.commands.entity.effect.duration").addValidator((Validator<Float>)Validators.greaterThan((DataType)0.0f));
            this.requirePermission(HytalePermissions.fromCommand("player.effect.apply.other"));
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PlayerRef targetPlayerRef = this.playerArg.get(context);
            final Ref<EntityStore> ref = targetPlayerRef.getReference();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(PlayerEffectApplyOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return;
            }
            final Store<EntityStore> store = ref.getStore();
            final World world = store.getExternalData().getWorld();
            world.execute(() -> {
                final Player playerComponent = store.getComponent(ref, Player.getComponentType());
                if (playerComponent == null) {
                    context.sendMessage(PlayerEffectApplyOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                }
                else {
                    final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
                    if (!PlayerEffectApplyOtherCommand.$assertionsDisabled && playerRefComponent == null) {
                        throw new AssertionError();
                    }
                    else {
                        final EffectControllerComponent effectControllerComponent = store.getComponent(ref, EffectControllerComponent.getComponentType());
                        if (!PlayerEffectApplyOtherCommand.$assertionsDisabled && effectControllerComponent == null) {
                            throw new AssertionError();
                        }
                        else {
                            final EntityEffect effect = this.effectArg.get(context);
                            final Float duration = this.durationArg.get(context);
                            effectControllerComponent.addEffect(ref, effect, duration, OverlapBehavior.OVERWRITE, store);
                            context.sendMessage(Message.translation("server.commands.player.effect.apply.success.other").param("username", playerRefComponent.getUsername()).param("effect", effect.getId()).param("duration", duration));
                        }
                    }
                }
            });
        }
        
        static {
            MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        }
    }
}
