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

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

import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import com.hypixel.hytale.server.core.inventory.transaction.ItemStackTransaction;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.Message;
import org.bson.BsonDocument;
import com.hypixel.hytale.server.core.entity.entities.Player;
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.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.DefaultArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.item.config.Item;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class GiveCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final RequiredArg<Item> itemArg;
    @Nonnull
    private final DefaultArg<Integer> quantityArg;
    @Nonnull
    private final OptionalArg<Double> durabilityArg;
    @Nonnull
    private final OptionalArg<String> metadataArg;
    
    public GiveCommand() {
        super("give", "server.commands.give.desc");
        this.itemArg = this.withRequiredArg("item", "server.commands.give.item.desc", ArgTypes.ITEM_ASSET);
        this.quantityArg = this.withDefaultArg("quantity", "server.commands.give.quantity.desc", ArgTypes.INTEGER, 1, "1");
        this.durabilityArg = this.withOptionalArg("durability", "server.commands.give.durability.desc", ArgTypes.DOUBLE);
        this.metadataArg = this.withOptionalArg("metadata", "server.commands.give.metadata.desc", ArgTypes.STRING);
        this.requirePermission(HytalePermissions.fromCommand("give.self"));
        this.addUsageVariant(new GiveOtherCommand());
        this.addSubCommand(new GiveArmorCommand());
    }
    
    @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 Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final Item item = this.itemArg.get(context);
        final Integer quantity = this.quantityArg.get(context);
        double durability = Double.MAX_VALUE;
        if (this.durabilityArg.provided(context)) {
            durability = this.durabilityArg.get(context);
        }
        BsonDocument metadata = null;
        if (this.metadataArg.provided(context)) {
            final String metadataStr = this.metadataArg.get(context);
            try {
                metadata = BsonDocument.parse(metadataStr);
            }
            catch (final Exception e) {
                context.sendMessage(Message.translation("server.commands.give.invalidMetadata").param("error", e.getMessage()));
                return;
            }
        }
        final ItemStack stack = new ItemStack(item.getId(), quantity, metadata).withDurability(durability);
        final ItemStackTransaction transaction = playerComponent.getInventory().getCombinedHotbarFirst().addItemStack(stack);
        final ItemStack remainder = transaction.getRemainder();
        final Message itemNameMessage = Message.translation(item.getTranslationKey());
        if (remainder == null || remainder.isEmpty()) {
            context.sendMessage(Message.translation("server.commands.give.received").param("quantity", quantity).param("item", itemNameMessage));
        }
        else {
            context.sendMessage(Message.translation("server.commands.give.insufficientInvSpace").param("quantity", quantity).param("item", itemNameMessage));
        }
    }
    
    private static class GiveOtherCommand extends CommandBase
    {
        @Nonnull
        private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
        @Nonnull
        private final RequiredArg<PlayerRef> playerArg;
        @Nonnull
        private final RequiredArg<Item> itemArg;
        @Nonnull
        private final DefaultArg<Integer> quantityArg;
        @Nonnull
        private final OptionalArg<Double> durabilityArg;
        @Nonnull
        private final OptionalArg<String> metadataArg;
        static final /* synthetic */ boolean $assertionsDisabled;
        
        GiveOtherCommand() {
            super("server.commands.give.other.desc");
            this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            this.itemArg = this.withRequiredArg("item", "server.commands.give.item.desc", ArgTypes.ITEM_ASSET);
            this.quantityArg = this.withDefaultArg("quantity", "server.commands.give.quantity.desc", ArgTypes.INTEGER, 1, "1");
            this.durabilityArg = this.withOptionalArg("durability", "server.commands.give.durability.desc", ArgTypes.DOUBLE);
            this.metadataArg = this.withOptionalArg("metadata", "server.commands.give.metadata.desc", ArgTypes.STRING);
            this.requirePermission(HytalePermissions.fromCommand("give.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(GiveOtherCommand.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(GiveOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                }
                else {
                    final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
                    if (!GiveOtherCommand.$assertionsDisabled && playerRefComponent == null) {
                        throw new AssertionError();
                    }
                    else {
                        final Item item = this.itemArg.get(context);
                        final Integer quantity = this.quantityArg.get(context);
                        double durability = Double.MAX_VALUE;
                        if (this.durabilityArg.provided(context)) {
                            durability = this.durabilityArg.get(context);
                        }
                        BsonDocument metadata = null;
                        if (this.metadataArg.provided(context)) {
                            final String metadataStr = this.metadataArg.get(context);
                            try {
                                metadata = BsonDocument.parse(metadataStr);
                            }
                            catch (final Exception e) {
                                context.sendMessage(Message.translation("server.commands.give.invalidMetadata").param("error", e.getMessage()));
                                return;
                            }
                        }
                        final ItemStack stack = new ItemStack(item.getId(), quantity, metadata).withDurability(durability);
                        final ItemStackTransaction transaction = playerComponent.getInventory().getCombinedHotbarFirst().addItemStack(stack);
                        final ItemStack remainder = transaction.getRemainder();
                        final Message itemNameMessage = Message.translation(item.getTranslationKey());
                        if (remainder == null || remainder.isEmpty()) {
                            context.sendMessage(Message.translation("server.commands.give.gave").param("targetUsername", targetPlayerRef.getUsername()).param("quantity", quantity).param("item", itemNameMessage));
                        }
                        else {
                            context.sendMessage(Message.translation("server.commands.give.insufficientInvSpace").param("quantity", quantity).param("item", itemNameMessage));
                        }
                    }
                }
            });
        }
        
        static {
            MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        }
    }
}
