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

package com.hypixel.hytale.server.core.event.events.player;

import java.lang.invoke.CallSite;
import java.lang.reflect.UndeclaredThrowableException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.StringConcatFactory;
import java.lang.invoke.MethodType;
import java.lang.invoke.MethodHandles;
import java.util.logging.Level;
import com.hypixel.hytale.logger.HytaleLogger;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.hypixel.hytale.protocol.packets.auth.ClientReferral;
import com.hypixel.hytale.protocol.HostAddress;
import com.hypixel.hytale.server.core.auth.PlayerAuthentication;
import java.util.UUID;
import com.hypixel.hytale.server.core.io.PacketHandler;
import com.hypixel.hytale.event.ICancellable;
import com.hypixel.hytale.event.IEvent;

public class PlayerSetupConnectEvent implements IEvent<Void>, ICancellable
{
    public static final String DEFAULT_REASON = "You have been disconnected from the server!";
    private final PacketHandler packetHandler;
    private final String username;
    private final UUID uuid;
    private final PlayerAuthentication auth;
    private final byte[] referralData;
    private final HostAddress referralSource;
    private boolean cancelled;
    private String reason;
    private ClientReferral clientReferral;
    
    public PlayerSetupConnectEvent(final PacketHandler packetHandler, final String username, final UUID uuid, final PlayerAuthentication auth, final byte[] referralData, final HostAddress referralSource) {
        this.packetHandler = packetHandler;
        this.username = username;
        this.uuid = uuid;
        this.auth = auth;
        this.referralData = referralData;
        this.referralSource = referralSource;
        this.reason = "You have been disconnected from the server!";
        this.cancelled = false;
    }
    
    public PacketHandler getPacketHandler() {
        return this.packetHandler;
    }
    
    public UUID getUuid() {
        return this.uuid;
    }
    
    public String getUsername() {
        return this.username;
    }
    
    public PlayerAuthentication getAuth() {
        return this.auth;
    }
    
    @Nullable
    public byte[] getReferralData() {
        return this.referralData;
    }
    
    public boolean isReferralConnection() {
        return this.referralData != null && this.referralData.length > 0;
    }
    
    @Nullable
    public HostAddress getReferralSource() {
        return this.referralSource;
    }
    
    @Nullable
    public ClientReferral getClientReferral() {
        return this.clientReferral;
    }
    
    public void referToServer(@Nonnull final String host, final int port) {
        this.referToServer(host, port, null);
    }
    
    public void referToServer(@Nonnull final String host, final int port, @Nullable final byte[] data) {
        final int MAX_REFERRAL_DATA_SIZE = 4096;
        Objects.requireNonNull(host, "Host cannot be null");
        if (port <= 0 || port > 65535) {
            throw new IllegalArgumentException("Port must be between 1 and 65535");
        }
        if (data != null && data.length > 4096) {
            throw new IllegalArgumentException("Referral data exceeds maximum size of 4096 bytes (got " + data.length);
        }
        HytaleLogger.getLogger().at(Level.INFO).log("Referring player %s (%s) to %s:%d with %d bytes of data", this.username, this.uuid, host, port, (data != null) ? data.length : 0);
        this.clientReferral = new ClientReferral(new HostAddress(host, (short)port), data);
    }
    
    public String getReason() {
        return this.reason;
    }
    
    public void setReason(final String reason) {
        Objects.requireNonNull(reason, "Reason can't be null");
        this.reason = reason;
    }
    
    @Override
    public boolean isCancelled() {
        return this.cancelled;
    }
    
    @Override
    public void setCancelled(final boolean cancelled) {
        this.cancelled = cancelled;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "PlayerSetupConnectEvent{username='" + this.username + "', uuid=" + String.valueOf(this.uuid) + ", auth=" + String.valueOf(this.auth) + ", referralData=" + ((this.referralData != null) ? /* invokedynamic(!) */ProcyonInvokeDynamicHelper_5.invoke(this.referralData.length) : "null") + ", referralSource=" + ((this.referralSource != null) ? /* invokedynamic(!) */ProcyonInvokeDynamicHelper_6.invoke(this.referralSource.host, this.referralSource.port) : "null") + ", cancelled=" + this.cancelled + ", reason='" + this.reason + "'}";
    }
    
    // This helper class was generated by Procyon to approximate the behavior of an
    // 'invokedynamic' instruction that it doesn't know how to interpret.
    private static final class ProcyonInvokeDynamicHelper_5
    {
        private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
        private static MethodHandle handle;
        private static volatile int fence;
        
        private static MethodHandle handle() {
            final MethodHandle handle = ProcyonInvokeDynamicHelper_5.handle;
            if (handle != null)
                return handle;
            return ProcyonInvokeDynamicHelper_5.ensureHandle();
        }
        
        private static MethodHandle ensureHandle() {
            ProcyonInvokeDynamicHelper_5.fence = 0;
            MethodHandle handle = ProcyonInvokeDynamicHelper_5.handle;
            if (handle == null) {
                MethodHandles.Lookup lookup = ProcyonInvokeDynamicHelper_5.LOOKUP;
                try {
                    handle = ((CallSite)StringConcatFactory.makeConcatWithConstants(lookup, "makeConcatWithConstants", MethodType.methodType(String.class, int.class), "\u0001 bytes")).dynamicInvoker();
                }
                catch (Throwable t) {
                    throw new UndeclaredThrowableException(t);
                }
                ProcyonInvokeDynamicHelper_5.fence = 1;
                ProcyonInvokeDynamicHelper_5.handle = handle;
                ProcyonInvokeDynamicHelper_5.fence = 0;
            }
            return handle;
        }
        
        private static String invoke(int p0) {
            try {
                return ProcyonInvokeDynamicHelper_5.handle().invokeExact(p0);
            }
            catch (Throwable t) {
                throw new UndeclaredThrowableException(t);
            }
        }
    }
    
    // This helper class was generated by Procyon to approximate the behavior of an
    // 'invokedynamic' instruction that it doesn't know how to interpret.
    private static final class ProcyonInvokeDynamicHelper_6
    {
        private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
        private static MethodHandle handle;
        private static volatile int fence;
        
        private static MethodHandle handle() {
            final MethodHandle handle = ProcyonInvokeDynamicHelper_6.handle;
            if (handle != null)
                return handle;
            return ProcyonInvokeDynamicHelper_6.ensureHandle();
        }
        
        private static MethodHandle ensureHandle() {
            ProcyonInvokeDynamicHelper_6.fence = 0;
            MethodHandle handle = ProcyonInvokeDynamicHelper_6.handle;
            if (handle == null) {
                MethodHandles.Lookup lookup = ProcyonInvokeDynamicHelper_6.LOOKUP;
                try {
                    handle = ((CallSite)StringConcatFactory.makeConcatWithConstants(lookup, "makeConcatWithConstants", MethodType.methodType(String.class, String.class, short.class), "\u0001:\u0001")).dynamicInvoker();
                }
                catch (Throwable t) {
                    throw new UndeclaredThrowableException(t);
                }
                ProcyonInvokeDynamicHelper_6.fence = 1;
                ProcyonInvokeDynamicHelper_6.handle = handle;
                ProcyonInvokeDynamicHelper_6.fence = 0;
            }
            return handle;
        }
        
        private static String invoke(String p0, short p1) {
            try {
                return ProcyonInvokeDynamicHelper_6.handle().invokeExact(p0, p1);
            }
            catch (Throwable t) {
                throw new UndeclaredThrowableException(t);
            }
        }
    }
}
