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

package com.hypixel.hytale.server.core.modules.prefabspawner.commands;

import com.hypixel.hytale.server.core.prefab.PrefabWeights;
import com.hypixel.hytale.server.core.modules.prefabspawner.PrefabSpawnerState;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
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.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;

public class PrefabSpawnerSetCommand extends TargetPrefabSpawnerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_PREFAB_SPAWNER_SET;
    @Nonnull
    protected final RequiredArg<String> prefabPathArg;
    @Nonnull
    protected final OptionalArg<Boolean> fitHeightmapArg;
    @Nonnull
    protected final OptionalArg<Boolean> inheritSeedArg;
    @Nonnull
    protected final OptionalArg<Boolean> inheritHeightCheckArg;
    @Nonnull
    protected final OptionalArg<Double> defaultWeightArg;
    
    public PrefabSpawnerSetCommand() {
        super("set", "server.commands.prefabspawner.set.desc");
        this.prefabPathArg = this.withRequiredArg("prefab", "server.commands.prefabspawner.set.prefab.desc", ArgTypes.STRING);
        this.fitHeightmapArg = this.withOptionalArg("fitHeightmap", "server.commands.prefabspawner.set.fitHeightmap.desc", ArgTypes.BOOLEAN);
        this.inheritSeedArg = this.withOptionalArg("inheritSeed", "server.commands.prefabspawner.set.inheritSeed.desc", ArgTypes.BOOLEAN);
        this.inheritHeightCheckArg = this.withOptionalArg("inheritHeightCheck", "server.commands.prefabspawner.set.inheritHeightCheck.desc", ArgTypes.BOOLEAN);
        this.defaultWeightArg = this.withOptionalArg("defaultWeight", "server.commands.prefabspawner.set.defaultWeight.desc", ArgTypes.DOUBLE);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final WorldChunk chunk, @Nonnull final PrefabSpawnerState prefabSpawner) {
        final String prefabPath = this.prefabPathArg.get(context);
        prefabSpawner.setPrefabPath(prefabPath);
        if (this.fitHeightmapArg.provided(context)) {
            final boolean fitHeightmap = getOrDefault(this.fitHeightmapArg, context, true);
            prefabSpawner.setFitHeightmap(fitHeightmap);
        }
        if (this.inheritSeedArg.provided(context)) {
            final boolean inheritSeed = getOrDefault(this.inheritSeedArg, context, true);
            prefabSpawner.setInheritSeed(inheritSeed);
        }
        if (this.inheritHeightCheckArg.provided(context)) {
            final boolean inheritHeightCheck = getOrDefault(this.inheritHeightCheckArg, context, true);
            prefabSpawner.setInheritHeightCondition(inheritHeightCheck);
        }
        if (this.defaultWeightArg.provided(context)) {
            final double weight = this.defaultWeightArg.get(context);
            PrefabWeights prefabWeights = prefabSpawner.getPrefabWeights();
            if (prefabWeights == PrefabWeights.NONE) {
                prefabWeights = new PrefabWeights();
            }
            prefabWeights.setDefaultWeight(weight);
            prefabSpawner.setPrefabWeights(prefabWeights);
        }
        chunk.markNeedsSaving();
        context.sendMessage(PrefabSpawnerSetCommand.MESSAGE_COMMANDS_PREFAB_SPAWNER_SET);
    }
    
    protected static boolean getOrDefault(@Nonnull final OptionalArg<Boolean> arg, @Nonnull final CommandContext context, final boolean defaultValue) {
        if (!arg.provided(context)) {
            return defaultValue;
        }
        final Boolean value = arg.get(context);
        return (value != null) ? value : defaultValue;
    }
    
    static {
        MESSAGE_COMMANDS_PREFAB_SPAWNER_SET = Message.translation("server.commands.prefabspawner.set");
    }
}
