#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <vector>
#include "Form.h"
#include "Stigma DLL.h"
using namespace std;


int numLuaStates = 0;
HWND MainWindowHWND;
HMENU hMenu;
HMENU hMenuPopupFile;
HMENU hMenuPopupExtras;
HMENU hMenuPopupAbout;
HMENU hMenuPopupLuaStateList;
HMODULE hInstance;
//HWND prnt_hWnd;
HWND ScriptTextBoxHWND;
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

bool BTC = false;

BOOL RegisterDLLWindowClass(wchar_t szClassName[])
{
	WNDCLASSEX wc;
	wc.hInstance = GetModuleHandle(NULL);
	wc.lpszClassName = (LPCWSTR)szClassName;
	wc.lpfnWndProc = DLLWindowProc;
	wc.style = CS_DBLCLKS;
	wc.cbSize = sizeof (WNDCLASSEX);
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszMenuName = L"Test";
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
	if (!RegisterClassEx(&wc))
		return 0;
}
void ShowForm() {
	hInstance = GetModuleHandle(NULL);
	MSG messages;
	hMenu = CreateDLLWindowMenu();
	RegisterDLLWindowClass(L"DLLWindowClass");
	//prnt_hWnd = FindWindow(NULL, L"ROBLOX");
	//MainWindowHWND = CreateWindowExW(0, L"DLLWindowClass", L"STIGMA_V2", WS_EX_PALETTEWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, prnt_hWnd, hMenu, hInstance, NULL);
	CreateFWindows();
	ShowWindow(MainWindowHWND, SW_SHOWNORMAL);
	while (GetMessage(&messages, NULL, 0, 0))
	{
		TranslateMessage(&messages);
		DispatchMessage(&messages);
	}
}

HMENU CreateDLLWindowMenu()
{
	HMENU heyMenu;
	heyMenu = CreateMenu();
	
	if (heyMenu == NULL)
		return FALSE;
	RefreshContextMenu(heyMenu);
	return heyMenu;
}
void RefreshContextMenu(HMENU hhMenu) {
	hMenuPopupFile = CreatePopupMenu();
	AppendMenu(hMenuPopupFile, MF_STRING, MYMENU_MINIMIZE, TEXT("Minimize"));
	AppendMenu(hMenuPopupFile, MF_STRING, MYMENU_EXIT, TEXT("Exit"));
	AppendMenuW(hhMenu, MF_POPUP, (UINT_PTR)hMenuPopupFile, TEXT("File"));

	hMenuPopupExtras = CreatePopupMenu();
	AppendMenu(hMenuPopupExtras, MF_STRING, MYMENU_TPH, TEXT("Teleport Handler"));
	AppendMenu(hMenuPopupExtras, MF_UNCHECKED, MYMENU_TOGGLE_TEAMCHANGING,TEXT("Block Team Changing"));
	AppendMenu(hMenuPopupExtras, MF_POPUP, MYMENU_TAKEDMG_MOD, TEXT("TakeDamage Mods"));
	AppendMenu(hMenuPopupExtras, MF_UNCHECKED, MYMENU_TAKEDMG_OP, TEXT("TakeDamage OP"));
	AppendMenu(hMenuPopupExtras, MF_UNCHECKED, MYMENU_TAKEDMG_NERF, TEXT("TakeDamage NERF"));
	AppendMenu(hhMenu,MF_POPUP, (UINT_PTR)hMenuPopupExtras, TEXT("Extras"));

	hMenuPopupLuaStateList = CreatePopupMenu();
	AppendMenu(hMenuPopupLuaStateList, MF_STRING, MYMENU_LSRefresh,TEXT("Refresh"));
	AppendMenu(hhMenu, MF_POPUP, (UINT_PTR)hMenuPopupLuaStateList, TEXT("Lua"));

	hMenuPopupAbout = CreatePopupMenu();
	AppendMenu(hMenuPopupAbout, MF_STRING, MYMENU_ABOUTMB, TEXT("About"));
	AppendMenu(hhMenu, MF_POPUP, (UINT_PTR)hMenuPopupAbout, TEXT("Help"));

	
}

void CreateFWindows() {
	//Execute BUTTON
	CreateWindow(L"BUTTON", L"EXE", WS_CHILD | WS_VISIBLE, 350, 0, 45, 150, MainWindowHWND, (HMENU)MYMENU_EXECUTECODE, hInstance, NULL);
	//Clear BUTTON
	CreateWindow(L"BUTTON", L"CLEAR", WS_CHILD | WS_VISIBLE, 350, 100, 45, 150, MainWindowHWND, (HMENU)MYMENU_CLEARSCRIPT, hInstance, NULL);
	//SCRIPT TEXTBOX
	ScriptTextBoxHWND = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"", WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_BORDER | WS_VSCROLL, 0, 0, 350, 250, MainWindowHWND, (HMENU)MYMENU_SCRIPTTEXTBOX, hInstance, 0);
	SendMessage(ScriptTextBoxHWND,EM_SETLIMITTEXT,0x7FFFFFFE,0);
	//TAKEDMG CHECKBOX
}

LRESULT CALLBACK DLLWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_COMMAND:
		switch (wParam)
		{
		case MYMENU_EXIT:
			if(MessageBoxA(0,"Are you sure you want to close stigma?","wtf r u doin",MB_YESNO) == IDYES)
				SendMessage(hwnd, WM_CLOSE, 0, 0);
			break;
		case MYMENU_MINIMIZE:
			ShowWindow(hwnd,SW_MINIMIZE);
			break;
		case MYMENU_ABOUTMB:
			MessageBox(hwnd, L"Created by momof513", L"About", MB_OK);
			break;
		case MYMENU_CLEARSCRIPT:
			SetDlgItemText(hwnd, MYMENU_SCRIPTTEXTBOX, L"");
			break;
		case MYMENU_EXECUTECODE:
			int length;
			length = SendMessage(ScriptTextBoxHWND, WM_GETTEXTLENGTH, 0, 0);
			if (length == -1)
				break;
			char buff[80896]; // = 1024 * 79
			char len[255];
			_itoa_s(length, len, 10);
			GetDlgItemTextA(hwnd, MYMENU_SCRIPTTEXTBOX, buff, length+1);
			ExecuteCode(buff);
			break;
		case MYMENU_TOGGLE_TEAMCHANGING:
			//RemoveMenu(hMenuPopupExtras,0,MF_BYPOSITION); // get rid of the old one
			BTC = !BTC;
			if(BTC)
			{
				CheckMenuItem(hMenuPopupExtras,MYMENU_TOGGLE_TEAMCHANGING,MF_CHECKED);
			}
			else
			{
				CheckMenuItem(hMenuPopupExtras,MYMENU_TOGGLE_TEAMCHANGING,MF_UNCHECKED);
			}
			break;
		case MYMENU_TAKEDMG_OP:
			bool isChecked1;
			isChecked1 = ( GetMenuState(hMenuPopupExtras,MYMENU_TAKEDMG_OP,MF_CHECKED) == 0x8 );
			if(!isChecked1) {
				TakeDamageType = 1;
				CheckMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_OP,MF_CHECKED);
				EnableMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_NERF,MF_DISABLED);
			}
			else {
				TakeDamageType = 0;
				CheckMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_OP,MF_UNCHECKED);
				EnableMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_NERF,MF_ENABLED);
			}
			break;
		case MYMENU_TAKEDMG_NERF:
			bool isChecked2;
			isChecked2 = ( GetMenuState(hMenuPopupExtras,MYMENU_TAKEDMG_NERF,MF_CHECKED) == 0x8 );
			if(!isChecked2) {
				TakeDamageType = 2;
				CheckMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_NERF,MF_CHECKED);
				EnableMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_OP,MF_DISABLED);
			}
			else {
				TakeDamageType = 0;
				CheckMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_NERF,MF_UNCHECKED);
				EnableMenuItem(hMenuPopupExtras,MYMENU_TAKEDMG_OP,MF_ENABLED);
			}
			break;
		case MYMENU_LSRefresh: {
				//RemoveMenu(hMenuPopupExtras,0,MF_BYPOSITION);
				for(;numLuaStates>0;numLuaStates--) {
					RemoveMenu(hMenuPopupLuaStateList,numLuaStates,MF_BYPOSITION); // Removes all menu things
				}
				vector<int> LS;
				LS = GetLuaStates();
				for (vector<int>::iterator it = LS.begin();it != LS.end();++it) {
					char STR[255] = "0x";
					char strbuffer[255];
					_itoa_s(*it,strbuffer,16);
					strcat_s(STR,strbuffer);
					AppendMenuA(hMenuPopupLuaStateList,MF_STRING, (WM_APP + 800) + numLuaStates,STR);
					numLuaStates++;
				}
				break;
			}
		case MYMENU_TPH:
			{
				TeleportUpdater();
				break;
			}
		}
		break;
		
		if(wParam > (WM_APP + 800)) {
			//int ls = Read
		}
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}