package com.walkertribe.ian.protocol.core;

import com.walkertribe.ian.enums.ConnectionType;
import com.walkertribe.ian.iface.PacketFactory;
import com.walkertribe.ian.iface.PacketFactoryRegistry;
import com.walkertribe.ian.iface.PacketReader;
import com.walkertribe.ian.iface.PacketWriter;
import com.walkertribe.ian.protocol.ArtemisPacket;
import com.walkertribe.ian.protocol.ArtemisPacketException;
import com.walkertribe.ian.protocol.BaseArtemisPacket;

/**
 * Enables/disables keystroke capture for this console. Note that the game
 * master console always captures keystrokes; all others don't by default unless
 * this packet enables it.
 * @author rjwut
 */
public class KeyCaptureTogglePacket extends BaseArtemisPacket {
	private static final int TYPE = 0xf754c8fe;
	private static final byte MSG_TYPE = 0x11;

	public static void register(PacketFactoryRegistry registry) {
		registry.register(ConnectionType.SERVER, TYPE, MSG_TYPE,
				new PacketFactory() {
			@Override
			public Class<? extends ArtemisPacket> getFactoryClass() {
				return KeyCaptureTogglePacket.class;
			}

			@Override
			public ArtemisPacket build(PacketReader reader)
					throws ArtemisPacketException {
				return new KeyCaptureTogglePacket(reader);
			}
		});
	}

	private boolean mEnabled;

	private KeyCaptureTogglePacket(PacketReader reader) {
		super(ConnectionType.SERVER, TYPE);
        reader.skip(4); // subtype
		mEnabled = reader.readInt() == 1;
	}

	public KeyCaptureTogglePacket(boolean enabled) {
		super(ConnectionType.SERVER, TYPE);
		mEnabled = enabled;
	}

	/**
	 * Returns true if this console should capture keystrokes; false otherwise.
	 */
	public boolean isEnabled() {
		return mEnabled;
	}

	@Override
	protected void writePayload(PacketWriter writer) {
		writer.writeByte(MSG_TYPE).writeInt(mEnabled ? 1 : 0);
	}

	@Override
	protected void appendPacketDetail(StringBuilder b) {
		b.append(mEnabled ? "ON" : "OFF");
	}
}
@@ -1425,19 +1425,20 @@
 der.read
-Int
+Byte
 () == 1;
@@ -1773,20 +1773,19 @@
 er.write
-Byte
+Int
 (MSG_TYP
@@ -1792,19 +1792,28 @@
 E).write
-Int
+Byte((byte) 
 (mEnable
@@ -1822,16 +1822,17 @@
 ? 1 : 0)
+)
 ;%0A%09%7D%0A%0A%09@

Fix for incorrect property sizes