/*
 * Kangaroo Punch MultiPlayer Game Server Mark II
 * Copyright (C) 2020-2021  Scott Duensing
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */


#include "database.h"
#include "console.h"
#include "array.h"

#include "version.h"


void clientApiVersionBad(ClientThreadT *client, PacketDecodeDataT *data) {
	// ***TODO***
}


void clientApiVersionOkay(ClientThreadT *client, PacketDecodeDataT *data) {
	uint64_t                 x            = 0;
	uint32_t                 y            = 0;
	uint16_t                 length       = 0;
	char                    *buffer       = NULL;
	PacketEncodeDataT        encoded      = { 0 };
	DBTableT                *record       = NULL;
	DBTableT               **table        = NULL;

	(void)data;

	// Fetch string table from DB.
	if (dbTableGet("strings", &table) == SUCCESS) {
		while (arrlen(table)) {
			record = table[0];
			arrdel(table, 0);
			// Strings are encoded in a single buffer as:  KEY\0DATA\0
			x = strlen(record->name);
			y = strlen(record->data);
			length = x + y + 2;
			buffer = (char *)malloc(length);
			if (!buffer) {
				consoleMessageQueue("%ld: Unable to allocate buffer for string packet!\n", client->threadIndex);
				break;
			}
			memcpy(buffer, record->name, x + 1);
			memcpy(&buffer[x + 1], record->data, y + 1);
			// Build packet.
			encoded.control    = PACKET_CONTROL_DAT;
			encoded.packetType = PACKET_TYPE_STRING;
			encoded.channel    = data->channel;
			encoded.encrypt    = 0;
			packetEncode(client->packetThreadData, &encoded, buffer, length);
			// Send it.
			packetSend(client->packetThreadData, &encoded);
			DEL(buffer);
			//consoleMessageQueue("PACKET_TYPE_STRING [%s] sent.\n", record->name);
			DEL(record->name);
			DEL(record->data);
			DEL(record);
		}
		arrfree(table);
	} else {
		consoleMessageQueue("%ld: Unable to fetch strings!\n", client->threadIndex);
		return;
	}

	// Fetch number table from DB.
	if (dbTableGet("numbers", &table) == SUCCESS) {
		while (arrlen(table)) {
			record = table[0];
			arrdel(table, 0);
			// Integers are encoded in a single buffer as:  1234DATA\0
			// Integers are 64 bit until sent to the client when they are truncated to 32.
			x = strlen(record->name);
			y = atol(record->data);
			length = x + 5;
			buffer = (char *)malloc(length);
			if (!buffer) {
				consoleMessageQueue("%ld: Unable to allocate buffer for number packet!\n", client->threadIndex);
				break;
			}
			memcpy(buffer, &y, 4);
			memcpy(&buffer[4], record->name, x + 1);
			// Build packet.
			encoded.control    = PACKET_CONTROL_DAT;
			encoded.packetType = PACKET_TYPE_NUMBER;
			encoded.channel    = data->channel;
			encoded.encrypt    = 0;
			packetEncode(client->packetThreadData, &encoded, buffer, length);
			// Send it.
			packetSend(client->packetThreadData, &encoded);
			DEL(buffer);
			//consoleMessageQueue("PACKET_TYPE_NUMBER [%s] sent.\n", record->name);
			DEL(record->name);
			DEL(record->data);
			DEL(record);
		}
		arrfree(table);
	} else {
		consoleMessageQueue("%ld: Unable to fetch numbers!\n", client->threadIndex);
		return;
	}

	// Build PROCEED packet.
	encoded.control    = PACKET_CONTROL_DAT;
	encoded.packetType = PACKET_TYPE_PROCEED;
	encoded.channel    = data->channel;
	encoded.encrypt    = 0;
	packetEncode(client->packetThreadData, &encoded, NULL, 0);
	// Send it.
	packetSend(client->packetThreadData, &encoded);
	consoleMessageQueue("PACKET_TYPE_PROCEED sent.\n");
}
