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

	misc.c

    PSP miscellaneous functions.

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

#include "psp.h"


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

int psp_cpuclock;
int bgimage_type;
int bgimage_blightness;


/*------------------------------------------------------
	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;
	}
}


void load_background(int no)
{
	int found = 0;
	char path[MAX_PATH];

	if (bgimage_type == BG_DEFAULT)
	{
		load_png(NULL, no);
		found = 1;
	}
	else if (bgimage_type == BG_USER)
	{
		switch (no)
		{
		case WP_LOGO:     sprintf(path, "%sdata/%s", launchDir, "logo.png");     break;
		case WP_FILER:    sprintf(path, "%sdata/%s", launchDir, "filer.png");    break;
		case WP_GAMECFG:  sprintf(path, "%sdata/%s", launchDir, "gamecfg.png");  break;
		case WP_KEYCFG:   sprintf(path, "%sdata/%s", launchDir, "keycfg.png");   break;
		case WP_STATE:    sprintf(path, "%sdata/%s", launchDir, "state.png");    break;
		case WP_COLORCFG: sprintf(path, "%sdata/%s", launchDir, "colorcfg.png"); break;
		case WP_CMDLIST:  sprintf(path, "%sdata/%s", launchDir, "cmdlist.png");  break;
		}

		found = load_png(path, -1);
	}
	else if (bgimage_type == BG_LOGOONLY)
	{
		sprintf(path, "%sdata/%s", launchDir, "logo.png");
		found = load_png(path, -1);
	}

	if (bgimage_type == BG_DISABLE || !found)
	{
		boxfill(0, 0, 479, 271, COLOR_BLACK);
		boxfill_alpha(0, 0, 479, 271, UI_COLOR(UI_PAL_BG2), 15 * bgimage_blightness / 100);
	}

	draw_bar_shadow();
	boxfill_alpha(0, 0, 479, 23, UI_COLOR(UI_PAL_BG1), 10);
	hline_alpha(0, 479, 23, UI_COLOR(UI_PAL_FRAME), 12);
	hline_alpha(0, 479, 24, UI_COLOR(UI_PAL_FRAME), 10);

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


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

void show_background(void)
{
	video_copy_rect32(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_BATTERY1;
	else if (bat > 33)	icon = ICON_BATTERY2;
	else if (bat >= 10) icon = ICON_BATTERY3;
	else				icon = ICON_BATTERY4;

	counter++;

	if ((charging && (counter % 60 < 40)) || !charging)
		small_icon_shadow(407, 3, UI_COLOR(UI_PAL_TITLE), icon);

	uifont_print_shadow(432, 5, UI_COLOR(UI_PAL_TITLE), "[");
	uifont_print_shadow(462, 5, UI_COLOR(UI_PAL_TITLE), "%]");

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

	width = uifont_get_string_width(bat_left);
	uifont_print_shadow(462 - width, 5, UI_COLOR(UI_PAL_TITLE), bat_left);

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

#if JAPANESE_UI
			sprintf(warning, "警告: バッテリーの残容量がありません(%d%%)。充電してください！", bat);
#else
			sprintf(warning, "Warning: Battery is low (%d%%). Please charge battery!", bat);
#endif
			width = uifont_get_string_width(warning);

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

			draw_dialog(sx - FONTSIZE/2, sy - FONTSIZE/2, ex + FONTSIZE/2, ey + FONTSIZE/2);

			uifont_print_shadow_center(sy, UI_COLOR(UI_PAL_WARNING), warning);
		}
	}
}
