/*
 * 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 "rest.h"
#include "database.h"

#include "signup.h"


void clientApiSignup(ClientThreadT *client, PacketDecodeDataT *data) {
	json_object             *response     = NULL;
	char                    *user         = NULL;
	char                    *pass         = NULL;
	char                    *first        = NULL;
	char                    *last         = NULL;
	char                    *email        = NULL;
	uint8_t                  result       = 0;
	char                    *string       = NULL;
	char                    *buffer       = NULL;
	char                    *packetData   = NULL;
	PacketEncodeDataT        encoded      = { 0 };
	uint16_t                 length       = 0;

	// This only makes sense if they're not logged in.
	if (!client->authenticated) {

		packetDebugPrint((uint8_t *)data->data, data->length);

		// Get info for the user.
		packetContentUnpack(data->data, "sssss", &email, &first, &last, &pass, &user);

		logWrite("Email:  %s\n\r", email);
		logWrite("First:  %s\n\r", first);
		logWrite(" Last:  %s\n\r", last);
		logWrite(" Pass:  %s\n\r", pass);
		logWrite(" User:  %s\n\r", user);

		// See if they already exist in the database.
		if (dbUserNameExists(user)) {
			// User name already exists.
			result = 0;
			buffer = "User name already exists.";
		} else {
			if (dbUserEmailExists(email)) {
				// Email already exists.
				result = 0;
				buffer = "E-mail already exists.";
			} else {
				// Create user in DB.
				if (dbUserCreate(first, last, user, pass, email) == FAIL) {
					result = 0;
					buffer = "Unknown error.  Sorry.";
				} else {
					// If we found a REST endpoint, create the user there as well.
					if (__restAvailable) {
						response = restRequest("USER_CREATE", "sssss",
										"first", first,
										"last",  last,
										"user",  user,
										"pass",  pass,
										"email", email
									);
						if (response) {
							// Success with REST.
							string = (char *)json_object_get_string(json_object_object_get(response, "result"));
							result = (string[0] == 't' || string[0] == 'T') ? 1 : 0;
							buffer = (char *)json_object_get_string(json_object_object_get(response, "reason"));
						} else {
							// Something bad happened.
							result = 0;
							buffer = "Unknown error.  Sorry.";
						}
					} else {
						// Success with DB only!
						result = 1;
						buffer = "Check your E-mail for activation instructions.";
					}
				}
			}
		}

		DEL(first);
		DEL(last);
		DEL(user);
		DEL(pass);
		DEL(email);
	}

	packetData = packetContentPack(&length, "is", result, buffer);
	if (response) restRelease(response);
	// Build packet.
	encoded.control    = PACKET_CONTROL_DAT;
	encoded.packetType = PACKET_TYPE_SIGNUP_RESULT;
	encoded.channel    = data->channel;
	encoded.encrypt    = 0;
	packetEncode(client->packetThreadData, &encoded, packetData, length);
	// Send it.
	packetSend(client->packetThreadData, &encoded);
	DEL(packetData);
}
