/*
 * 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
		guiMouseBusyPop();
		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",

		"games:d:descent:banner.png",
		"games:d:descent:box2.png",
		"games:d:descent:screen1.png",
		"games:d:descent:screen2.png",
		"games:d:descent:screen3.png",
		"games:d:descent:screen4.png",
		"games:d:descent:screen5.png",
		"games:d:descent:screen6.png",
		"games:d:descent:box1-thumb.png",
		"games:d:descent:box1.png",
		"games:d:descent:box2-thumb.png",
		"games:d:descent:screen1-thumb.png",
		"games:d:descent:screen2-thumb.png",
		"games:d:descent:screen3-thumb.png",
		"games:d:descent:screen4-thumb.png",
		"games:d:descent:screen5-thumb.png",
		"games:d:descent:screen6-thumb.png",
		"games:d:dm:screen1.png",
		"games:d:dm:screen2.png",
		"games:d:dm:screen3.png",
		"games:d:dm:screen4.png",
		"games:d:dm:screen5.png",
		"games:d:dm:screen6.png",
		"games:d:dm:screen6-thumb.png",
		"games:d:dm:screen5-thumb.png",
		"games:d:dm:screen4-thumb.png",
		"games:d:dm:screen3-thumb.png",
		"games:d:dm:screen2-thumb.png",
		"games:d:dm:screen1-thumb.png",
		"games:d:dm:banner.png",
		"games:t:tal:screen6.png",
		"games:t:tal:screen5.png",
		"games:t:tal:screen4.png",
		"games:t:tal:screen3.png",
		"games:t:tal:screen2.png",
		"games:t:tal:screen1.png",
		"games:t:tal:screen6-thumb.png",
		"games:t:tal:screen5-thumb.png",
		"games:t:tal:screen4-thumb.png",
		"games:t:tal:screen3-thumb.png",
		"games:t:tal:screen2-thumb.png",
		"games:t:tal:screen1-thumb.png",
		"games:t:tal:banner.png",
		"games:t:tw2002:screen6.png",
		"games:t:tw2002:screen5.png",
		"games:t:tw2002:screen4.png",
		"games:t:tw2002:screen3.png",
		"games:t:tw2002:screen2.png",
		"games:t:tw2002:screen1.png",
		"games:t:tw2002:screen1-thumb.png",
		"games:t:tw2002:screen2-thumb.png",
		"games:t:tw2002:screen3-thumb.png",
		"games:t:tw2002:screen4-thumb.png",
		"games:t:tw2002:screen5-thumb.png",
		"games:t:tw2002:screen6-thumb.png",
		"games:t:tw2002:banner.png",
		"games:b:bre:screen1.png",
		"games:b:bre:screen2.png",
		"games:b:bre:screen3.png",
		"games:b:bre:screen4.png",
		"games:b:bre:screen5.png",
		"games:b:bre:screen1-thumb.png",
		"games:b:bre:screen2-thumb.png",
		"games:b:bre:screen3-thumb.png",
		"games:b:bre:screen4-thumb.png",
		"games:b:bre:screen5-thumb.png",
		"games:b:bre:banner.png",
		"games:f:fe:screen6.png",
		"games:f:fe:screen5.png",
		"games:f:fe:screen4.png",
		"games:f:fe:screen3.png",
		"games:f:fe:screen2.png",
		"games:f:fe:screen1.png",
		"games:f:fe:screen1-thumb.png",
		"games:f:fe:screen2-thumb.png",
		"games:f:fe:screen3-thumb.png",
		"games:f:fe:screen4-thumb.png",
		"games:f:fe:screen5-thumb.png",
		"games:f:fe:screen6-thumb.png",
		"games:f:fe:banner.png",
		"games:f:fh:screen1.png",
		"games:f:fh:screen2.png",
		"games:f:fh:screen3.png",
		"games:f:fh:screen4.png",
		"games:f:fh:screen5.png",
		"games:f:fh:screen6.png",
		"games:f:fh:screen6-thumb.png",
		"games:f:fh:screen5-thumb.png",
		"games:f:fh:screen4-thumb.png",
		"games:f:fh:screen3-thumb.png",
		"games:f:fh:screen2-thumb.png",
		"games:f:fh:screen1-thumb.png",
		"games:f:fh:banner.png",
		"games:g:gbkg:screen3.png",
		"games:g:gbkg:screen2.png",
		"games:g:gbkg:screen1.png",
		"games:g:gbkg:screen1-thumb.png",
		"games:g:gbkg:screen2-thumb.png",
		"games:g:gbkg:screen3-thumb.png",
		"games:g:gbkg:banner.png",
		"games:g:gwar:screen1.png",
		"games:g:gwar:screen2.png",
		"games:g:gwar:screen3.png",
		"games:g:gwar:screen4.png",
		"games:g:gwar:screen5.png",
		"games:g:gwar:screen6.png",
		"games:g:gwar:screen6-thumb.png",
		"games:g:gwar:screen5-thumb.png",
		"games:g:gwar:screen4-thumb.png",
		"games:g:gwar:screen3-thumb.png",
		"games:g:gwar:screen2-thumb.png",
		"games:g:gwar:screen1-thumb.png",
		"games:g:gwar:banner.png",
		"games:h:hackn:screen3.png",
		"games:h:hackn:screen2.png",
		"games:h:hackn:screen1.png",
		"games:h:hackn:screen1-thumb.png",
		"games:h:hackn:screen2-thumb.png",
		"games:h:hackn:screen3-thumb.png",
		"games:h:hackn:banner.png",
		"games:o:odd:screen4.png",
		"games:o:odd:screen3.png",
		"games:o:odd:screen2.png",
		"games:o:odd:screen1.png",
		"games:o:odd:screen4-thumb.png",
		"games:o:odd:screen3-thumb.png",
		"games:o:odd:screen2-thumb.png",
		"games:o:odd:screen1-thumb.png",
		"games:o:odd:banner.png",
		"games:s:sg:screen3.png",
		"games:s:sg:screen2.png",
		"games:s:sg:screen1.png",
		"games:s:sg:screen3-thumb.png",
		"games:s:sg:screen2-thumb.png",
		"games:s:sg:screen1-thumb.png",
		"games:s:sg:banner.png",
		"games:s:sre:screen1.png",
		"games:s:sre:screen1-thumb.png",
		"games:s:sre:banner.png",
		"browser:no-box.png",
		"browser:no-screen.png",

		NULL
	};

	BEGIN

	guiMouseBusyPush();
	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);
	}
	*/
}
