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

	misc.c

    PSP miscellaneous functions.

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

#include "psp.h"


/***************************************************************************
	Global variables
 **************************************************************************/

int psp_cpuclock;


/*------------------------------------------------------
	Set PSP CPU clock
 -----------------------------------------------------*/

void set_cpu_clock(void)
{
	switch (psp_cpuclock)
	{
	case 1:  scePowerSetClockFrequency(266, 266, 133); break;
	case 2:  scePowerSetClockFrequency(333, 333, 166); break;
	default: scePowerSetClockFrequency(222, 222, 111); break;
	}
}


/*------------------------------------------------------
	Wait msec.
 -----------------------------------------------------*/

void wait(int msec)
{
	TICKER curr = ticker();
	TICKER target, prev;

	target = curr + msec * (TICKS_PER_SEC / 1000);

	while (curr - target < 0)
	{
		prev = curr;
		curr = ticker();
		if (!bLoop) break;
	}
}


/*------------------------------------------------------
	Load background image
 -----------------------------------------------------*/

void load_background(void)
{
	char path[MAX_PATH];

#ifdef SUPPORT_BMP
	sprintf(path, "%s%s", launchDir, "logo.png");
	if (!load_png(path))
	{
		sprintf(path, "%s%s", launchDir, "logo.bmp");
		if (!load_bmp(path))
		{
			boxfill(0, 0, SCR_WIDTH, SCR_HEIGHT, BG3_COL);
		}
	}
#else
	sprintf(path, "%s%s", launchDir, "logo.png");
	if (!load_png(path))
	{
		boxfill(0, 0, SCR_WIDTH, SCR_HEIGHT, BG3_COL);
	}
#endif

	if (bg_brightness != 100)
	{
		int brightness = 31 - 31 * bg_brightness / 100;
		boxfill_alpha(0, 0, SCR_WIDTH, SCR_HEIGHT, 0, brightness);
	}

	boxfill(0, 0, 479, 20, BG1_COL);
	hline_brightness(0, 479, 21, 25);
	hline_brightness(0, 479, 22, 50);
	hline_brightness(0, 479, 23, 75);
	draw_logo(3, 3, LOGO_COL);

	video_copy_rect(draw_frame, work_frame, &full_rect, &full_rect);
}


/*------------------------------------------------------
	Show background image
 -----------------------------------------------------*/

void show_background(void)
{
	video_copy_rect(work_frame, draw_frame, &full_rect, &full_rect);
}


/*------------------------------------------------------
	Draw battery status
 -----------------------------------------------------*/

void draw_battery_status(int show_warning)
{
	int width, icon;
	int bat = scePowerGetBatteryLifePercent();
	int charging = scePowerIsBatteryCharging();
	static u32 counter = 0;
	char bat_left[8];

	if (bat > 66) icon = ICON_S_BATTERY1;
	else if (bat > 33) icon = ICON_S_BATTERY2;
	else if (bat >= 10) icon = ICON_S_BATTERY3;
	else icon = ICON_S_BATTERY4;

	counter++;

	if ((charging && (counter % 60 < 40)) || !charging)
	{
		small_icon_shadow(408 + 1, 2, icon, 4);
		small_icon(408, 1, LOGO_COL, icon);
	}

	font_print_shadow(432, 0, LOGO_COL, "[", 4);
	font_print_shadow(462, 0, LOGO_COL, "%]", 4);

	if (bat >= 0 && bat <= 100)
		sprintf(bat_left, "%3d", bat);
	else
		strcpy(bat_left, "---");

	width = font_get_string_width(bat_left);
	font_print_shadow(462 - width, 0, LOGO_COL, bat_left, 4);

	if (!charging && bat < 10)
	{
		if (show_warning  && (counter % 120 < 40))
		{
			int sx, sy, ex, ey;
			char warning[128];

			sprintf(warning, "Warning: Battery is low (%d%%). Please charge battery!", bat);
			width = font_get_string_width(warning);

			sx = (SCR_WIDTH - width) >> 1;
			sy = (SCR_HEIGHT - 16) >> 1;
			ex = sx + width;
			ey = sy + 16;

			draw_dialog(sx - 6, sy - 4, ex + 6, ey + 8);
			font_print_shadow_center(sy, MAKECOL15(255,128,128), warning, 4);
		}
	}
}


#if 0
/*------------------------------------------------------
	Draw clock
 -----------------------------------------------------*/

void draw_clock(void)
{
	pspTime t;
	char buf[32], buf2[32];
	int width;
	extern int neogeo_region;

	sceRtcGetCurrentClockLocalTime(&t);

	switch (neogeo_region)
	{
	case 0:
	case 1:	sprintf(buf, "%d/%d  ", t.month, t.day); break;
	case 2: sprintf(buf, "%d/%d  ", t.day, t.month); break;
	}

	sprintf(buf2, "%d:%d", t.hour, t.minutes);
	strcat(buf, buf2);

	width = font_get_string_width(buf);

	font_print_shadow(400 - width + 1, 1, LOGO_COL, buf, 4);
}

#endif
