#include <windows.h>
#include <math.h>

HBRUSH wypelnienie;
HPEN pen;
int x=3;
int y=4;

void Rysuj (HWND hwnd){ //tworzy szachownicę
	HDC hdc = GetDC( hwnd );
	wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( 0x00FF00 ) );
	pen =( HPEN ) SelectObject( hdc, CreatePen( PS_NULL, 1, RGB( 255, 0, 0 ) ) );
			for (int i=0;i<8;i++) {
				for (int j=0;j<8;j++) {
					if (floor((i+j)/2)*2==(i+j)) 
						wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( 0x00FF00 ) );
					else	
						wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( 0xff0000 ) );
					Rectangle( hdc, i*50, j*50, i*50+50, j*50+50 );
					}
				}
	wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( 0x0000FF ) );
	Ellipse(hdc,x*50, y*50, x*50+50, y*50+50);
}
void CzyscPole(HWND hwnd){  //czyści pole na szachownicy
	HDC hdc = GetDC( hwnd );
	pen =( HPEN ) SelectObject( hdc, CreatePen( PS_NULL, 1, RGB( 255, 0, 0 ) ) );
	if (floor((x+y)/2)*2==(x+y)) 
		wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( 0x00FF00 ) );
	else	
		wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( 0xFF0000 ) );
	Rectangle( hdc, x*50, y*50, x*50+50, y*50+50 );
}


/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	short color=0;
	HDC hdc = GetDC( hwnd );
	switch(Message) {
		case WM_CREATE:		//odświerzanie okna
		case WM_SHOWWINDOW: //odświerzanie okna
		case WM_ACTIVATE: 	//kiedy ALT+TAB
		case WM_SIZE:		//kiedy zmiana rozmiaru
		case WM_MOVE:		//kiedy przesunięcie okna
			{
			Rysuj(hwnd);
		 	break;
		}
		case WM_LBUTTONDOWN:  	//obsługa lewego przycisku myszy
			color=128;
		case WM_RBUTTONDOWN: { //obsługa prawego przycisku myszy
			if (color==0) color=255;
			int x_spr,y_spr; 
			x_spr = floor(LOWORD(lParam) /50);
    		y_spr = floor(HIWORD(lParam) /50);
			if ((x_spr<8)&&(y_spr<8)) {   //sprawdzenie czy kliknięcie na szachownicy
				CzyscPole(hwnd);			
				x=x_spr;
				y=y_spr;
				pen =( HPEN ) SelectObject( hdc, CreatePen( PS_NULL, 1, RGB( 255, 0, 0 ) ) );
				wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( RGB( 255, color, 0 )) );
				Ellipse(hdc,x*50, y*50, x*50+50, y*50+50);
				}
			break;
		}
		case WM_COMMAND: {  //obs-uga przycisków
			CzyscPole(hwnd);			
			switch( wParam ){
				case 101:  //symboliczny numer/identyfikator przycisku
					if (y!=0) y--;
					break;
				case 102:  //symboliczny numer/identyfikator przycisku
					if (y!=7) y++;
					break;
				case 103:  //symboliczny numer/identyfikator przycisku
					if (x!=0) x--;
					break;
				case 104:  //symboliczny numer/identyfikator przycisku
					if (x!=7) x++;
					break;
			}
			pen =( HPEN ) SelectObject( hdc, CreatePen( PS_NULL, 1, RGB( 255, 0, 0 ) ) );
			wypelnienie =( HBRUSH ) SelectObject( hdc, CreateSolidBrush( RGB( 255, 0, 0 )) );
			Ellipse(hdc,x*50, y*50, x*50+50, y*50+50);
			break;
		} 
		/* trap the WM_CLOSE (clicking X) message, and actually tell the window to close */
		case WM_CLOSE: {
			DestroyWindow(hwnd);
			break;
		}
		
		/* Upon destruction, tell the main thread to stop */
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG Msg; /* A temporary location for all messages */

	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,"WindowClass","Szachownica",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInstance,NULL);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	 CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "kliknij lewy lub prawy klawisz myszki         na szachownicy", WS_CHILD | WS_VISIBLE, 
		450, 100, 150, 60, hwnd, NULL, hInstance, NULL );
	HWND hGora = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " ^ ", WS_CHILD | WS_VISIBLE, 
		500, 200, 50, 50, hwnd, (HMENU)101, hInstance, NULL );
	HWND hDol = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " v ", WS_CHILD | WS_VISIBLE, 
		500, 300, 50, 50, hwnd, (HMENU)102, hInstance, NULL );
	HWND hLewo = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " < ", WS_CHILD | WS_VISIBLE, 
		450, 250, 50, 50, hwnd, (HMENU)103, hInstance, NULL );
	HWND hPrawo = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", " > ", WS_CHILD | WS_VISIBLE, 
		550, 250, 50, 50, hwnd, (HMENU)104, hInstance, NULL );
	Rysuj(hwnd);
	
	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produre unreasonably CPU usage
	*/
	while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&Msg); /* Translate keycodes to chars if present */
		DispatchMessage(&Msg); /* Send it to WndProc */
	}
	return Msg.wParam;
}
