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

package com.hypixel.hytale.server.core.plugin.commands;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.server.core.entity.entities.player.pages.CustomUIPage;
import com.hypixel.hytale.server.core.plugin.pages.PluginListPage;
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.basecommands.AbstractPlayerCommand;
import com.hypixel.hytale.server.core.plugin.PluginState;
import com.hypixel.hytale.server.core.HytaleServerConfig;
import com.hypixel.hytale.server.core.HytaleServer;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import java.util.Collection;
import com.hypixel.hytale.server.core.util.message.MessageFormat;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import com.hypixel.hytale.server.core.plugin.PluginBase;
import java.util.Set;
import com.hypixel.hytale.server.core.plugin.PluginManager;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.ParseResult;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import javax.annotation.Nonnull;
import com.hypixel.hytale.common.plugin.PluginIdentifier;
import com.hypixel.hytale.server.core.command.system.arguments.types.SingleArgumentType;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractCommandCollection;

public class PluginCommand extends AbstractCommandCollection
{
    @Nonnull
    private static final SingleArgumentType<PluginIdentifier> PLUGIN_IDENTIFIER_ARG_TYPE;
    
    public PluginCommand() {
        super("plugin", "server.commands.plugin.desc");
        this.addAliases("plugins", "pl");
        this.addSubCommand(new PluginListCommand());
        this.addSubCommand(new PluginLoadCommand());
        this.addSubCommand(new PluginUnloadCommand());
        this.addSubCommand(new PluginReloadCommand());
        this.addSubCommand(new PluginManageCommand());
    }
    
    static {
        PLUGIN_IDENTIFIER_ARG_TYPE = new SingleArgumentType<PluginIdentifier>("server.commands.parsing.argtype.pluginidentifier.usage", new String[0]) {
            @Nonnull
            @Override
            public PluginIdentifier parse(@Nonnull final String input, @Nonnull final ParseResult parseResult) {
                try {
                    return PluginIdentifier.fromString(input);
                }
                catch (final Exception e) {
                    parseResult.fail(Message.translation("server.commands.parsing.argtype.pluginidentifier.fail").param("input", input).param("error", e.getMessage()));
                    return null;
                }
            }
        };
    }
    
    private static class PluginListCommand extends CommandBase
    {
        public PluginListCommand() {
            super("list", "server.commands.plugin.list.desc");
            this.addAliases("ls");
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PluginManager module = PluginManager.get();
            final Set<Message> plugins = module.getPlugins().stream().map((Function<? super Object, ?>)PluginBase::getIdentifier).map((Function<? super Object, ?>)PluginIdentifier::toString).map((Function<? super Object, ?>)Message::raw).collect((Collector<? super Object, ?, Set<Message>>)Collectors.toSet());
            context.sendMessage(MessageFormat.list(Message.translation("server.commands.plugin.plugins"), plugins));
        }
    }
    
    private static class PluginLoadCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<PluginIdentifier> pluginNameArg;
        @Nonnull
        private final FlagArg bootFlag;
        
        public PluginLoadCommand() {
            super("load", "server.commands.plugin.load.desc");
            this.pluginNameArg = this.withRequiredArg("pluginName", "server.commands.plugin.load.pluginName.desc", PluginCommand.PLUGIN_IDENTIFIER_ARG_TYPE);
            this.bootFlag = this.withFlagArg("boot", "server.commands.plugin.load.boot.desc");
            this.addAliases("l");
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PluginManager module = PluginManager.get();
            final PluginIdentifier identifier = this.pluginNameArg.get(context);
            final PluginBase plugin = module.getPlugin(identifier);
            if (identifier != null) {
                final boolean onlyBootList = ((Argument<Arg, Boolean>)this.bootFlag).get(context);
                final HytaleServerConfig serverConfig = HytaleServer.get().getConfig();
                HytaleServerConfig.ModConfig.setBoot(serverConfig, identifier, true);
                if (serverConfig.consumeHasChanged()) {
                    HytaleServerConfig.save(serverConfig).join();
                }
                context.sendMessage(Message.translation("server.commands.plugin.bootListEnabled").param("id", identifier.toString()));
                if (onlyBootList) {
                    return;
                }
            }
            if (plugin == null || plugin.getState() == PluginState.DISABLED) {
                context.sendMessage(Message.translation("server.commands.plugin.pluginLoading").param("id", identifier.toString()));
                if (module.load(identifier)) {
                    context.sendMessage(Message.translation("server.commands.plugin.pluginLoaded").param("id", identifier.toString()));
                }
                else {
                    context.sendMessage(Message.translation("server.commands.plugin.failedToLoadPlugin").param("id", identifier.toString()));
                }
            }
            else {
                assert identifier != null;
                switch (plugin.getState()) {
                    case NONE: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToLoadInvalidState").param("id", identifier.toString()));
                        break;
                    }
                    case SETUP: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToLoadSetup").param("id", identifier.toString()));
                        break;
                    }
                    case START: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToLoadStarted").param("id", identifier.toString()));
                        break;
                    }
                    case ENABLED: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToLoadAlreadyEnabled").param("id", identifier.toString()));
                        break;
                    }
                    default: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedPluginState").param("state", plugin.getState().toString()));
                        break;
                    }
                }
            }
        }
    }
    
    private static class PluginUnloadCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<PluginIdentifier> pluginNameArg;
        @Nonnull
        private final FlagArg bootFlag;
        
        public PluginUnloadCommand() {
            super("unload", "server.commands.plugin.unload.desc");
            this.pluginNameArg = this.withRequiredArg("pluginName", "server.commands.plugin.unload.pluginName.desc", PluginCommand.PLUGIN_IDENTIFIER_ARG_TYPE);
            this.bootFlag = this.withFlagArg("boot", "server.commands.plugin.unload.boot.desc");
            this.addAliases("u");
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PluginManager module = PluginManager.get();
            final PluginIdentifier identifier = this.pluginNameArg.get(context);
            final PluginBase plugin = module.getPlugin(identifier);
            if (identifier != null) {
                final boolean onlyBootList = ((Argument<Arg, Boolean>)this.bootFlag).get(context);
                final HytaleServerConfig serverConfig = HytaleServer.get().getConfig();
                HytaleServerConfig.ModConfig.setBoot(serverConfig, identifier, false);
                if (serverConfig.consumeHasChanged()) {
                    HytaleServerConfig.save(serverConfig).join();
                }
                context.sendMessage(Message.translation("server.commands.plugin.bootListDisabled").param("id", identifier.toString()));
                if (onlyBootList) {
                    return;
                }
            }
            if (plugin != null) {
                switch (plugin.getState()) {
                    case NONE: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToUnloadState").param("id", identifier.toString()));
                        break;
                    }
                    case SETUP: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToUnloadSetup").param("id", identifier.toString()));
                        break;
                    }
                    case START: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToUnloadStarted").param("id", identifier.toString()));
                        break;
                    }
                    case ENABLED: {
                        context.sendMessage(Message.translation("server.commands.plugin.pluginUnloading").param("id", identifier.toString()));
                        if (module.unload(identifier)) {
                            context.sendMessage(Message.translation("server.commands.plugin.pluginUnloaded").param("id", identifier.toString()));
                            break;
                        }
                        context.sendMessage(Message.translation("server.commands.plugin.failedToUnload").param("id", identifier.toString()));
                        break;
                    }
                    case DISABLED: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToUnloadDisabled").param("id", identifier.toString()));
                        break;
                    }
                    default: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedPluginState").param("state", plugin.getState().toString()));
                        break;
                    }
                }
            }
            else {
                context.sendMessage(Message.translation("server.commands.plugin.notLoaded").param("id", identifier.toString()));
            }
        }
    }
    
    private static class PluginReloadCommand extends CommandBase
    {
        @Nonnull
        private final RequiredArg<PluginIdentifier> pluginNameArg;
        
        public PluginReloadCommand() {
            super("reload", "server.commands.plugin.reload.desc");
            this.pluginNameArg = this.withRequiredArg("pluginName", "server.commands.plugin.reload.pluginName.desc", PluginCommand.PLUGIN_IDENTIFIER_ARG_TYPE);
            this.addAliases("r");
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PluginManager module = PluginManager.get();
            final PluginIdentifier identifier = this.pluginNameArg.get(context);
            final PluginBase plugin = module.getPlugin(identifier);
            if (plugin != null) {
                switch (plugin.getState()) {
                    case NONE: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToReloadState").param("id", identifier.toString()));
                        break;
                    }
                    case SETUP: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToReloadSetup").param("id", identifier.toString()));
                        break;
                    }
                    case START: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToReloadStarted").param("id", identifier.toString()));
                        break;
                    }
                    case ENABLED: {
                        if (module.reload(identifier)) {
                            context.sendMessage(Message.translation("server.commands.plugin.pluginReloaded").param("id", identifier.toString()));
                            break;
                        }
                        context.sendMessage(Message.translation("server.commands.plugin.failedToReload").param("id", identifier.toString()));
                        break;
                    }
                    case DISABLED: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedToReloadDisabled").param("id", identifier.toString()));
                        break;
                    }
                    default: {
                        context.sendMessage(Message.translation("server.commands.plugin.failedPluginState").param("state", plugin.getState().toString()));
                        break;
                    }
                }
            }
            else {
                context.sendMessage(Message.translation("server.commands.plugin.notLoaded").param("id", identifier.toString()));
            }
        }
    }
    
    private static class PluginManageCommand extends AbstractPlayerCommand
    {
        public PluginManageCommand() {
            super("manage", "server.commands.plugin.manage.desc");
            this.addAliases("m");
        }
        
        @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;
            playerComponent.getPageManager().openCustomPage(ref, store, new PluginListPage(playerRef));
        }
    }
}
