#define _MILSKO
#include <midas.h>

MwWidget window, menu = NULL, fileview = NULL, editor = NULL, console = NULL;

static void image_draw_inject(MwWidget handle) {
	char	  version[128];
	MwPoint	  p, p2;
	MwLLColor c  = MwParseColor(handle, "#fff");
	MwLLColor cd = MwParseColor(handle, "#222");
	MwFLFont  f  = MwFLBuildFont(0);

	handle->bgcolor = MwParseColor(handle, "#888");

	sprintf(version, "Version %s\n\n\xc2\xa9 Pyrite development team", VERSION);

	p.x = MwGetInteger(handle, MwNwidth) - 16;
	p.y = MwGetInteger(handle, MwNheight) - 16 - MwTextHeight(handle, f, version) / 2;
	p2  = p;

	p.x += 1;
	p.y += 1;
	MwDrawText(handle, f, &p, version, MwALIGNMENT_END, cd);
	p = p2;

	MwDrawText(handle, f, &p, version, MwALIGNMENT_END, c);

	MwLLFreeColor(handle->bgcolor);
	handle->bgcolor = NULL;

	MwLLFreeColor(cd);
	MwLLFreeColor(c);
}

static void MWAPI resize(MwWidget handle, void* user, void* client) {
	int ww = MwGetInteger(window, MwNwidth);
	int wh = MwGetInteger(window, MwNheight);
	int x, y, w, h;
	int padding = 5;

	if(menu == NULL) menu = MwCreateWidget(MwMenuClass, "menu", window, 0, 0, 0, 0);

	ww -= padding * 2;
	wh -= MwGetInteger(menu, MwNheight) + padding * 2;

	x = padding;
	y = padding + MwGetInteger(menu, MwNheight);
	w = (ww - padding) * 0.3;
	h = (wh - padding) * 0.8;
	if(fileview == NULL) {
		fileview = MwCreateWidget(MwTreeViewClass, "fileview", window, x, y, w, h);
	} else {
		MwVaApply(fileview,
			  MwNx, x,
			  MwNy, y,
			  MwNwidth, w,
			  MwNheight, h,
			  NULL);
	}

	x = x + w + padding;
	y = padding + MwGetInteger(menu, MwNheight);
	w = (ww - padding) * 0.7;
	h = (wh - padding) * 0.8;
	if(editor == NULL) {
		editor = MwCreateWidget(MwFrameClass, "editor", window, x, y, w, h);
		MwSetInteger(editor, MwNhasBorder, 1);
	} else {
		MwVaApply(editor,
			  MwNx, x,
			  MwNy, y,
			  MwNwidth, w,
			  MwNheight, h,
			  NULL);
	}

	x = padding;
	y = y + h + padding;
	w = ww;
	h = (wh - padding) * 0.2;
	if(console == NULL) {
		console = MwCreateWidget(MwListBoxClass, "console", window, x, y, w, h);
	} else {
		MwVaApply(console,
			  MwNx, x,
			  MwNy, y,
			  MwNwidth, w,
			  MwNheight, h,
			  NULL);
	}
}

void gui_init(void) {
	MwRect rc;
	int    w, h;

	MwLibraryInit();

	window = MwVaCreateWidget(MwWindowClass, "window", NULL, MwDEFAULT, MwDEFAULT, 640, 480,
				  MwNtitle, "Midas",
				  NULL);

	MwVaApply(window,
		  MwNiconPixmap, MwLoadIcon(window, icon_icon),
		  NULL);

	MwGetScreenSize(window, &rc);
	w = MwGetInteger(window, MwNwidth);
	h = MwGetInteger(window, MwNheight);

	if(rc.width >= 1280 && rc.height >= 1024) {
		w = 1024;
		h = 768;
	} else if(rc.width >= 1024 && rc.height >= 768) {
		w = 800;
		h = 600;
	} else {
		w = 640;
		h = 480;
	}

	MwVaApply(window,
		  MwNx, (rc.width - w) / 2,
		  MwNy, (rc.height - h) / 2,
		  MwNwidth, w,
		  MwNheight, h,
		  NULL);

	MwTimeSleep(50);

	MwAddUserHandler(window, MwNresizeHandler, resize, NULL);

	resize(window, NULL, NULL);

	gui_version();
}

static void version_ok_activate(MwWidget handle, void* user, void* client) {
	int	  i;
	MwWidget* children = MwGetChildren(user);

	for(i = 0; children[i] != NULL; i++) {
		if(MwGetClass(children[i]) == MwImageClass) {
			MwLLPixmap px = MwGetVoid(children[i], MwNpixmap);

			MwVaApply(children[i],
				  MwNpixmap, NULL,
				  NULL);
			MwLLDestroyPixmap(px);

			break;
		}
	}

	MwDestroyWidget(user);
}

void gui_version(void) {
	MwWidget    version, image, ok;
	MwRect	    rc;
	int	    x	= MwGetInteger(window, MwNx);
	int	    y	= MwGetInteger(window, MwNy);
	int	    ww	= MwGetInteger(window, MwNwidth);
	int	    wh	= MwGetInteger(window, MwNheight);
	int	    bsh = 48;
	int	    bh	= bsh / 2;
	int	    w	= 640;
	int	    h	= 426 + bh;
	MwLLPixmap  px	= MwLoadIcon(window, icon_midas);
	MwSizeHints sh;

	sh.min_width = sh.max_width = w;
	sh.min_height = sh.max_height = h;

	MwGetScreenSize(window, &rc);

	version = MwVaCreateWidget(MwWindowClass, "window", NULL, x + (ww - w) / 2, y + (wh - h) / 2, w, h,
				   MwNtitle, "Version information",
				   MwNsizeHints, &sh,
				   NULL);

	image		   = MwVaCreateWidget(MwImageClass, "image", version, 0, 0, w, h - bsh,
					      MwNpixmap, px,
					      NULL);
	image->draw_inject = image_draw_inject;

	ok = MwVaCreateWidget(MwButtonClass, "btn", version, w - bh * 4 - (bsh - bh) / 2, h - bsh + (bsh - bh) / 2, bh * 4, bh,
			      MwNtext, "OK",
			      NULL);
	MwAddUserHandler(ok, MwNactivateHandler, version_ok_activate, version);

	MwReparent(version, window);
}

void gui_loop(void) {
	MwLoop(window);
}
