/*
 * 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 "network.h"
#include "vesa.h"
#include "db.h"

#include "taglist.h"
#include "msgbox.h"
#include "window.h"
#include "picture.h"
#include "button.h"

#include "menu.h"
#include "hangup.h"
#include "welcome.h"
#include "file.h"


static void btnMsgBoxLogoff(MsgBoxButtonT button);
static void btnMsgBoxOkay(MsgBoxButtonT button);
static void menuEnable(uint8_t enable);
static void menuFilesReady(char *updatedFile);
static void picChatClick(WidgetT *widget);
static void picEmailClick(WidgetT *widget);
static void picForumsClick(WidgetT *widget);
static void picGamesClick(WidgetT *widget);
static void picLogoffClick(WidgetT *widget);
static void picProfileClick(WidgetT *widget);
static void updateGameDatabase(void);


static PictureT *_picChat     = NULL;
static PictureT *_picEmail    = NULL;
static PictureT *_picForums   = NULL;
static PictureT *_picGames    = NULL;
static PictureT *_picLogoff   = NULL;
static PictureT *_picProfile  = NULL;


static void btnMsgBoxLogoff(MsgBoxButtonT button) {

	if (button == MSGBOX_BUTTON_ONE) {
		guiDelete(D(_picProfile));
		guiDelete(D(_picLogoff));
		guiDelete(D(_picGames));
		guiDelete(D(_picForums));
		guiDelete(D(_picEmail));
		guiDelete(D(_picChat));
		// ***TODO*** Need a way to close all other windows.
		hangupShow(welcomeShow);
	}

	menuEnable(1);
}


static void btnMsgBoxOkay(MsgBoxButtonT button) {
	(void)(button);

	menuEnable(1);
}


static void menuEnable(uint8_t enable) {
	/*
	widgetEnableSet(W(_picProfile), enable);
	widgetEnableSet(W(_picLogoff),  enable);
	widgetEnableSet(W(_picGames),   enable);
	widgetEnableSet(W(_picForums),  enable);
	widgetEnableSet(W(_picEmail),   enable);
	widgetEnableSet(W(_picChat),    enable);
	*/
}


static void menuFilesReady(char *updatedFile) {
	uint16_t  x;
	uint16_t  y;

	BEGIN

	// Did a file get updated or are we done?
	if (updatedFile) {
		// Was this the game database?
		if (strcmp(updatedFile, "generated:games.dat") == 0) {
			// Load changes into database.
			updateGameDatabase();
		}
	} else {
		// Show UI
		x = vbeDisplayWidthGet()  - 49;
		y = vbeDisplayHeightGet() - 49;

		_picLogoff = pictureNew(x, y, "menu:48logoff.png");
		pictureClickHandlerSet(_picLogoff, picLogoffClick);
		guiAttach(guiRootGet(), W(_picLogoff));
		x -= 49;

		_picForums = pictureNew(x, y, "menu:48forums.png");
		pictureClickHandlerSet(_picForums, picForumsClick);
		guiAttach(guiRootGet(), W(_picForums));
		x -= 49;

		_picEmail = pictureNew(x, y, "menu:48email.png");
		pictureClickHandlerSet(_picEmail, picEmailClick);
		guiAttach(guiRootGet(), W(_picEmail));
		x -= 49;

		_picGames = pictureNew(x, y, "menu:48games.png");
		pictureClickHandlerSet(_picGames, picGamesClick);
		guiAttach(guiRootGet(), W(_picGames));
		x -= 49;

		_picChat = pictureNew(x, y, "menu:48chat.png");
		pictureClickHandlerSet(_picChat, picChatClick);
		guiAttach(guiRootGet(), W(_picChat));
		x -= 49;

		_picProfile = pictureNew(x, y, "menu:48profile.png");
		pictureClickHandlerSet(_picProfile, picProfileClick);
		guiAttach(guiRootGet(), W(_picProfile));
	}

	END
}


void menuShow(void) {
	char *fileList[] = {
		"menu:48chat.png",
		"menu:48email.png",
		"menu:48forums.png",
		"menu:48games.png",
		"menu:48logoff.png",
		"menu:48profile.png",
		"generated:games.dat",
		NULL
	};

	BEGIN

	fileCacheCheck(menuFilesReady, fileList);

	END
}


static void picChatClick(WidgetT *widget) {
	(void)widget;
	// ***TODO***
	menuEnable(0);
	msgBoxOne("Chat", MSGBOX_ICON_MESSAGE, "Yeah, this doesn't do anything yet.", "Okay", btnMsgBoxOkay);
}


static void picEmailClick(WidgetT *widget) {
	(void)widget;
	// ***TODO***
	menuEnable(0);
	msgBoxOne("E-Mail", MSGBOX_ICON_MESSAGE, "Yeah, this doesn't do anything yet.", "Okay", btnMsgBoxOkay);
}


static void picForumsClick(WidgetT *widget) {
	(void)widget;
	// ***TODO***
	menuEnable(0);
	msgBoxOne("Forums", MSGBOX_ICON_MESSAGE, "Yeah, this doesn't do anything yet.", "Okay", btnMsgBoxOkay);
}


static void picGamesClick(WidgetT *widget) {
	(void)widget;
	// ***TODO***
	menuEnable(0);
	msgBoxOne("Games", MSGBOX_ICON_MESSAGE, "Yeah, this doesn't do anything yet.", "Okay", btnMsgBoxOkay);
}


static void picLogoffClick(WidgetT *widget) {
	(void)widget;
	menuEnable(0);
	msgBoxTwo("Logoff?", MSGBOX_ICON_QUESTION, "This will disconnect you from the server.", "Okay", btnMsgBoxLogoff, "Cancel", btnMsgBoxLogoff);
}


static void picProfileClick(WidgetT *widget) {
	(void)widget;
	// ***TODO***
	menuEnable(0);
	msgBoxOne("Options", MSGBOX_ICON_MESSAGE, "Yeah, this doesn't do anything yet.", "Okay", btnMsgBoxOkay);
}


static void updateGameDatabase(void) {
	FILE     *f = NULL;
	uint8_t   c;

	// Do we need to create the game database?
	dbExecute(
		"CREATE TABLE IF NOT EXISTS games ("
		"title        VARCHAR(255), "
		"publisher    VARCHAR(255), "
		"developer    VARCHAR(255), "
		"description  TEXT, "
		"releaseDate  VARCHAR(255), "
		"rating       VARCHAR(255), "
		"series       VARCHAR(255), "
		"origin       VARCHAR(255), "
		"shortName    VARCHAR(8)    PRIMARY KEY, "
		"worksWith    VARCHAR(255), "
		"type         VARCHAR(8), "
		"maxPlayers   INTEGER, "
		"joinable     BOOLEAN, "
		"screens      INTEGER, "
		"boxes        INTEGER, "
		"touched      BOOLEAN "
		")",
	NULL);

	// Mark everything untouched.
	dbExecute("UPDATE games SET touced=0", NULL);

	// Process latest downloaded game list.
	f = cacheFOpen("generated:games.dat", "rb");
	if (f) {
		while (1) {
			// Get next byte.
			c = fgetc(f);
			// End of file?
			if (feof(f)) break;

		}
		cacheFClose(f);
		// Delete anything untouched.
		dbExecute("DELETE FROM games WHERE touced=0", NULL);
	}
}
