//////////////////////////////////////////
// DiceProc.h
// Copyright (c) 05/21/2022 by BCCSkelton
//////////////////////////////////////////

/////////////////////////////////
//主ウィンドウCMyWndの関数の定義
//ウィンドウメッセージ関数
/////////////////////////////////
bool CMyWnd::OnInit(WPARAM wParam, LPARAM lParam) {

	//コモンコントロールの初期化
	InitCommonControls();
	//使用言語の設定
	if(MessageBox(m_hWnd, "\"Yes\" selects Japanese, while \"No\" does English.\r\n「はい」は日本語、"\
					"「いいえ」は英語を選択します。", "Choise of Language - 言語の選択",
					MB_YESNO | MB_ICONQUESTION) == IDYES)
		g_JAPANESE = TRUE;
	//メインダイアログやコントロールのフォントを日本語へ変更する
	if(g_JAPANESE) {
		g_Font.SetFont(m_hWnd, 8, "ＭＳ 明朝");
		SendItemMsg(IDC_EDIT, WM_SETFONT, (WPARAM)g_Font.GetFont(), (LPARAM)TRUE);
		SendItemMsg(IDC_STATUSBAR, WM_SETFONT, (WPARAM)g_Font.GetFont(), (LPARAM)TRUE);
		SendMsg(WM_SETTEXT, 0, (LPARAM)Str_10);
		SendItemMsg(IDC_HOWTOUSE, WM_SETTEXT, 0, (LPARAM)Str_11);
		SendItemMsg(IDC_ROLL, WM_SETTEXT, 0, (LPARAM)Str_12);
		SendItemMsg(IDOK, WM_SETTEXT, 0, (LPARAM)Str_13);
	}/* ダイアログのデフォルトが無ければこれが必要
	else {
		g_Font.SetFont(m_hWnd, 8, "Times New Roman");
		SendItemMsg(IDC_EDIT, WM_SETFONT, (WPARAM)g_Font.GetFont(), (LPARAM)TRUE);
		SendItemMsg(IDC_STATUSBAR, WM_SETFONT, (WPARAM)g_Font.GetFont(), (LPARAM)TRUE);
		SendMsg(WM_SETTEXT, 0, (LPARAM)Str_00);
		SendItemMsg(IDC_HOWTOUSE, WM_SETTEXT, 0, (LPARAM)Str_01);
		SendItemMsg(IDC_ROLL, WM_SETTEXT, 0, (LPARAM)Str_02);
		SendItemMsg(IDOK, WM_SETTEXT, 0, (LPARAM)Str_03);
	}*/
	//ステータスバー登録−SetHandle(hWnd))
	SBar.SetHandle(GetDlgItem(m_hWnd, IDC_STATUSBAR));
	//ステータスバー区画設定
	int sec[2] = {100, -1};
	SBar.SetSection(2, sec);
	//ステータスバー文字列設定
	SBar.SetText(0, "Dice Ver 1.0");
	if(g_JAPANESE)
		SBar.SetText(1, Str_14);
	else
		SBar.SetText(1, Str_04);
	return TRUE;
}

bool CMyWnd::OnClose(WPARAM wParam, LPARAM lParam) {

	char *message, *title;
	if(g_JAPANESE) {
		message = Str_15;
		title = Str_16;
	}
	else {
		message = Str_05;
		title = Str_06;
	}
	if(MessageBox(m_hWnd, message, title, MB_YESNO | MB_ICONINFORMATION) == IDYES) {
		//処理をするとDestroyWindow、PostQuitMessageが呼ばれる
		EndModal(0);	//WinMainの正常終了は0Lを返す
		return TRUE;
	}
	else
		//そうでなければウィンドウではDefWindowProc関数をreturn、ダイアログではreturn FALSEとなる。
		return FALSE;
}

/////////////////////////////////
//主ウィンドウCMyWndの関数の定義
//メニュー項目、コントロール関数
/////////////////////////////////
bool CMyWnd::OnHowtouse() {

	CSTR str;
	char* title;
	if(g_JAPANESE) {
		str.FromFile("RurlesJP.txt");
		title = Str_17;
	}
	else {
		str.FromFile("RurlesEN.txt");
		title = Str_07;
	}
	MessageBox(m_hWnd, str.ToChar(), title, MB_OK | MB_ICONINFORMATION);
	return TRUE;
}

bool CMyWnd::OnRoll() {

	int num[3];
	char buff[256];
	CSTR str;
	PlaySound("IDS_DICESOUND", m_hInstance, SND_RESOURCE | SND_ASYNC);
	for(int i = 0; i < 3; i++) {
		num[i] = SendItemMsg(IDC_D1 + i, DM_ROLL, 0, 0);
		if(g_JAPANESE)
			wsprintf(buff, Str_18, num[i], i);
		else
			wsprintf(buff, Str_08, num[i], i);
		str = str + buff + "\r\n";
		SendItemMsg(IDC_EDIT, WM_SETTEXT, 0, (LPARAM)str.ToChar());
	}
	m_Times--;		//賽を振る回数を減らす（目や役が出来なければ、3回迄賽を振れる）
/* 開発時のテスト用
	if(g_JAPANESE) 
		str = 
		"/////////////////////////////////////////////////////////////////\r\n"\
		"// チンチロリンの目、役の判定条件について\r\n"\
		"// 1 → 0 0 0 1\r\n"\
		"// 2 → 0 0 1 0\r\n"\
		"// 3 → 0 0 1 1\r\n"\
		"// 4 → 0 1 0 0\r\n"\
		"// 5 → 0 1 0 1\r\n"\
		"// 6 → 0 1 1 0\r\n"\
		"//「アラシ」\r\n"\
		"// num[0] | num[1] | num[2]（== num[0] & num[1] & num[2]）== num[0];\r\n"\
		"// 全ての要素のOR（またはAND）がいずれも要素である場合。\r\n"\
		"//「456」\r\n"\
		"// num[0] ^ num[1] ^ num[2] == 7 && num[0] & num[1] & num[2] == 4\r\n"\
		"// 各桁のビットが奇数個立っており、全ての要素が4以上である場合。\r\n"\
		"//「123」\r\n"\
		"// num[0] ^ num[1] ^ num[2] == 0 && num[0] | num[1] | num[2] == 3\r\n"\
		"// 各桁のビットが偶数個立っており、全ての要素が3以下である場合。\r\n"\
		"//「目」\r\n"\
		"// num[0] == num[1]) | (num[1] == num[2]) | (num[0] == num[2]\r\n"\
		"// 二つの要素のいずれかが等しい場合。\r\n"\
		"// なお、3つの要素の排他的論理和が最後の目となる。\r\n"\
		"/////////////////////////////////////////////////////////////////\r\n";
	else
		str = 
		"/////////////////////////////////////////////////////////////////\r\n"\
		"// Aoubt the conditions to determine hands of \"Chinchirorin\"\r\n"\
		"// 1 → 0 0 0 1\r\n"\
		"// 2 → 0 0 1 0\r\n"\
		"// 3 → 0 0 1 1\r\n"\
		"// 4 → 0 1 0 0\r\n"\
		"// 5 → 0 1 0 1\r\n"\
		"// 6 → 0 1 1 0\r\n"\
		"// <Storm>\r\n"\
		"// num[0] | num[1] | num[2]（== num[0] & num[1] & num[2]）== num[0];\r\n"\
		"// In case that all numbers' OR (or AND) is any of the numbers\r\n"\
		"// <456>\r\n"\
		"// num[0] ^ num[1] ^ num[2] == 7 && num[0] & num[1] & num[2] == 4\r\n"\
		"// In case that every digit has odd of \'1\' AND all numbers are more than 3.\r\n"\
		"// <123>\r\n"\
		"// num[0] ^ num[1] ^ num[2] == 0 && num[0] | num[1] | num[2] == 3\r\n"\
		"// Incase that every digit has even of \'1\' AND all numbers are less than 4.\r\n"\
		"// <Point>\r\n"\
		"// num[0] == num[1]) | (num[1] == num[2]) | (num[0] == num[2]\r\n"\
		"// In case that any of two numbers are same.\r\n"\
		"// In such case, the other number must be XOR of the three numbers.\r\n"\
		"/////////////////////////////////////////////////////////////////\r\n";
*/
	int Num_OR = num[0] | num[1] | num[2];
	int Num_AND = num[0] & num[1] & num[2];
	int Num_XOR = num[0] ^ num[1] ^ num[2];
	int Num_EQ = (num[0] == num[1]) | (num[1] == num[2]) | (num[0] == num[2]);
/* 開発時のテスト用
	wsprintf(buff, "num[0]= %d, num[1] = %d, num[2] = %d\r\n", num[0], num[1], num[2]);
	str = str + buff;
	wsprintf(buff, "num[0] | num[1] | num[2] = %d\r\n", Num_OR);
	str = str + buff;
	wsprintf(buff, "num[0] & num[1] & num[2] = %d\r\n", Num_AND);
	str = str + buff;
	wsprintf(buff, "num[0] ^ num[1] ^ num[2] = %d\r\n", Num_XOR);
	str = str + buff;
	wsprintf(buff, "(num[0] == num[1]) | (num[1] == num[2]) | (num[0] == num[2]) = %d\r\n", Num_EQ);
	str = str + buff;
	SendItemMsg(IDC_EDIT, WM_SETTEXT, 0, (LPARAM)str.ToChar());
*/
	if(Num_OR == Num_AND) {					//アラシの場合
		if(g_JAPANESE)
			wsprintf(buff, "アラシが出ました！親の総取りです。\r\n");
		else
			wsprintf(buff, "Storm's here! You, Dealer's got everything!\r\n");
		m_Times = 3;						//初期化される
	}
	else if(Num_XOR == 7 && Num_AND == 4) {	//456の場合
		if(g_JAPANESE)
			wsprintf(buff, "456が出ました！親の総取りです。\r\n");
		else
			wsprintf(buff, "456's here! You, Dealer's got everything!\r\n");
		m_Times = 3;						//初期化される
	}
	else if(Num_XOR == 0 && Num_OR == 3) {	//123の場合
		if(g_JAPANESE)
			wsprintf(buff, "なんと、123です！倍付け配等で親落ちです。\r\n");
		else
			wsprintf(buff, "My God, 123's here! Gotta pay doubled bets and quit Dealer!\r\n");
		m_Times = 0;						//親落ち
	}
	else if(Num_EQ) {
		if(Num_XOR == 6) {					//目の場合
			if(g_JAPANESE)
				wsprintf(buff, "親に%dの目がつきました！親の総取りです。\r\n", Num_XOR);
			else
				wsprintf(buff, "You've got Point of %d! You, Dealer's got everything.\r\n", Num_XOR);
			m_Times = 3;					//初期化される

		}
		else if(Num_XOR == 1) {
			if(g_JAPANESE)
				wsprintf(buff, "なんと%dの目です！親の総付けです。\r\n", Num_XOR);
			else
				wsprintf(buff, "My God, You've got Point of %d! Gotta pay everyone.\r\n", Num_XOR);
			m_Times = 0;					//親落ち
		}
		else {
			if(g_JAPANESE)
				wsprintf(buff, "親に%dの目がつきました。\r\n", Num_XOR);
			else
				wsprintf(buff, "You've got Point of %d.\r\n", Num_XOR);
			m_Times = 3;					//初期化される
		}
	}
	else
		*buff = NULL;
	//目または役が出た場合、ステータスバーに表示
	SBar.SetText(1, buff);
	//最後に賽を振れる残回数を表示
	if(g_JAPANESE) {
		wsprintf(buff, Str_19, m_Times);
		str = str + buff + "\r\n";
	}
	else {
		wsprintf(buff, Str_09, m_Times);
		str = str + buff + "\r\n";
	}
	SendItemMsg(IDC_EDIT, WM_SETTEXT, 0, (LPARAM)str.ToChar());
	//終了チェック
	if(!m_Times) {
		if(g_JAPANESE) {
			if(MessageBox(m_hWnd, "あなたの負けです。まだ続けますか？", "勝負の終了",
							MB_YESNO | MB_ICONQUESTION) == IDYES)
				m_Times = 3;
			else
				OnIdok();
		}
		else {
			if(MessageBox(m_hWnd, "You lost. Wanna continue yet?", "Game over",
							MB_YESNO | MB_ICONQUESTION) == IDYES)
				m_Times = 3;
			else
				OnIdok();
		}
	}
	return TRUE;
}

bool CMyWnd::OnIdok() {

	SendMsg(WM_CLOSE, 0, 0);
	return TRUE;
}

bool CMyWnd::OnDice1(WPARAM wParam) {

	char buff[64], *cp;
	if(g_JAPANESE)
		cp = "%d番の賽が振られ、賽の目は%dでした。";
	else
		cp = "The dice of %d was rolled and made %d.";
	wsprintf(buff, cp, LOWORD(wParam) - IDC_D1 + 1, HIWORD(wParam));
	SBar.SetText(1, buff);
	return TRUE;
}

bool CMyWnd::OnDice2(WPARAM wParam) {

	char buff[64], *cp;
	if(g_JAPANESE)
		cp = "%d番の賽が振られ、賽の目は%dでした。";
	else
		cp = "The dice of %d was rolled and made %d.";
	wsprintf(buff, cp, LOWORD(wParam) - IDC_D1 + 1, HIWORD(wParam));
	SBar.SetText(1, buff);
	return TRUE;
}

bool CMyWnd::OnDice3(WPARAM wParam) {

	char buff[64], *cp;
	if(g_JAPANESE)
		cp = "%d番の賽が振られ、賽の目は%dでした。";
	else
		cp = "The dice of %d was rolled and made %d.";
	wsprintf(buff, cp, LOWORD(wParam) - IDC_D1 + 1, HIWORD(wParam));
	SBar.SetText(1, buff);
	return TRUE;
}

///////////////////////////////
//ユーザーダイアログの関数定義
//コントロール関数
///////////////////////////////
