/*
 * 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 "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 btnLogoffClick(WidgetT *widget);
static void btnOptionsClick(WidgetT *widget);
static void btnMsgBoxLogoff(MsgBoxButtonT button);
static void menuFilesReady(void);
static void picDoorClick(WidgetT *widget);
static void setButtons(uint8_t enabled);


static WindowT  *_winMenu    = NULL;
static PictureT *_picDoor    = NULL;
static PictureT *_picDOS     = NULL;
static PictureT *_picIF      = NULL;
static ButtonT  *_btnOptions = NULL;
static ButtonT  *_btnLogoff  = NULL;


static void btnLogoffClick(WidgetT *widget) {
	(void)widget;

	setButtons(0);
	msgBoxTwo("Logoff?", MSGBOX_ICON_QUESTION, "This will disconnect you from the server.", "Okay", btnMsgBoxLogoff, "Cancel", btnMsgBoxLogoff);
}


static void btnMsgBoxLogoff(MsgBoxButtonT button) {

	if (button == MSGBOX_BUTTON_ONE) {
		guiDelete(D(_winMenu));
		// ***TODO*** Need a way to close all other windows.
		hangupShow(welcomeShow);
	} else {
		setButtons(1);
	}
}


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


static void menuFilesReady(void) {
	char *dos     = strdup(cacheFilenameGet("menu:dosgames.png"));
	char *door    = strdup(cacheFilenameGet("menu:doorgames.png"));
	char *fiction = strdup(cacheFilenameGet("menu:interactivefiction.png"));

	TagItemT uiMenu[] = {
		T_START,
			T_WINDOW, O(_winMenu),
				T_TITLE, P("KangaWorld Main Menu"),
				T_X, vbeDisplayWidthGet() - 360, T_Y, vbeDisplayHeightGet() - 285,
				T_WIDTH, 350, T_HEIGHT, 275,

				T_PICTURE, O(_picDOS),
					T_FILENAME, P(dos),
					T_X, 18, T_Y, 18,
				T_PICTURE, T_DONE,

				T_PICTURE, O(_picDoor),
					T_FILENAME, P(door),
					T_X, 18, T_Y, 77,
					T_CLICK, P(picDoorClick),
				T_PICTURE, T_DONE,

				T_PICTURE, O(_picIF),
					T_FILENAME, P(fiction),
					T_X, 18, T_Y, 136,
				T_PICTURE, T_DONE,

				T_BUTTON, O(_btnOptions),
					T_TITLE, P("Options"),
					T_X, 23, T_Y, 202,
					T_CLICK, P(btnOptionsClick),
				T_BUTTON, T_DONE,

				T_BUTTON, O(_btnLogoff),
					T_TITLE, P("Logoff"),
					T_X, 245, T_Y, 202,
					T_CLICK, P(btnLogoffClick),
				T_BUTTON, T_DONE,

			T_WINDOW, T_DONE,
		T_END
	};

	tagListRun(uiMenu);

	DEL(fiction);
	DEL(door);
	DEL(dos);
}


void menuShow(void) {
	char *fileList[] = {
		"menu:dosgames.png",
		"menu:doorgames.png",
		"menu:interactivefiction.png",
		NULL
	};
	fileCacheCheck(menuFilesReady, fileList);
}


static void picDoorClick(WidgetT *widget) {

}


static void setButtons(uint8_t enabled) {
	// ***TODO*** This should disable the entire dialog instead of just the buttons.  Need to finish the Enable GUI code first.
	widgetEnableSet(W(_btnOptions), enabled);
	widgetEnableSet(W(_btnLogoff), enabled);
}
