/***   [clck.c]
*
*	時計ウィンドウ 関連		(C)ささがわ
*
*	For GNU C Compiler (GCC)   Version 1.39
*
***/

#include <stdio.h>
#include <math.h>
#include <bios.h>
#include "graph.h"
#include "mos.h"
#include "window.h"
#include "icn.h"
#include "others.h"

#define WH_CAN		1
#define WH_TITLE	2
#define WH_OTHER	0

extern int	PAL_Black;
static int	wx, wy;
static int	ptime[3];
static float	pcord[6];

extern void	segpatch(void);
static void	Draw_window(void);
static int	Where(int, int);
static void	clock(void);
static void	cal(int, int);

void WIND_clock(void) {
	int		ret = 0, i;
	struct RECT	a, b;
	
	wx = 235;
	wy = 145;
	a.x1 = 319;	a.y1 = 259;
	a.x2 = 320;	a.y2 = 260;
	b.x1 = wx;	b.y1 = wy;
	b.x2 = wx + 169;	b.y2 = wy + 190;
	afterImage(&a, &b);
	
	for (i = 0; i < 3; ptime[i++] = -1);
	for (i = 0; i < 6; pcord[i++] = 0);
	Draw_window();
	while (!ret) {
		char	mb;
		int		mx, my, wh;
		
		CLOCK(0);
		if (MOS_rdpos(&mb, &mx, &my), !(mb & 1)) {
			clock();
			continue;
		}
		
		if ((wh = Where(mx, my)) == WH_CAN) {
			if (Button(wx + 6, wy + 6, wx + 25, wy + 25))
				ret = 1;
		} else if (wh == WH_TITLE) {
			struct RECT	s, w;
			
			w.x1 = wx;	w.y1 = wy;
			w.x2 = wx + 169;	w.y2 = wy + 190;
			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();
				for (i = 0; i < 3; ptime[i++] = -1);
				for (i = 0; i < 6; pcord[i++] = 0);
			}
		} else {
			while (MOS_rdpos(&mb, &mx, &my), mb & 1)
				clock();
		}
	}
	
	return;
}

static void Draw_window(void) {
	short	cir[3];
	struct opnwin_t	opw;
	
	opw.title = "Ｃｌｏｃｋ";
	opw.x1 = wx;
	opw.y1 = wy;
	opw.x2 = opw.x1 + 169;
	opw.y2 = opw.y1 + 190;
	opw.shdw = 1;
	opw.canb = 1;
	opw.nopt = 0;
	opw.wopt = NULL;
	opw.expb = 0;
	opw.ord = 0;
	MOS_disp(0);
	drawWindow(&opw);
	
	EGB_color(EGB_work, EGB_COL_FORE, PAL_Black);
	EGB_color(EGB_work, EGB_COL_PAINT, 15);
	EGB_paintMode(EGB_work, 0x022);
	
	EGB_pen(EGB_work, 0);
	EGB_penSize(EGB_work, 7);
	cir[0] = wx + 85;
	cir[1] = wy + 106;
	cir[2] = 65;
	EGB_circle(EGB_work, (char *)cir);
	EGB_penSize(EGB_work, 1);
	MOS_disp(1);
}

static int Where(int x, int y) {
	int		ret;
	
	x -= wx;
	y -= wy;
	if (5 < x && x < 26 && 5 < y && y < 26)
		ret = WH_CAN;
	else if (26 < x && x < 164 && 5 < y && y < 26)
		ret = WH_TITLE;
	else
		ret = WH_OTHER;
	
	return ret;
}

static void clock(void) {
	struct calender_t	a;
	
	_bios_timeofday(_TIME_GETCLOCK, &a);
	if (a.second == ptime[2])
		return;
	
	MOS_disp(0);
	cal(2, a.second);
	cal(1, a.minute);
	cal(0, a.hour * 60 + a.minute);
	MOS_disp(1);
}

#define PI	(3.141592654)
static void cal(int mode, int t) {
	int		rr;
	float	r, x, y;
	
	switch (mode) {
		case 0:
			EGB_penSize(EGB_work, 7);
			rr = 40;
			break;
		case 1:
			EGB_penSize(EGB_work, 5);
			rr = 55;
			break;
		default:
			EGB_penSize(EGB_work, 1);
			rr = 55;
			break;
	}
	segpatch();		/* Segment Register Settings */
	r = PI / 2.0 - (mode == 0 ? (t % 720) / 720.0 : t / 60.0) * 2.0 * PI;
	x = rr * cos(r);
	y = rr * sin(r);
	if (ptime[mode] != t) {
		EGB_line(wx + 85, wy + 106, wx + 85 + pcord[mode * 2], wy + 106 - pcord[mode * 2 + 1], 15);
		ptime[mode] = t;
		pcord[mode * 2] = x;
		pcord[mode * 2 + 1] = y;
	}
	EGB_line(wx + 85, wy + 106, wx + 85 + x, wy + 106 - y, PAL_Black);
	EGB_penSize(EGB_work, 1);
}
#undef PI
