/******************************************************************
 *						Author:	Zhou Yundong
 *						Time: 20070401 12:44
 ******************************************************************/
#include "StdAfx.h"
#include <commdlg.h>
#include "Sample.h"
#include "resource.h"
#include "time.h"
#include "Think.h"

void CMainWin::OnPaint()
{
	CPaintDC dc(this);
	ViewImage.Copy(&Back);
	ShowWhiteShading();
	ShowExtraShading();
	ShowLeftBoard();
	ShowRightBoard();
	ShowLed();
	ShowGreyWord();
	ShowCountQuestion();
	ShowSawedge();
	ShowDottedLine();
	ShowCover();
	ShowEditBox();
	ShowSum();
	ShowGlideParebox();
	ShowGlideKilparebox();
	ShowPare();
	ShowKillerPare();
	ViewImage.Draw(dc, dc.ps.rcPaint);
}

void CMainWin::ShowLeftBoard()
{
	int i, j;
	for(i=0; i<9; i++)
	{
		for(j=0; j<9; j++)
		{
			if(leftBoard[i+mult9[j]] != 0)
			{
				if(fixation[i+mult9[j]])
				{
					SpriteNumber.Set(&SpriteImageNumbers, CPoint(20+mult49[i], 19+mult49[j]),
						CPoint((leftBoard[i+mult9[j]]-1)*11, 13));
					SpriteNumber.Draw(&ViewImage);
				}
				else
				{
					SpriteNumber.Set(&SpriteImageNumbers, CPoint(20+mult49[i], 19+mult49[j]),
						CPoint((leftBoard[i+mult9[j]]-1)*11, 0));
					SpriteNumber.Draw(&ViewImage);
				}
			}
		}
	}
}

void CMainWin::ShowRightBoard()
{
	int i, j;
	for(i=0; i<9; i++)
	{
		for(j=0; j<9; j++)
		{
			if(rightBoard[i+mult9[j]] != 0)
			{
				if(rightStatus == 1 && oldFix[i+mult9[j]])
				{
					SpriteNumber.Set(&SpriteImageNumbers, CPoint(491+mult49[i], 19+mult49[j]),
						CPoint((rightBoard[i+mult9[j]]-1)*11, 13));
				}
				else
				{
					SpriteNumber.Set(&SpriteImageNumbers, CPoint(491+mult49[i], 19+mult49[j]),
						CPoint((rightBoard[i+mult9[j]]-1)*11, 0));
				}
				SpriteNumber.Draw(&ViewImage);
			}
		}
	}
}

void CMainWin::ShowGreyWord()
{
	if(underThinking || !autoSolute)
	{
		{
			CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 57), CPoint(0, 0),
				CSize(27, 27));
			SpriteNumber.Draw(&ViewImage);
		}
		{
			CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 169), CPoint(27, 0),
				CSize(27, 27));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	if(underThinking)
	{
		{
			CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 99), CPoint(81, 0),
				CSize(27, 27));
			SpriteNumber.Draw(&ViewImage);
		}
		{
			CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 127), CPoint(108, 0),
				CSize(27, 27));
			SpriteNumber.Draw(&ViewImage);
		}
		{
			CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 328), CPoint(243, 0),
				CSize(27, 27));
			SpriteNumber.Draw(&ViewImage);
		}
		{
			CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 375), CPoint(270, 0),
				CSize(27, 27));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	if(autoSolute)
	{
		CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 231), CPoint(54, 0),
			CSize(27, 27));
		SpriteNumber.Draw(&ViewImage);
	}
	if(editKind == 1)
	{
		CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 280), CPoint(135, 0),
			CSize(27, 27));
		SpriteNumber.Draw(&ViewImage);
	}
	else if(editKind == 2)
	{
		CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 280), CPoint(162, 0),
			CSize(27, 27));
		SpriteNumber.Draw(&ViewImage);
	}
	else if(editKind == 3)
	{
		CSprite SpriteNumber(&SpriteImageGrayWord, CPoint(443, 280), CPoint(189, 0),
			CSize(27, 27));
		SpriteNumber.Draw(&ViewImage);
	}
}

void CMainWin::ShowCountQuestion()
{
	int digit2, digit1;
	if(rightStatus == 3 && !underThinking)
	{
		digit2 = countQuestion / 10;
		digit1 = countQuestion % 10;
		{
			CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(449, 210), CPoint(7*digit2, 0),
				CSize(7, 15));
			SpriteNumber.Draw(&ViewImage);
		}
		{
			CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(457, 210), CPoint(7*digit1, 0),
				CSize(7, 15));
			SpriteNumber.Draw(&ViewImage);
		}
	}
}

void CMainWin::ShowLed()
{
	if(rightStatus == 4)
	{
		CSprite SpriteNumber(&SpriteImageLeds, CPoint(450, 10), CPoint(39, 0), CSize(13, 15));
		SpriteNumber.Draw(&ViewImage);
	}
	else if(rightStatus != 2 && !underThinking)
	{
		if(flagAnswer == 0)
		{
			CSprite SpriteNumber(&SpriteImageLeds, CPoint(450, 10), CPoint(0, 0), CSize(13, 15));
			SpriteNumber.Draw(&ViewImage);
		}
		else if(flagAnswer == 1)
		{
			CSprite SpriteNumber(&SpriteImageLeds, CPoint(450, 10), CPoint(13, 0), CSize(13, 15));
			SpriteNumber.Draw(&ViewImage);
		}
		else if(flagAnswer == 2)
		{
			CSprite SpriteNumber(&SpriteImageLeds, CPoint(450, 10), CPoint(26, 0), CSize(13, 15));
			SpriteNumber.Draw(&ViewImage);
		}
	}
}

void CMainWin::ShowDottedLine()
{
	int spilth;
	int i, j;
	// 已知点序列endPoints[]
	for(i=0; i<pCircle; i++)
	{
		spilth = 0;
		for(j=0; j<pEndPoints[i]-1; j++)
		{
			ViewImage.DrawDottedLine(endPoints[i][j], endPoints[i][j+1], spilth, colorGroup[i]);
		}
	}
	for(i=0; i<pRightCircle; i++)
	{
		spilth = 0;
		for(j=0; j<pRightEndPoints[i]-1; j++)
		{
			ViewImage.DrawDottedLine(rightEndPoints[i][j], rightEndPoints[i][j+1], spilth, rightColorGroup[i]);
		}
	}
}

void CMainWin::ShowSum()
{
	int i;
	int digit2, digit1;
	for(i=0; i<81; i++)
	{
		if(leftUniq[i])
		{
			if(leftSum[i] == 0)
			{
				continue;
			}
			digit2 = leftSum[i] / 10;
			digit1 = leftSum[i] % 10;
			// 分成一位数和两位数
			if(digit2 == 0)
			{
				CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(mult49[mod9[i]]+10, mult49[divide9[i]]+2),
					CPoint(7*digit1, 0), CSize(7, 15));
				SpriteNumber.Draw(&ViewImage);
			}
			else
			{
				{
					CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(mult49[mod9[i]]+7, mult49[divide9[i]]+2),
						CPoint(7*digit2, 0), CSize(7, 15));
					SpriteNumber.Draw(&ViewImage);
				}
				{
					CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(mult49[mod9[i]]+13, mult49[divide9[i]]+2),
						CPoint(7*digit1, 0), CSize(7, 15));
					SpriteNumber.Draw(&ViewImage);
				}
			}
		}
	}
	for(i=0; i<81; i++)
	{
		if(rightUniq[i])
		{
			if(rightSum[i] == 0)
			{
				continue;
			}
			digit2 = rightSum[i] / 10;
			digit1 = rightSum[i] % 10;
			// 分成一位数和两位数
			if(digit2 == 0)
			{
				CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(mult49[mod9[i]]+10+471, mult49[divide9[i]]+2),
					CPoint(7*digit1, 0), CSize(7, 15));
				SpriteNumber.Draw(&ViewImage);
			}
			else
			{
				{
					CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(mult49[mod9[i]]+7+471, mult49[divide9[i]]+2),
						CPoint(7*digit2, 0), CSize(7, 15));
					SpriteNumber.Draw(&ViewImage);
				}
				{
					CSprite SpriteNumber(&SpriteImageLittleNumber, CPoint(mult49[mod9[i]]+13+471, mult49[divide9[i]]+2),
						CPoint(7*digit1, 0), CSize(7, 15));
					SpriteNumber.Draw(&ViewImage);
				}
			}
		}
	}
}

void CMainWin::ShowCover()
{
	int i;
	if(gameKind == 1)
	{
		for(i=0; i<81; i++)
		{
			if(leftUniq[i] && leftSum[i] != 0)
			{
				CSprite SpriteNumber(&SpriteImageCover, CPoint(mult49[mod9[i]]+7, mult49[divide9[i]]+5),
					CPoint(13*squareNoTbl[i]+13, 9), CSize(13, 9));
				SpriteNumber.Draw(&ViewImage);
			}
		}
	}
	else if(gameKind == 3)
	{
		for(i=0; i<81; i++)
		{
			if(leftUniq[i] && leftSum[i] != 0)
			{
				if(leftExtra[i] == selectExtra && selectExtra != 0)
				{
					CSprite SpriteNumber(&SpriteImageCover, CPoint(mult49[mod9[i]]+7, mult49[divide9[i]]+5),
						CPoint(143, 0), CSize(13, 9));
					SpriteNumber.Draw(&ViewImage);
				}
				else
				{
					CSprite SpriteNumber(&SpriteImageCover, CPoint(mult49[mod9[i]]+7, mult49[divide9[i]]+5),
						CPoint(13*leftExtra[i]+13, 0), CSize(13, 9));
					SpriteNumber.Draw(&ViewImage);
				}
			}
		}
	}
	if(gameKindRight == 1)
	{
		for(i=0; i<81; i++)
		{
			if(rightUniq[i] && rightSum[i] != 0)
			{
				CSprite SpriteNumber(&SpriteImageCover, CPoint(mult49[mod9[i]]+7+471, mult49[divide9[i]]+5),
					CPoint(13*squareNoTbl[i]+13, 9), CSize(13, 9));
				SpriteNumber.Draw(&ViewImage);
			}
		}
	}
	else if(gameKindRight == 3)
	{
		for(i=0; i<81; i++)
		{
			if(rightUniq[i] && rightSum[i] != 0)
			{
				CSprite SpriteNumber(&SpriteImageCover, CPoint(mult49[mod9[i]]+7+471, mult49[divide9[i]]+5),
					CPoint(13*rightExtra[i]+13, 0), CSize(13, 9));
				SpriteNumber.Draw(&ViewImage);
			}
		}
	}
}

void CMainWin::ShowEditBox()
{
	if(underSumEditing)
	{
		if(CreateCaret(hWnd, NULL, 1, 9))
		{
			SetCaretPos(mult49[mod9[editingHead]]+7, mult49[divide9[editingHead]]+5);
			ShowCaret(hWnd);
		}
		CSprite SpriteNumber(&SpriteImageCover, CPoint(mult49[mod9[editingHead]]+7, mult49[divide9[editingHead]]+5),
			CPoint(0, 0), CSize(13, 9));
		SpriteNumber.Draw(&ViewImage);
	}
}

void CMainWin::ShowPare()
{
	int i;
	int px, py;
	// 画垂直的大于小于号
	for(i=0; i<72; i++)
	{
		if(leftPare[i] == 1 || leftPare[i] == 2)
		{
			px = 46 + mult49[i%8];
			py = 15 + mult49[i/8];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((leftPare[i]-1)*7, 7), CSize(7, 19));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	// 画水平的大于小于号
	for(i=0; i<72; i++)
	{
		if(leftPare[i+72] == 1 || leftPare[i+72] == 2)
		{
			px = 15 + mult49[i%9];
			py = 46 + mult49[i/9];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((leftPare[i+72]-1)*19, 0), CSize(19, 7));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	// 画垂直的大于小于号
	for(i=0; i<72; i++)
	{
		if(rightPare[i] == 1 || rightPare[i] == 2)
		{
			px = 46 + mult49[i%8] + 471;
			py = 15 + mult49[i/8];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((rightPare[i]-1)*7, 7), CSize(7, 19));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	// 画水平的大于小于号
	for(i=0; i<72; i++)
	{
		if(rightPare[i+72] == 1 || rightPare[i+72] == 2)
		{
			px = 15 + mult49[i%9] + 471;
			py = 46 + mult49[i/9];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((rightPare[i+72]-1)*19, 0), CSize(19, 7));
			SpriteNumber.Draw(&ViewImage);
		}
	}
}

void CMainWin::ShowKillerPare()
{
	int i;
	int px, py;
	// 画垂直的大于小于号
	for(i=0; i<72; i++)
	{
		if(leftKilpare[i] == 1 || leftKilpare[i] == 2)
		{
			px = 46 + mult49[i%8];
			py = 8 + mult49[i/8];
			if(leftKilpare[i] == 2)
			{
				px -= 2;
			}
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((leftKilpare[i]-1)*9, 35), CSize(9, 33));
			SpriteNumber.Draw(&ViewImage);
		}
		else if(leftKilpare[i] == 3)
		{
			px = 45 + mult49[i%8];
			py = 8 + mult49[i/8];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint(42, 44), CSize(9, 33));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	// 画水平的大于小于号
	for(i=0; i<72; i++)
	{
		if(leftKilpare[i+72] == 1 || leftKilpare[i+72] == 2)
		{
			px = 8 + mult49[i%9];
			py = 46 + mult49[i/9];
			if(leftKilpare[i+72] == 2)
			{
				py -= 2;
			}
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((leftKilpare[i+72]-1)*33, 26), CSize(33, 9));
			SpriteNumber.Draw(&ViewImage);
		}
		else if(leftKilpare[i+72] == 3)
		{
			px = 8 + mult49[i%9];
			py = 45 + mult49[i/9];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint(33, 77), CSize(33, 9));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	// 画垂直的大于小于号
	for(i=0; i<72; i++)
	{
		if(rightKilpare[i] == 1 || rightKilpare[i] == 2)
		{
			px = 46 + mult49[i%8] + 471;
			py = 8 + mult49[i/8];
			if(leftKilpare[i] == 2)
			{
				px -= 2;
			}
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((rightKilpare[i]-1)*9, 35), CSize(9, 33));
			SpriteNumber.Draw(&ViewImage);
		}
		else if(rightKilpare[i] == 3)
		{
			px = 45 + mult49[i%8] + 471;
			py = 8 + mult49[i/8];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint(42, 44), CSize(9, 33));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	// 画水平的大于小于号
	for(i=0; i<72; i++)
	{
		if(rightKilpare[i+72] == 1 || rightKilpare[i+72] == 2)
		{
			px = 8 + mult49[i%9] + 471;
			py = 46 + mult49[i/9];
			if(leftKilpare[i+72] == 2)
			{
				py -= 2;
			}
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint((rightKilpare[i+72]-1)*33, 26), CSize(33, 9));
			SpriteNumber.Draw(&ViewImage);
		}
		else if(rightKilpare[i+72] == 3)
		{
			px = 8 + mult49[i%9] + 471;
			py = 45 + mult49[i/9];
			CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
				CPoint(33, 77), CSize(33, 9));
			SpriteNumber.Draw(&ViewImage);
		}
	}
}

void CMainWin::ShowGlideParebox()
{
	int px, py;
	if(glidingPare < 72)
	{
		px = 46 + mult49[glidingPare%8];
		py = 15 + mult49[glidingPare/8];
		CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
			CPoint(38, 7), CSize(7, 19));
		SpriteNumber.Draw(&ViewImage);
	}
	else if(glidingPare < 144)
	{
		px = 15 + mult49[(glidingPare-72)%9];
		py = 46 + mult49[(glidingPare-72)/9];
		CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
			CPoint(38, 0), CSize(19, 7));
		SpriteNumber.Draw(&ViewImage);
	}
}

void CMainWin::ShowGlideKilparebox()
{
	int px, py;
	if(glidingKilpare < 72)
	{
		px = 45 + mult49[glidingKilpare%8];
		py = 8 + mult49[glidingKilpare/8];
		CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
			CPoint(33, 44), CSize(9, 33));
		SpriteNumber.Draw(&ViewImage);
	}
	else if(glidingKilpare < 144)
	{
		px = 8 + mult49[(glidingKilpare-72)%9];
		py = 45 + mult49[(glidingKilpare-72)/9];
		CSprite SpriteNumber(&SpriteImageCompare, CPoint(px, py),
			CPoint(33, 35), CSize(33, 9));
		SpriteNumber.Draw(&ViewImage);
	}
}

void CMainWin::ShowSawedge()
{
	int i;
	int index;
	if(gameKind == 4 || gameKind == 3)
	{
		for(i=0; i<81; i++)
		{
			if((index=colorSaw[i][0]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(1+mult49[i%9], 1+mult49[i/9]),
					CPoint(0, index*2), CSize(48, 2));
				SpriteNumber.Draw(&ViewImage);
			}
			if((index=colorSaw[i][1]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(47+mult49[i%9], 1+mult49[i/9]),
					CPoint(index*2, 20), CSize(2, 48));
				SpriteNumber.Draw(&ViewImage);
			}
			if((index=colorSaw[i][2]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(1+mult49[i%9], 47+mult49[i/9]),
					CPoint(0, index*2), CSize(48, 2));
				SpriteNumber.Draw(&ViewImage);
			}
			if((index=colorSaw[i][3]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(1+mult49[i%9], 1+mult49[i/9]),
					CPoint(index*2, 20), CSize(2, 48));
				SpriteNumber.Draw(&ViewImage);
			}
		}
	}
	if(rightKind == 1)
	{
		for(i=0; i<81; i++)
		{
			if((index=colorSawRight[i][0]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(1+mult49[i%9]+471, 1+mult49[i/9]),
					CPoint(0, index*2), CSize(48, 2));
				SpriteNumber.Draw(&ViewImage);
			}
			if((index=colorSawRight[i][1]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(47+mult49[i%9]+471, 1+mult49[i/9]),
					CPoint(index*2, 20), CSize(2, 48));
				SpriteNumber.Draw(&ViewImage);
			}
			if((index=colorSawRight[i][2]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(1+mult49[i%9]+471, 47+mult49[i/9]),
					CPoint(0, index*2), CSize(48, 2));
				SpriteNumber.Draw(&ViewImage);
			}
			if((index=colorSawRight[i][3]) != 10)
			{
				CSprite SpriteNumber(&SpriteImageSawedge, CPoint(1+mult49[i%9]+471, 1+mult49[i/9]),
					CPoint(index*2, 20), CSize(2, 48));
				SpriteNumber.Draw(&ViewImage);
			}
		}
	}
}

void CMainWin::ShowExtraShading()
{
	int i;
	if(gameKind == 5 || gameKind == 3)
	{
		for(i=0; i<81; i++)
		{
			if(leftExtra[i] != 0)
			{
				if(leftExtra[i] == selectExtra)
				{
					CSprite SpriteNumber(&SpriteImageExtra, CPoint(1+mult49[i%9], 1+mult49[i/9]),
						CPoint(432, 0), CSize(48, 48));
					SpriteNumber.Draw(&ViewImage);
				}
				else
				{
					CSprite SpriteNumber(&SpriteImageExtra, CPoint(1+mult49[i%9], 1+mult49[i/9]),
						CPoint(48*(leftExtra[i]-1), 0), CSize(48, 48));
					SpriteNumber.Draw(&ViewImage);
				}
			}
		}
	}
	for(i=0; i<81; i++)
	{
		if(rightExtra[i] != 0)
		{
			CSprite SpriteNumber(&SpriteImageExtra, CPoint(1+mult49[i%9]+471, 1+mult49[i/9]),
				CPoint(48*(rightExtra[i]-1), 0), CSize(48, 48));
			SpriteNumber.Draw(&ViewImage);
		}
	}
}

void CMainWin::ShowWhiteShading()
{
	int i;
	if(gameKind == 4 || gameKind == 5 || gameKind == 3)
	{
		for(i=0; i<81; i++)
		{
			CSprite SpriteNumber(&SpriteImageExtra, CPoint(1+mult49[i%9], 1+mult49[i/9]),
				CPoint(480, 0), CSize(48, 48));
			SpriteNumber.Draw(&ViewImage);
		}
	}
	if(gameKindRight == 4 || gameKindRight == 5 || gameKindRight == 3)
	{
		for(i=0; i<81; i++)
		{
			CSprite SpriteNumber(&SpriteImageExtra, CPoint(1+mult49[i%9]+471, 1+mult49[i/9]),
				CPoint(480, 0), CSize(48, 48));
			SpriteNumber.Draw(&ViewImage);
		}
	}
}