#include <all.inc>
#include "bn_defs.inc"
#include "focus.inc"

// 使用例：
// AttachScript( msgbox, _, "confirm_yesno", " msg = よろしいですか？ ; yes = はい ; no = いいえ " );

public obj:manager;
public obj:focus;
public select;

// ダイアログ部分
public float:width = DIALOG_WIDTH;
public float:height = DIALOG_HEIGHT;
public float:red = DIALOG_NORMAL_COL_R;
public float:green = DIALOG_NORMAL_COL_G;
public float:blue = DIALOG_NORMAL_COL_B;
static obj:defocus;
static obj:clear;
static obj:frame;
//static obj:hilight;

public float: border_left = 0.0 ;

// メッセージ部分
static obj:msg;
static obj:label[2];
static obj:btn[2];
static obj:push_btn;
static bool:msg_ready;

obj:create_text(obj:parent, _w, _h)
{
	new obj:item = CreateTextbox(parent);
	SetOptionI(item, TAG("ALPH"), ALPHA_BLEND_ADD);
	SetOptionI(item, TAG("ZSRT"), ZSORT_TOP_MOST);
	SetOptionI(item, TAG("HALN"), ALIGN_CENTER);
	SetOptionI(item, TAG("FSIZ"), FONTSIZE_NORMAL);
	SetOptionI(item, TAG("TW64"), _w << 6);
	SetOptionI(item, TAG("TH64"), _h << 6);
	SetOptionI(item, TAG("LS64"), 4 << 6);
	return item;
}

obj:create_label(obj:parent, float:x, float:y)
{
	new obj:item = CreateLabel(parent);
	SetPos(item, x, y, 0.0);
	SetOptionI(item, TAG("ALPH"), ALPHA_BLEND_ADD);
	SetOptionI(item, TAG("HALN"), ALIGN_CENTER);
	SetOptionI(item, TAG("VALN"), ALIGN_CENTER);
	SetOptionI(item, TAG("FSIZ"), FONTSIZE_NORMAL);
	SetOptionI(item, TAG("AP64"), 64);
	SetOptionI(item, TAG("KP64"), 64);
	SetOptionI(item, TAG("ZSRT"), ZSORT_TOP_MOST);
	SetOptionF(item, TAG("ZOFS"), 2.0);
	return item;
}

obj:create_button(obj:parent, float:x, float:y)
{
	new obj:item = CreatePlane3x3(parent);
	SetPos(item, x, y, 0.0);
	SetScale(item, 80.0, 32.0, 1.0);
	SetColor(item, 0.0, 0.54, 0.78, 0.0);
	SetOptionI(item, TAG("ZSRT"), ZSORT_TOP_MOST);
	SetOptionF(item, TAG("ZOFS"), 1.0);
	SetOptionI(item, TAG("ALPH"), ALPHA_BLEND_ADD);

	new float:tex_mrgn[4] = {0.5, 0.5, 0.5, 0.5};
	SetOptionP(item, TAG("MGNT"), tex_mrgn);

	new float:scale[3];
	GetScale(item, scale[0], scale[1], scale[2]);

	new float:mrgn[4];
	if (scale[0] > scale[1]) {
		mrgn[0] = mrgn[1] = 0.5 * scale[1] / scale[0];
		mrgn[2] = mrgn[3] = 0.5;
	}
	else {
		mrgn[2] = mrgn[3] = 0.5 * scale[0] / scale[1];
		mrgn[0] = mrgn[1] = 0.5;
	}
	SetOptionP(item, TAG("MGN"), mrgn);
	return item;
}

static
_get_rect(n, &float:_w, &float:_h)
{
	new rect[5];
	for (;;) {
		GetOptionP(label[n], TAG("RECT"), rect);
		if (rect[0]) {
			new float:val = float(rect[3] >> 6) + float(FONTSIZE_NORMAL * 2);
			if (_w < val) {
				_w = val;
			}
			val = float(rect[4] >> 6) + 20.0;
			if (_h < val) {
				_h = val;
			}
			break;
		}
		sleep;
	}
}

main()
{
	assert manager;
	assert focus;

	// 各種引数取得
	new str_msg[512 char];
	GetArg("msg", str_msg);
	new str_yes[32 char];
	GetArg("yes", str_yes);
	new str_no[32 char];
	GetArg("no", str_no);

	// 初期値修正
	select = clamp(select, 0, 1);

	// 部品を作成
	{
		// Defocusの作成
		defocus = CreateDefocus(Self());
		SetColor(defocus, 1.0, 1.0, 1.0, 0.0);
		SetOptionI(defocus, TAG("LEVL"), 3);
		SetOptionI(defocus, TAG("ZSRT"), ZSORT_TOP_MOST);
		SetOptionF(defocus, TAG("ZOFS"), -3.0);

		// Dst Alpha のクリア
		{
			clear = CreatePlane(defocus);
			SetLocalScale(clear, width * 2.0, height * 2.0, 1.0);
			SetColor(clear, 1.0, 1.0, 1.0, 0.0);
			SetOptionI(clear, TAG("ZSRT"), ZSORT_TOP_MOST);
			SetOptionF(clear, TAG("ZOFS"), -2.0);
			SetOptionI(clear, TAG("ALPH"), ALPHA_BLEND_NONE);
		}

		// 下地
		{
		//	frame = CreatePlane3x3(clear);
			frame = CreatePlane(clear);
			//SetScale(frame, width, height, 1.0);
			SetScale(frame, width + border_left, height, 1.0);
			SetPos(frame, - border_left * 0.5, 0.0, 0.0 ) ;
			SetColor(frame, red, green, blue, 0.0);
//			SetTexture(frame, imgGetResource(TEX_WINDOW1));
			SetOptionI(frame, TAG("ZSRT"), ZSORT_TOP_MOST);
			SetOptionF(frame, TAG("ZOFS"), -1.0);
			SetOptionI(frame, TAG("ALPH"), ALPHA_BLEND_ADD);
			SetOptionI(frame, TAG("FDEG"), 1 );

//			new float:tex_mrgn[4] = {0.5, 0.5, 0.5, 0.5};
//			SetOptionP(frame, TAG("MGNT"), tex_mrgn);

//			new float:mrgn[4];
//			mrgn[0] = mrgn[1] = 0.5 * 128.0 / width;
//			mrgn[2] = mrgn[3] = 0.5 * 128.0 / height;
//			SetOptionP(frame, TAG("MGN"), mrgn);
		}
/*
		// ハイライト
		{
			hilight = CreatePlane(frame);
			SetPos(hilight, 0.3, -0.2, 0.0);
			SetScale(hilight, 2.0, 2.0, 1.0);
			SetColor(hilight, 0.0, 0.0, 0.0, 0.0);
			SetTexture(hilight, imgGetResource(TEX_STAR));
			SetOptionI(hilight, TAG("ZSRT"), ZSORT_TOP_MOST);
			SetOptionI(hilight, TAG("ALPH"), ALPHA_BLEND_ADD|ALPHA_BLEND_DESTALPHA);
		}
*/
		// メッセージ
		{
			msg = create_text(defocus,
				floatround(width - 64.0), floatround(height - 48.0));
			SetOptionS(msg, TAG("UTF8"), str_msg);
			SetColor(msg,
				LABEL_NORMAL_COL_R,
				LABEL_NORMAL_COL_G,
				LABEL_NORMAL_COL_B,
				0.0
			);
		}

		// ボタン(yes)
		{
			btn[0] = create_button(defocus, -64.0, 78.0);
			SetTexture(btn[0], imgGetResource(TEX_BUTTON0));
			label[0] = create_label(defocus, -64.0, 78.0);
			SetColor(label[0], 1.0, 1.0, 1.0 /*LABEL_TOUCHABLE_COL_R, LABEL_TOUCHABLE_COL_G, LABEL_TOUCHABLE_COL_B*/, 0.0);
			SetOptionS(label[0], TAG("UTF8"), str_yes);
		}

		// ボタン(no)
		{
			btn[1] = create_button(defocus, 64.0, 78.0);
			SetTexture(btn[1], imgGetResource(TEX_BUTTON0));
			label[1] = create_label(defocus, 64.0, 78.0);
			SetColor(label[1], 1.0, 1.0, 1.0 /*LABEL_TOUCHABLE_COL_R, LABEL_TOUCHABLE_COL_G, LABEL_TOUCHABLE_COL_B*/, 0.0);
			SetOptionS(label[1], TAG("UTF8"), str_no);
		}

		// 押されたときの重ね合わせボタン
		{
			push_btn = create_button(defocus, 0.0, 78.0);
			SetTexture(push_btn, imgGetResource(TEX_BUTTON1));
			Hide(push_btn);
		}
	}

	// ラベルサイズ計算
	{
		new float:w = 0.0;
		new float:h = 0.0;
		new float:x;
		_get_rect(0, w, h);
		_get_rect(1, w, h);
		x = -12.0 - w * 0.5;
		SetPos(btn[0], x, 78.0, 0.0);
		SetPos(label[0], x, 78.0, 0.0);
		SetScale(btn[0], w, h, 1.0);
		x = 12.0 + w * 0.5;
		SetPos(btn[1], x, 78.0, 0.0);
		SetPos(label[1], x, 78.0, 0.0);
		SetScale(btn[1], w, h, 1.0);
		SetScale(push_btn, w, h, 1.0);
	}

	// ダイアログ開く音
	sndSePlay(SE_DLG_OPEN);

	// フェードイン
	do_fade(1.0, 300.0);
	AnimColor(defocus, 0.35, 0.35, 0.35, 1.0, 300.0);
	wait(300);

	// 最初にフォーカス移動
	SetFocus(focus, btn[select]);

	// パッドヘルプの更新
	update_padhelp(PAD_HELP_RR|PAD_HELP_RD);

	for (;;) {

		// パッド入力
		{
			new pad;
			PadState(.press = pad);

			// 左右で選択変更
			if ((pad & ePADLright) && !select) {
				sndSePlay(SE_CURSOR);
				select++;
				SetFocus(focus, btn[select]);
				do_fade(1.0, 100.0);
			}
			else if ((pad & ePADLleft) && select) {
				sndSePlay(SE_CURSOR);
				select--;
				SetFocus(focus, btn[select]);
				do_fade(1.0, 100.0);
			}

			assert select >= 0;
			assert select < 2;

			// ○で決定
			if (pad & ePADRright && !IsFocusMoving(focus)) {
				sndSePlay(SE_DLG_OK);

				// テクスチャ切り替え
				{
					new float:_x, float:_y;
					GetPos(btn[select], _x, _y, _);
					SetPos(push_btn, _x, _y, 0.0);
					Show(push_btn);
					AnimColor(btn[select], 0.0, 0.54, 0.78, 0.0, 100.0);
					AnimColor(push_btn, 0.0, 0.54, 0.78, 1.0, 100.0);
					wait(100);
					AnimColor(btn[select], 0.0, 0.54, 0.78, 1.0, 100.0);
					AnimColor(push_btn, 0.0, 0.54, 0.78, 0.0, 100.0);
					wait(100);
					Hide(push_btn);
				}
				break;
			}

			// ×でキャンセル
			if (pad & ePADRdown && !IsFocusMoving(focus)) {
				sndSePlay(SE_DLG_CANCEL);
				select = -1;
				break;
			}
		}

		// 光源計算
		/*
		if (focus) {
			new float:focus_pos[3];
			GetPos(focus, focus_pos[0], focus_pos[1], focus_pos[2], frame);
			SetPos(hilight, -focus_pos[0], focus_pos[1], 0.0);
			new float:focus_alpha;
			GetColor(focus, _, _, _, focus_alpha);
			AnimColor(hilight,
				focus_alpha * 0.5,
				focus_alpha * 0.5,
				focus_alpha * 0.5,
				1.0, 100.0
			);
		}
*/
		// メッセージ完成待ち
		if (!msg_ready) {
			new rect[5];
			GetOptionP(msg, TAG("RECT"), rect);
			if (rect[0]) {
				msg_ready = true;
				SetPos(msg, - float(rect[3] >> 6) * 0.5, -8.0 - float(rect[4] >> 6) * 0.5, 0.0);
				AnimColor(msg,
					LABEL_NORMAL_COL_R,
					LABEL_NORMAL_COL_G,
					LABEL_NORMAL_COL_B,
					LABEL_NORMAL_COL_A,
					100.0
				);
			}
		}

		sleep;
	}

	// パッドヘルプの更新
	update_padhelp(0);

	// フォーカスなし
	SetFocus(focus, obj:0);

	// フェードアウト
	do_fade(0.0, 300.0);
	AnimColor(msg,
		LABEL_NORMAL_COL_R,
		LABEL_NORMAL_COL_G,
		LABEL_NORMAL_COL_B,
		0.0,
		300.0
	);
	AnimColor(defocus, 1.0, 1.0, 1.0, 0.0, 300.0);
	wait(300);

	return 0;
}

do_fade(float:alpha, float:time)
{
	AnimColor(frame, red, green, blue, alpha * 0.5, time);
	if (select) {
		AnimColor(label[0], 0.3, 0.3, 0.3, /*LABEL_TOUCHABLE_COL_R, LABEL_TOUCHABLE_COL_G, LABEL_TOUCHABLE_COL_B,*/ alpha, time);
		AnimColor(label[1], LABEL_NORMAL_COL_R, LABEL_NORMAL_COL_G, LABEL_NORMAL_COL_B, alpha, time);
	}
	else {
		AnimColor(label[0], LABEL_NORMAL_COL_R, LABEL_NORMAL_COL_G, LABEL_NORMAL_COL_B, alpha, time);
		AnimColor(label[1], 0.3, 0.3, 0.3,/*LABEL_TOUCHABLE_COL_R, LABEL_TOUCHABLE_COL_G, LABEL_TOUCHABLE_COL_B, */alpha, time);
	}
	AnimColor(btn[0], 1.0, 1.0, 1.0, alpha, time);
	AnimColor(btn[1], 1.0, 1.0, 1.0, alpha, time);
}

update_padhelp(padhelp)
{
	SetSystemParam(SYSTEM_PARAM_PADHELP_ONOFF, padhelp);
}
