/***   [werror.c]
*
*	エラーウィンドウ		(C)ささがわ
*
*	For GNU C Compiler (GCC)   Version 1.39
*
***/

#include <stdio.h>
#include <string.h>
#include "graph.h"
#include "mos.h"
#include "window.h"
#include "icn.h"
#include "others.h"
#include "beep.h"

#define WH_TITLE	1
#define WH_ON		2
#define WH_OTHER	0

extern int	PAL_Back;
extern int	PAL_Black;
static int	wx, wy;

static void	Draw_window(void);
static int	Where(int, int);
static void Message(int, const char *);

void WIND_error(int no, const char *f) {
	int		ret = 0;
	char	fname[20];
	struct RECT	a, b;
	
	if (no < 0 || 5 < no)
		return;
	
	MOS_disp(0);
	ICN_mos(0);
	EGB_cls(0);
	MOS_disp(1);
	if (f == NULL)
		fname[0] = '\0';
	else {
		strncpy(fname, f, 20);
		fname[strLE(fname, 18)] = '\0';
	}
	wx = 174;
	wy = 169;
	a.x1 = 319;	a.y1 = 259;
	a.x2 = 320;	a.y2 = 260;
	b.x1 = wx;	b.y1 = wy;
	b.x2 = wx + 291;	b.y2 = wy + 141;
	afterImage(&a, &b);
	
	Draw_window();
	Message(no, fname);
	while (!ret) {
		char	mb;
		int		mx, my, wh;
		
		CLOCK(0);
		if (MOS_rdpos(&mb, &mx, &my), !(mb & 1))
			continue;
		
		if ((wh = Where(mx, my)) == WH_ON) {
			if (Button(wx + 195, wy + 108, wx + 274, wy + 129)) {
				ret = 1;
			}
		} else if (wh == WH_TITLE) {
			struct RECT	s, w;
			
			w.x1 = wx;	w.y1 = wy;
			w.x2 = wx + 291;	w.y2 = wy + 141;
			s.x1 = 0;	s.y1 = 40;	s.x2 = 639;	s.y2 = 463;
			if (dragWindow(mx, my, &w, &s, 0, 0)) {
				wx = w.x1;	wy = w.y1;
				MOS_disp(0);
				EGB_cls(0);
				MOS_disp(1);
				Draw_window();
				Message(no, fname);
			}
		} else {
			while (MOS_rdpos(&mb, &mx, &my), mb & 1);
		}
	}
	
	MOS_disp(0);
	EGB_cls(0);
	MOS_disp(1);
	return;
}

static void Draw_window(void) {
	int		i;
	struct opnwin_t	opw;
	
	opw.title = "お 知 ら せ";
	opw.x1 = wx;
	opw.y1 = wy;
	opw.x2 = opw.x1 + 291;
	opw.y2 = opw.y1 + 141;
	opw.shdw = 1;
	opw.canb = 0;
	opw.nopt = 0;
	opw.wopt = NULL;
	opw.expb = 0;
	opw.ord = 0;
	MOS_disp(0);
	drawWindow(&opw);
	
	EGB_boxf(wx + 14, wy + 37, wx + 47, wy + 70, PAL_Black, 15);
	ICN_put(25, wx + 15, wy + 38, PAL_Black);
	DrawButton(1, wx + 194, wy + 107, wx + 275, wy + 130);
	for (i = 0; i < 2; i++)
		EGB_str2(" 確    認 ", wx + 195 + i, wy + 126, PAL_Black);
	MOS_disp(1);
	BEP_on();
}

static int Where(int x, int y) {
	int		ret;
	
	x -= wx;
	y -= wy;
	if (5 < x && x < 286 && 5 < y && y < 26)
		ret = WH_TITLE;
	else if (194 < x && x < 275 && 107 < y && y < 130)
		ret = WH_ON;
	else
		ret = WH_OTHER;
	
	return ret;
}

static void Message(int no, const char *f) {
	int		i;
	static const char *mes[][2] = { {
	/*      "                           " */
			"ファイルの読み込みに",
			"            失敗しました"
		}, {
			"メモリが足りません",
			""
		}, {
			"これ以上、ウィンドウを",
			"       開くことは出来ません"
		}, {
			"背景画の読み込みに",
			"            失敗しました"
		}, {
			"サウンドの読み込みに",
			"            失敗しました"
		}, {
			"この機能は、将来",
			"     サポートされる予定です"
	} };
	
	i = !f[0] ? 10 : 0;
	MOS_disp(0);
	EGB_str2(mes[no][0], wx + 62, wy + 54 + i, PAL_Black);
	EGB_str2(mes[no][1], wx + 62, wy + 74 + i, PAL_Black);
	if (f[0]) {
		EGB_str2("ﾌｧｲﾙ名", wx + 62, wy + 94, PAL_Black);
		EGB_str2(f, wx + 118, wy + 94, PAL_Black);
	}
	MOS_disp(1);
}
