//Main Read loop for a client. Also processes what is recieved.
func ClientReader(client *Client) {
	for {
		bytesRead, buffer := client.Read()
		dbgMsg.Printf("RAW RECIEVE: % X", buffer)

		if bytesRead < 10 {
			client.Outgoing <- []byte{0x00}
			client.Close()
			break
			//client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte(client.Room), []byte("Minimum header not recieved."))
			//client.Close()
		}

		//
		cmdBit, encryptionByte, ts, payload := protocolParser(buffer)
		dbgMsg.Printf("CMDBIT: % x, ENCBIT: % x, TS: % d, PAYLOAD: % x", cmdBit, encryptionByte, ts, payload)

		if encryptionByte == 0xAE {
			payload, _ = barrenoid.Decrypt(client.AES_Key, payload)
			dbgMsg.Printf("Decrypted Payload: % X", payload)
			//dbgMsg.Printf("%s\n", string(payload))
		} else if encryptionByte == 0x00 {
			dbgMsg.Println("Unencrypted packet.")
			// no need to decrypt
		} else {
			//bad packet reject
		}

		if cmdBit == 0x0D && encryptionByte == 0xAE {
			dbgMsg.Printf("TO SENDR %X", payload)
			client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte(client.Room), payload)
		} else if cmdBit == 0x0D && encryptionByte == 0x00 {
			client.Outgoing <- protocolPacker(0x0D, 0x00, []byte(client.Name), []byte(client.Room), payload)
		} else if cmdBit == 0x1C && encryptionByte == 0x00 {
			client.Name = string(payload)
			client.Outgoing <- protocolPacker(0x0D, 0x00, []byte(client.Name), []byte(client.Room), []byte("Changed Name"))
		} else if cmdBit == 0x1C && encryptionByte == 0xAE {
			client.Name = string(payload)
			client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte(client.Room), []byte("Changed Name"))
		} else {
			//bad packet reject
			client.Outgoing <- protocolPacker(0x0D, 0xAE, []byte(client.Name), []byte(client.Room), []byte("Unknown command bit."))
		}
	}

	//Client has disconnected, Make a new list and copy over good clients
	for i, c := range RoomList[client.Room].Clients {
		//TODO rename to client connections = ClientList
		if c.Conn == client.Conn {
			RoomList[client.Room].Clients[i] = RoomList[client.Room].Clients[len(RoomList[client.Room].Clients)-1]
			RoomList[client.Room].Clients = RoomList[client.Room].Clients[:len(RoomList[client.Room].Clients)-1]
		}
	}
	dbgMsg.Printf("Client reader done for %s", client.Name)
	client.Outgoing <- []byte(client.Name + " Disconnected.")
}
func Test_BarrenoidAes(t *testing.T) {
	testString := []byte("Test Encrypted Message 123")
	encryptedTestString, _ := barrenoid.Encrypt(AES_KEY, testString)
	//fmt.Printf("E: %X\n", encryptedTestString)
	decryptedPayload, _ := barrenoid.Decrypt(AES_KEY, encryptedTestString)
	//fmt.Printf("D: %X - %s\n", decryptedPayload, decryptedPayload)
	if string(testString) != string(decryptedPayload) {
		t.Fatalf("Incorrect payload: % s != % s", testString, decryptedPayload)
	}
}
func Test_ChangeNameEncrypted(t *testing.T) {
	testString := []byte("NewName123Encrypted")
	encryptedTestString, _ := barrenoid.Encrypt(AES_KEY, testString)
	CONN.Write(build(CHANGE_NAME, AES_ENCRYPTION, encryptedTestString))
	reply := readServer()
	cmdBitResp, encBitResp, tsResp, nameStringResp, roomStringResp, payloadResp := parseServerProtocol(reply)
	decryptedPayload, _ := barrenoid.Decrypt(AES_KEY, payloadResp)
	valid, msg := validateHeader(DATA, cmdBitResp, AES_ENCRYPTION, encBitResp, tsResp, string(testString), nameStringResp, "GeneralChat", roomStringResp)

	if valid != true {
		t.Fatalf(msg)
	}
	if string(decryptedPayload) != "Changed Name" {
		t.Fatalf("Incorrect payload: % X != % X", payloadResp, testString)
	}
}
func Test_EncryptedMessage(t *testing.T) {
	testString := []byte("Test Encrypted Message 123")
	encryptedTestString, _ := barrenoid.Encrypt(AES_KEY, testString)
	CONN.Write(build(DATA, AES_ENCRYPTION, encryptedTestString))
	reply := readServer()
	cmdBitResp, encBitResp, tsResp, nameStringResp, roomStringResp, payloadResp := parseServerProtocol(reply)
	decryptedPayload, _ := barrenoid.Decrypt(AES_KEY, payloadResp)
	valid, msg := validateHeader(DATA, cmdBitResp, AES_ENCRYPTION, encBitResp, tsResp, "Anonymous", nameStringResp, "GeneralChat", roomStringResp)

	if valid != true {
		t.Fatalf(msg)
	}
	if string(decryptedPayload) != string(testString) {
		t.Fatalf("Incorrect payload: % X != % X", decryptedPayload, testString)
	}
}