/***************************************************************************

	mesbox.c

	Message Box

***************************************************************************/

#include "psp.h"

#define MB_MES_MAX	16

#define MB_COLOR_INFO		&ui_palette[UI_PAL_INFO].r,    &ui_palette[UI_PAL_INFO].g,    &ui_palette[UI_PAL_INFO].b
#define MB_COLOR_WARNING	&ui_palette[UI_PAL_WARNING].r, &ui_palette[UI_PAL_WARNING].g, &ui_palette[UI_PAL_WARNING].b
#define MB_COLOR_NORMAL		&ui_palette[UI_PAL_SELECT].r,  &ui_palette[UI_PAL_SELECT].g, &ui_palette[UI_PAL_SELECT].b

#define MB_INFO(s)			{ MB_COLOR_INFO, s }
#define MB_NORMAL(s)		{ MB_COLOR_NORMAL, s }
#define MB_WARNING(s)		{ MB_COLOR_WARNING, s }
#define MB_SKIP				{ MB_COLOR_NORMAL, "\n" }
#define MB_EOM				{ MB_COLOR_NORMAL, "\0" }

enum
{
	MB_OKONLY = 0,
	MB_YESNO,
	MB_TYPE_MAX
};


/***************************************************************************
	Private structures
 ***************************************************************************/

typedef struct message_t
{
	int *r, *g, *b;
	const char *text;
} UI_MESSAGE;

typedef struct dialog_t
{
	const int type;
	const UI_MESSAGE mes[MB_MES_MAX];
} UI_MESSAGEBOX;


const static UI_MESSAGEBOX mesbox[MB_NUM_MAX] =
{
	// MB_LAUNCHDIRECTORY
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_INFO("NEO･GEO CDのディレクトリが見つかりました。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " ゲーム起動   " FONT_CROSS " ディレクトリ参照"),
#else
			MB_INFO("Found NEO･GEO CD directory."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to launch, " FONT_CROSS " to browse directory"),
#endif
			MB_EOM
		}
	},
	// MB_LAUNCHZIPFILE
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_WARNING("注意 : zip圧縮したゲームはロードが遅くなります。"),
			MB_SKIP,
			MB_INFO("zipファイルを解凍することを推奨します。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " 起動        " FONT_CROSS " 中止"),
#else
			MB_WARNING("Warning : zipped game can be very slow."),
			MB_SKIP,
			MB_INFO("Try to uncompress the zip file."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to launch, " FONT_CROSS " to cancel"),
#endif
			MB_EOM
		}
	},
	// MB_BOOTBIOS
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_INFO("NEO･GEO CDZのBIOSを起動します。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " 続行    " FONT_CROSS " 中止"),
#else
			MB_INFO("Press " FONT_CIRCLE " to boot NEO･GEO CDZ BIOS."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to confirm, " FONT_CROSS " to cancel"),
#endif
			MB_EOM
		}
	},
	// MB_EXITEMULATOR
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_INFO("エミュレータを終了します。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " 続行    " FONT_CROSS " 中止"),
#else
			MB_INFO("Press " FONT_CIRCLE " to exit emulator."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to confirm, " FONT_CROSS " to cancel"),
#endif
			MB_EOM
		}
	},
	// MB_SETSTARTUPDIR
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_INFO("このディレクトリを初期ディレクトリに設定します。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " 続行    " FONT_CROSS " 中止"),
#else
			MB_INFO("This directory set as startup directory."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to confirm, " FONT_CROSS " to cancel"),
#endif
			MB_EOM
		}
	},
	// MB_RESETEMULATION
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_INFO("エミュレーションをリセットします。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " 続行    " FONT_CROSS " 中止"),
#else
			MB_INFO("Reset NEO･GEO CDZ emulation."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to confirm, " FONT_CROSS " to cancel"),
#endif
			MB_EOM
		}
	},
	// MB_REStARTEMULATION
	{
		MB_OKONLY,
		{
#if JAPANESE_UI
			MB_INFO("エミュレータを再起動します。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE "ボタンを押してください。"),
#else
			MB_INFO("Need to restart NEO･GEO CDZ emulation."),
			MB_SKIP,
			MB_NORMAL("Press " FONT_CIRCLE " button."),
#endif
			MB_EOM
		}
	},
	// MB_DELETESTATE
	{
		MB_YESNO,
		{
#if JAPANESE_UI
			MB_INFO("ステートデータを削除します。"),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " 続行    " FONT_CROSS " 中止"),
#else
			MB_INFO("Delete save state."),
			MB_SKIP,
			MB_NORMAL(FONT_CIRCLE " to confirm, " FONT_CROSS " to cancel"),
#endif
			MB_EOM
		}
	}
};


/*--------------------------------------------------------
	メッセージボックス表示
 -------------------------------------------------------*/

int messagebox(int no)
{
	int i, w, lines, width, height;
	int sx, sy, ex, ey, ret = 0;
	const UI_MESSAGE *mes;

	if (no > MB_NUM_MAX) return 0;

	video_copy_rect32(show_frame, draw_frame, &full_rect, &full_rect);

	boxfill_alpha(0, 0, SCR_WIDTH - 1, SCR_HEIGHT - 1, COLOR_BLACK, 8);

	lines = 0;
	width = 0;

	mes = mesbox[no].mes;

	while (mes[lines].text[0] != '\0' && lines < MB_MES_MAX)
		lines++;

	for (i = 0; i < lines; i++)
	{
		w = uifont_get_string_width(mes[i].text);
		if (width < w) width = w;
	}

	width >>= 1;			// width / 2
	height = lines << 3;	// (line * (FONTSIZE + 2)) / 2

	sx = SCR_WIDTH / 2 - width;;
	ex = SCR_WIDTH / 2 + width;
	sy = SCR_HEIGHT / 2 - height;
	ey = SCR_HEIGHT / 2 + height;

	draw_dialog(sx - 21, sy - 21, ex + 21, ey + 21);

	sy++;

	for (i = 0; i < lines; i++)
	{
		int r = *mesbox[no].mes[i].r;
		int g = *mesbox[no].mes[i].g;
		int b = *mesbox[no].mes[i].b;

		uifont_print_shadow_center(sy + (i << 4), r, g, b, mesbox[no].mes[i].text);
	}

	video_flip_screen(1);
	pad_wait_clear();

	if (mesbox[no].type)
	{
		do
		{
			video_wait_vsync();
			pad_update();

			if (pad_pressed(PSP_CTRL_CIRCLE))
			{
				ret = 1;
				pad_wait_clear();
				break;
			}

   			if (get_app_status()) break;

		} while (!pad_pressed(PSP_CTRL_CROSS));
	}
	else
	{
		while (1)
		{
			video_wait_vsync();
			pad_update();

			if (pad_pressed(PSP_CTRL_CIRCLE))
			{
				ret = 1;
				pad_wait_clear();
				break;
			}

   			if (get_app_status()) break;
		}
	}

	pad_wait_clear();
	video_flip_screen(1);

	return ret;
}
