/*
 * 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 "browser.h"

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

#include "network.h"
#include "util.h"


static WindowT     *_winBrowser        = NULL;
static ButtonT     *_btnBoxTab         = NULL;
static ButtonT     *_btnDescriptionTab = NULL;
static ButtonT     *_btnInfoTab        = NULL;
static ButtonT     *_btnScreensTab     = NULL;
static ButtonT     *_btnSearchTab      = NULL;
static uint8_t      _channel           = 0;
static FrameT      *_fraBox            = NULL;
static FrameT      *_fraDescription    = NULL;
static FrameT      *_fraInfo           = NULL;
static FrameT      *_fraScreens        = NULL;
static FrameT      *_fraSearch         = NULL;
static ListboxT    *_lstDescription    = NULL;
static ListboxT    *_lstInfo           = NULL;
static PictureT    *_picBanner         = NULL;
static PictureT    *_picBox1           = NULL;
static PictureT    *_picBox2           = NULL;
static PictureT    *_picThumb1         = NULL;
static PictureT    *_picThumb2         = NULL;
static PictureT    *_picThumb3         = NULL;
static PictureT    *_picThumb4         = NULL;
static PictureT    *_picThumb5         = NULL;
static PictureT    *_picThumb6         = NULL;


static void btnTabClick(WidgetT *widget);
static void packetHandler(PacketDecodeDataT *packet);


void browserShow(void) {

	char **lines = NULL;

	TagItemT uiBrowser[] = {
		T_START,
			T_WINDOW, O(_winBrowser),
				T_TITLE, P("Game Browser"),
				T_WIDTH, 525, T_HEIGHT, 323,

				// SEARCH
				T_BUTTON, O(_btnSearchTab),
					T_X, 5, T_Y, 5,
					T_TITLE, P("Search"),
					T_CLICK, P(btnTabClick),
				T_BUTTON, T_DONE,
				T_FRAME, O(_fraSearch),
					T_X, 5, T_Y, 32,
					T_WIDTH, 500, T_HEIGHT, 250,
					T_TITLE, P("Search"),
					T_VISIBLE, T_FALSE,
				T_FRAME, T_DONE,

				// GAME
				T_BUTTON, O(_btnDescriptionTab),
					T_X, 78, T_Y, 5,
					T_TITLE, P("Game"),
					T_CLICK, P(btnTabClick),
				T_BUTTON, T_DONE,
				T_FRAME, O(_fraDescription),
					T_X, 5, T_Y, 32,
					T_WIDTH, 500, T_HEIGHT, 250,
					T_TITLE, P("Description"),
					T_VISIBLE, T_FALSE,

					T_PICTURE, O(_picBanner),
						T_X, 1, // 490PX wide
						T_FILENAME, P("banner.png"),
					T_PICTURE, T_DONE,
					T_LISTBOX, O(_lstDescription),
						T_X, 1, T_Y, 105,
						T_WIDTH, 493, T_HEIGHT, 124,  // Works out to 57 characters wide, 8 high.
					T_LISTBOX, T_DONE,

				T_FRAME, T_DONE,

				// SCREENSHOTS
				T_BUTTON, O(_btnScreensTab),
					T_X, 135, T_Y, 5,
					T_TITLE, P("Screens"),
					T_CLICK, P(btnTabClick),
				T_BUTTON, T_DONE,
				T_FRAME, O(_fraScreens),
					T_X, 5, T_Y, 32,
					T_WIDTH, 500, T_HEIGHT, 250,
					T_TITLE, P("Screenshots"),
					T_VISIBLE, T_FALSE,

						T_PICTURE, O(_picThumb1),  // Thumbs are 160x100
							T_X, 1, T_Y, 5,
							T_FILENAME, P("thumb1.png"),
						T_PICTURE, T_DONE,
						T_PICTURE, O(_picThumb2),
							T_X, 167, T_Y, 5,
							T_FILENAME, P("thumb2.png"),
						T_PICTURE, T_DONE,
						T_PICTURE, O(_picThumb3),
							T_X, 333, T_Y, 5,
							T_FILENAME, P("thumb3.png"),
						T_PICTURE, T_DONE,

						T_PICTURE, O(_picThumb4),
							T_X, 1, T_Y, 116,
							T_FILENAME, P("thumb4.png"),
						T_PICTURE, T_DONE,
						T_PICTURE, O(_picThumb5),
							T_X, 167, T_Y, 116,
							T_FILENAME, P("thumb5.png"),
						T_PICTURE, T_DONE,
						T_PICTURE, O(_picThumb6),
							T_X, 333, T_Y, 116,
							T_FILENAME, P("thumb6.png"),
						T_PICTURE, T_DONE,

				T_FRAME, T_DONE,

				// BOX
				T_BUTTON, O(_btnBoxTab),
					T_X, 216, T_Y, 5,
					T_TITLE, P("Box"),
					T_CLICK, P(btnTabClick),
				T_BUTTON, T_DONE,
				T_FRAME, O(_fraBox),
					T_X, 5, T_Y, 32,
					T_WIDTH, 500, T_HEIGHT, 250,
					T_TITLE, P("Packaging"),
					T_VISIBLE, T_FALSE,

						T_PICTURE, O(_picBox1),  // Thumbs are 240x210
							T_X, 1, T_Y, 5,
							T_FILENAME, P("box1.png"),
						T_PICTURE, T_DONE,
						T_PICTURE, O(_picBox2),
							T_X, 250, T_Y, 5,
							T_FILENAME, P("box2.png"),
						T_PICTURE, T_DONE,

				T_FRAME, T_DONE,

				// INFO
				T_BUTTON, O(_btnInfoTab),
					T_X, 265, T_Y, 5,
					T_TITLE, P("Info"),
					T_CLICK, P(btnTabClick),
				T_BUTTON, T_DONE,
				T_FRAME, O(_fraInfo),
					T_X, 5, T_Y, 32,
					T_WIDTH, 500, T_HEIGHT, 250,
					T_TITLE, P("Information"),
					T_VISIBLE, T_TRUE,

					T_LISTBOX, O(_lstInfo),
						T_X, 1, T_Y, 5,
						T_WIDTH, 493, T_HEIGHT, 220,  // Works out to 57 characters wide, 15 high.
					T_LISTBOX, T_DONE,

				T_FRAME, T_DONE,


			T_WINDOW, T_DONE,
		T_END
	};

	tagListRun(uiBrowser);
	_channel = netChannelGet(packetHandler);

	guiDebugAreaShow(W(_btnSearchTab));
	guiDebugAreaShow(W(_btnDescriptionTab));
	guiDebugAreaShow(W(_btnScreensTab));
	guiDebugAreaShow(W(_btnBoxTab));
	guiDebugAreaShow(W(_btnInfoTab));

	lines = utilWrapText(
		"The Post-Terran Minerals Corporation (PTMC) digs up minerals on all nine planets of the solar system, "
		"employing humans and robots to do its job. Unfortunately, the mining robots are now being controlled by a hacker, "
		"and have taken the human workers hostage. The PTMC has tried to get the robots back under their control, but "
		"communication with the mining stations has been lost. In desperation, the PTMC modifies a Pyro-GX ship for combat "
		"and hires a mercenary, codenamed 'Material Defender', to fly it. The mission is to destroy the infected mines and "
		"rescue human hostages, destroying any hostile robot that gets in the way.", 57);
	while (arrlen(lines) > 0) {
		listboxItemAdd(_lstDescription, lines[0]);
		DEL(lines[0]);
		arrdel(lines, 0);
	}

	listboxItemAdd(_lstInfo, "123456789012345678901234567890123456789012345678901234567");
	listboxItemAdd(_lstInfo, "2");
	listboxItemAdd(_lstInfo, "3");
	listboxItemAdd(_lstInfo, "4");
	listboxItemAdd(_lstInfo, "5");
	listboxItemAdd(_lstInfo, "6");
	listboxItemAdd(_lstInfo, "7");
	listboxItemAdd(_lstInfo, "8");
	listboxItemAdd(_lstInfo, "9");
	listboxItemAdd(_lstInfo, "10");
	listboxItemAdd(_lstInfo, "11");
	listboxItemAdd(_lstInfo, "12");
	listboxItemAdd(_lstInfo, "13");
	listboxItemAdd(_lstInfo, "14");
	listboxItemAdd(_lstInfo, "15");
	listboxItemAdd(_lstInfo, "16");
	listboxItemAdd(_lstInfo, "17");
}


static void btnTabClick(WidgetT *widget) {

	widgetVisibleSet(W(_fraBox),         0);
	widgetVisibleSet(W(_fraDescription), 0);
	widgetVisibleSet(W(_fraInfo),        0);
	widgetVisibleSet(W(_fraScreens),     0);
	widgetVisibleSet(W(_fraSearch),      0);

	if (widget == W(_btnBoxTab))         widgetVisibleSet(W(_fraBox),         1);
	if (widget == W(_btnDescriptionTab)) widgetVisibleSet(W(_fraDescription), 1);
	if (widget == W(_btnInfoTab))        widgetVisibleSet(W(_fraInfo),        1);
	if (widget == W(_btnScreensTab))     widgetVisibleSet(W(_fraScreens),     1);
	if (widget == W(_btnSearchTab))      widgetVisibleSet(W(_fraSearch),      1);
}


static void packetHandler(PacketDecodeDataT *packet) {

}
