#include <windows.h>
#include <cstring>
#include <stdio.h>
#define PSWD_ENTRD 0

LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
void AddControls(HWND);
char* wchar_to_char(wchar_t*);
HWND hEdit;


int check_password(char *passwd){
	return 0;
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow){
	
	

	WNDCLASSW wc = {0};

	wc.hbrBackground= (HBRUSH)COLOR_WINDOW;
	wc.hCursor 		= LoadCursor(NULL,IDC_ARROW);
	wc.hInstance 	= hInst;
	wc.lpszClassName= L"myWindowClass";
	wc.lpfnWndProc	= WindowProcedure ;
	
	if (!RegisterClassW(&wc)){
		return -1;
	}

	CreateWindowW(L"myWIndowClass",L"Crackme",WS_OVERLAPPEDWINDOW | WS_VISIBLE,100,200,500,500,NULL,NULL,NULL,NULL );

	MSG msg = {0};
	
	while(GetMessage(&msg,NULL,NULL,NULL)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;

}


LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp){
	//MessageBox(NULL,"Crackme By X3eRo0","LMAO",MB_OK);
	
	switch(msg){
		case WM_COMMAND:
			switch(wp){
				case PSWD_ENTRD:
					wchar_t buff[100];
					GetWindowTextW(hEdit,buff,100);
					char * str = wchar_to_char(buff);
					if(check_password(str)){
						MessageBoxW(NULL,L"Wrong Password",L"LMAO",MB_OK);
					}else{
						MessageBoxW(NULL,L"Correct Password",L"LMAO",MB_OK);
					}
					break;

			}break;
		case WM_CREATE:
			AddControls(hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProcW(hWnd,msg,wp,lp);
	}
}

void AddControls(HWND hWnd){
	CreateWindowW(L"Static",L"Enter Password: ",WS_VISIBLE | WS_CHILD | SS_CENTER ,30,50,150,20,hWnd,NULL,NULL,NULL);
	hEdit = CreateWindowW(L"Edit",L"Password",WS_VISIBLE|WS_CHILD|WS_BORDER,190,50,200,20,hWnd,NULL,NULL,NULL);
	CreateWindowW(L"Button",L"LOGIN", WS_VISIBLE | WS_CHILD, 200,80,70,30,hWnd,NULL,NULL,NULL);
}

char* wchar_to_char(wchar_t* pwchar){
	int currentCharIndex = 0;
	char currentChar = pwchar[currentCharIndex];

	while (currentChar != '\0'){
		currentCharIndex++;
		currentChar = pwchar[currentCharIndex];
	}

	const int charCount = currentCharIndex + 1;
	char* char_str = (char*)malloc(sizeof(char) * charCount);
	for (int i = 0; i < charCount; i++){
		char character = pwchar[i];
		*char_str = character;
		char_str += sizeof(char);
	}
	char_str += '\0';
	char_str -= (sizeof(char) * charCount);
	return char_str;
}