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

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

import java.util.Set;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import java.awt.Color;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.builtin.crafting.CraftingPlugin;
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.types.ArgTypes;
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;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractCommandCollection;

public class RecipeCommand extends AbstractCommandCollection
{
    public RecipeCommand() {
        super("recipe", "server.commands.recipe.desc");
        this.addSubCommand(new Learn());
        this.addSubCommand(new Forget());
        this.addSubCommand(new List());
    }
    
    static class Learn extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<Item> itemArg;
        
        Learn() {
            super("learn", "server.commands.recipe.learn.desc");
            this.itemArg = this.withRequiredArg("item", "server.commands.recipe.learn.item.desc", ArgTypes.ITEM_ASSET);
            this.addUsageVariant(new LearnOther());
        }
        
        @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 Item item = this.itemArg.get(context);
            final String itemId = item.getId();
            final Message itemMessage = Message.translation(item.getTranslationKey());
            if (CraftingPlugin.learnRecipe(ref, itemId, store)) {
                context.sendMessage(Message.translation("server.modules.learnrecipe.success").param("name", itemMessage).color(Color.GREEN));
            }
            else {
                context.sendMessage(Message.translation("server.modules.learnrecipe.alreadyKnown").param("name", itemMessage).color(Color.RED));
            }
        }
        
        private static class LearnOther extends CommandBase
        {
            @Nonnull
            private final RequiredArg<Item> itemArg;
            @Nonnull
            private final RequiredArg<PlayerRef> playerArg;
            
            LearnOther() {
                super("server.commands.recipe.learn.other.desc");
                this.itemArg = this.withRequiredArg("item", "server.commands.recipe.learn.item.desc", ArgTypes.ITEM_ASSET);
                this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            }
            
            @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(Message.translation("server.commands.errors.playerNotInWorld"));
                    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(Message.translation("server.commands.errors.playerNotInWorld"));
                    }
                    else {
                        final Item item = this.itemArg.get(context);
                        final String itemId = item.getId();
                        final Message itemMessage = Message.translation(item.getTranslationKey());
                        if (CraftingPlugin.learnRecipe(ref, itemId, store)) {
                            context.sendMessage(Message.translation("server.commands.recipe.learn.success.other").param("username", targetPlayerRef.getUsername()).param("name", itemMessage).color(Color.GREEN));
                        }
                        else {
                            context.sendMessage(Message.translation("server.commands.recipe.learn.alreadyKnown.other").param("username", targetPlayerRef.getUsername()).param("name", itemMessage).color(Color.RED));
                        }
                    }
                });
            }
        }
    }
    
    static class Forget extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<Item> itemArg;
        
        Forget() {
            super("forget", "server.commands.recipe.forget.desc");
            this.itemArg = this.withRequiredArg("item", "server.commands.recipe.forget.item.desc", ArgTypes.ITEM_ASSET);
            this.addUsageVariant(new ForgetOther());
        }
        
        @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 Item item = this.itemArg.get(context);
            final String itemId = item.getId();
            if (CraftingPlugin.forgetRecipe(ref, itemId, store)) {
                context.sendMessage(Message.translation("server.commands.recipe.forgotten").param("id", itemId).color(Color.GREEN));
            }
            else {
                context.sendMessage(Message.translation("server.commands.recipe.alreadyNotKnown").param("id", itemId).color(Color.RED));
            }
        }
        
        private static class ForgetOther extends CommandBase
        {
            @Nonnull
            private final RequiredArg<Item> itemArg;
            @Nonnull
            private final RequiredArg<PlayerRef> playerArg;
            
            ForgetOther() {
                super("server.commands.recipe.forget.other.desc");
                this.itemArg = this.withRequiredArg("item", "server.commands.recipe.forget.item.desc", ArgTypes.ITEM_ASSET);
                this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            }
            
            @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(Message.translation("server.commands.errors.playerNotInWorld"));
                    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(Message.translation("server.commands.errors.playerNotInWorld"));
                    }
                    else {
                        final Item item = this.itemArg.get(context);
                        final String itemId = item.getId();
                        if (CraftingPlugin.forgetRecipe(ref, itemId, store)) {
                            context.sendMessage(Message.translation("server.commands.recipe.forgotten.other").param("username", targetPlayerRef.getUsername()).param("id", itemId).color(Color.GREEN));
                        }
                        else {
                            context.sendMessage(Message.translation("server.commands.recipe.alreadyNotKnown.other").param("username", targetPlayerRef.getUsername()).param("id", itemId).color(Color.RED));
                        }
                    }
                });
            }
        }
    }
    
    static class List extends AbstractPlayerCommand
    {
        List() {
            super("list", "server.commands.recipe.list.desc");
            this.addUsageVariant(new ListOther());
        }
        
        @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 Set<String> knownRecipes = playerComponent.getPlayerConfigData().getKnownRecipes();
            context.sendMessage(Message.translation("server.commands.recipe.knownRecipes").param("list", knownRecipes.toString()));
        }
        
        private static class ListOther extends CommandBase
        {
            @Nonnull
            private final RequiredArg<PlayerRef> playerArg;
            
            ListOther() {
                super("server.commands.recipe.list.other.desc");
                this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            }
            
            @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(Message.translation("server.commands.errors.playerNotInWorld"));
                    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(Message.translation("server.commands.errors.playerNotInWorld"));
                    }
                    else {
                        final Set<String> knownRecipes = playerComponent.getPlayerConfigData().getKnownRecipes();
                        context.sendMessage(Message.translation("server.commands.recipe.knownRecipes.other").param("username", targetPlayerRef.getUsername()).param("list", knownRecipes.toString()));
                    }
                });
            }
        }
    }
}
