package common.packet;

import java.io.IOException;
import common.network.IClientLoginHandler;
import common.network.Packet;
import common.network.PacketBuffer;

public class RPacketServerConfig implements Packet<IClientLoginHandler> {
	private boolean requiresAccess;
	private boolean requiresAuth;
	private boolean passwordAuth;
	private boolean pubkeyAuth;

	public RPacketServerConfig() {
	}

	public RPacketServerConfig(boolean requiresAccess, boolean requiresAuth, boolean passwordAuth, boolean pubkeyAuth) {
		this.requiresAccess = requiresAccess;
		this.requiresAuth = requiresAuth;
		this.passwordAuth = passwordAuth;
		this.pubkeyAuth = pubkeyAuth;
	}

	public final void readPacketData(PacketBuffer buf) throws IOException {
		byte flags = buf.readByte();
		this.requiresAccess = (flags & 1) != 0;
		this.requiresAuth = (flags & 2) != 0;
		this.passwordAuth = (flags & 4) != 0;
		this.pubkeyAuth = (flags & 8) != 0;
	}

	public final void writePacketData(PacketBuffer buf) throws IOException {
		byte flags = 0;
		flags |= this.requiresAccess ? 1 : 0;
		flags |= this.requiresAuth ? 2 : 0;
		flags |= this.passwordAuth ? 4 : 0;
		flags |= this.pubkeyAuth ? 8 : 0;
		buf.writeByte(flags);
	}

	public void processPacket(IClientLoginHandler handler) {
		handler.handleConfig(this);
	}

	public boolean hasAccessPassword() {
		return this.requiresAccess;
	}

	public boolean isAuthenticating() {
		return this.requiresAuth;
	}

	public boolean canUsePassword() {
		return this.passwordAuth;
	}

	public boolean canUsePubkey() {
		return this.pubkeyAuth;
	}
}
