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

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

import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.permissions.PermissionHolder;
import com.hypixel.hytale.server.core.command.system.CommandUtil;
import com.hypixel.hytale.server.core.permissions.HytalePermissions;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
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.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractTargetPlayerCommand;

public class ReferCommand extends AbstractTargetPlayerCommand
{
    @Nonnull
    private final RequiredArg<String> hostArg;
    @Nonnull
    private final RequiredArg<Integer> portArg;
    
    public ReferCommand() {
        super("refer", "Refer a player to another server for testing");
        this.hostArg = this.withRequiredArg("host", "Target server hostname or IP", ArgTypes.STRING);
        this.portArg = this.withRequiredArg("port", "Target server port", ArgTypes.INTEGER);
        this.addAliases("transfer");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nullable final Ref<EntityStore> sourceRef, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final String host = this.hostArg.get(context);
        final int port = this.portArg.get(context);
        final boolean isTargetingOther = !ref.equals(sourceRef);
        if (isTargetingOther) {
            CommandUtil.requirePermission(context.sender(), HytalePermissions.fromCommand("refer.other"));
        }
        else {
            CommandUtil.requirePermission(context.sender(), HytalePermissions.fromCommand("refer.self"));
        }
        if (port <= 0 || port > 65535) {
            context.sendMessage(Message.translation("server.commands.refer.invalidPort"));
            return;
        }
        try {
            playerRef.referToServer(host, port);
            if (isTargetingOther) {
                context.sendMessage(Message.translation("server.commands.refer.success.other").param("username", playerRef.getUsername()).param("host", host).param("port", port));
            }
            else {
                context.sendMessage(Message.translation("server.commands.refer.success.self").param("host", host).param("port", port));
            }
        }
        catch (final Exception e) {
            context.sendMessage(Message.translation("server.commands.refer.failed").param("error", e.getMessage()));
        }
    }
}
