#include "SendKeyborad.h"

#include <Windows.h>



//static map<string, int> funcKeyMap;
//static BOOL flag = true;
//static void initMap(){
//	funcKeyMap["space"] = VK_SPACE;
//	funcKeyMap["delete"] = VK_DELETE;
//	funcKeyMap["esc"] = VK_ESCAPE;
//
//	funcKeyMap["back"] = VK_BACK;
//	funcKeyMap["tab"] = VK_TAB;
//	funcKeyMap["enter"] = VK_RETURN;
//}

int getFunckey(string fString){
	const char* c = fString.c_str();

	if(strstr(c,"enter")!=NULL)
		return VK_RETURN;
	else if(strstr(c,"space")!=NULL)
		return VK_SPACE;
	else if(strstr(c,"del")!=NULL)
		return VK_DELETE;
	else if(strstr(c,"back")!=NULL)
		return VK_BACK;
	else if(strstr(c,"tab")!=NULL)
		return VK_TAB;
	else if(strstr(c,"esc")!=NULL)
		return VK_ESCAPE;
	else return NULL;
}

char* titleHandle(string type, const char* title){
	char str[100], str2[200];



	if("firefox"==type){
		sprintf(str, " - Mozilla Firefox");
	}else if("chrome"==(type)){
		sprintf(str, " - Google Chrome");
	}else if("maxthon"==(type)){
		sprintf(str, " - 傲游浏览器 3.0.22.2000");
	}else if("ie6"==type||"ie7"==type||"ie8"==type){
		sprintf(str, " - Microsoft Internet Explorer");
	}

	sprintf(str2, "%s%s", title, str);

	return str2;
}

HWND getHWND(string type, const char* title1){


	FILE *fp = fopen("d:\\4.txt","w+");


	if("firefox"==type){
		int nLenName = strlen(title1) + 1;
		LPTSTR pName = new char[nLenName];
		memset(pName, 0, nLenName);
		strcpy(pName, title1);

		char * windowName = new char[100];


		Utf8ToAnsi(pName,windowName,strlen(pName));
		HWND tab = FindWindow(0, titleHandle(type,title1));
		return tab;
	}else if("chrome"==type){
		HWND chrom = ::FindWindow("Chrome_WidgetWin_0",  titleHandle(type,title1));

		HWND shell = FindWindowEx(chrom, NULL, "Chrome_WidgetWin_0", NULL);
		HWND tab = FindWindowEx(shell, NULL, "Chrome_RenderWidgetHostHWND", NULL);

		fprintf(fp,"handler:%d,%d,%d,title:%s",chrom,shell,tab,title1);

		fclose(fp);
		return tab;
	}else if("safari"==type){
		HWND safari = FindWindow("{1C03B488-D53B-4a81-97F8-754559640193}", title1); 
		HWND shell = FindWindowEx(safari,NULL, "SearchableWebView",NULL);
		HWND tab = FindWindowEx(shell,NULL,"WebKit2WebViewWindowClass",NULL); 
		return tab;
	}else if("maxthon"==type){
		HWND ay = FindWindow("Maxthon3Cls_MainFrm",titleHandle(type,title1)); 
		HWND shell = FindWindowEx(ay,NULL, "Maxthon3Cls_BrowserView",NULL); 
		HWND tab = FindWindowEx(shell,NULL, "Maxthon3Cls_WebViewHost",NULL);
		return tab;
	}else if("ie6"==type){
		HWND ie0 = FindWindow("IEFrame",titleHandle(type,title1));
		HWND shell = FindWindowEx(ie0,NULL, "Shell DocObject View",NULL);
		HWND tab = FindWindowEx(shell,NULL, "Internet Explorer_Server",NULL);
		return tab;
	}else if("ie7"==type){
		HWND ie0 = ::FindWindow("IEFrame",titleHandle(type,title1));
		HWND tabWin = ::FindWindowEx(ie0,NULL,"TabWindowClass",NULL);
		HWND shell = ::FindWindowEx(tabWin,NULL, "Shell DocObject View",NULL);
		HWND tab = FindWindowEx(shell,NULL, "Internet Explorer_Server",NULL);
		return tab;
	}else if("ie8"==type){

		HWND ie0 = FindWindow("IEFrame",titleHandle(type,title1));
		HWND frametab = FindWindowEx(ie0,NULL, "Frame Tab",NULL);
		HWND tabWin = FindWindowEx(frametab,NULL,"TabWindowClass",NULL);
		HWND shell = FindWindowEx(tabWin,NULL, "Shell DocObject View",NULL);
		HWND tab = FindWindowEx(shell,NULL, "Internet Explorer_Server",NULL);

				fprintf(fp,"handler:%d,%d,%d",ie0,frametab,tabWin);
				fclose(fp);
		return tab;
	}else
		return NULL;
}


BOOL Utf8ToAnsi(LPCSTR lpcszStr, char* lpwszStr, DWORD dwSize)
{
	DWORD dwMinSize;
	WCHAR* strTmp ;
	dwMinSize = MultiByteToWideChar (CP_UTF8, 0, lpcszStr, -1, NULL, 0);
	strTmp = new WCHAR[dwMinSize];
	if(dwSize < dwMinSize)
	{
		return FALSE;
	}
	MultiByteToWideChar (CP_UTF8, 0, lpcszStr, -1, strTmp, dwMinSize); 

	int   targetLen=WideCharToMultiByte(CP_ACP,0,(LPWSTR)strTmp,-1,(char   *)lpwszStr,0,NULL,NULL); 
	WideCharToMultiByte(CP_ACP,0,(LPWSTR)strTmp,-1,(char   *)lpwszStr,targetLen,NULL,NULL); 
	return TRUE;
}



void SendKeyborad(string BrowserType, const char* WindowName,string funcKey,BYTE charKey)
{

	//initMap();

	HWND hbrowser = getHWND(BrowserType,WindowName);

	FILE *fp = fopen("d:\\1.txt","w+");

	HWND hnow = ::GetForegroundWindow();

	::AttachThreadInput(::GetCurrentThreadId(), ::GetWindowThreadProcessId(::GetForegroundWindow(), NULL), TRUE);

	::SetForegroundWindow(hbrowser);
	::SetFocus(hbrowser);
	Sleep(30);
	//ctrl组合键
	fprintf(fp,"%d\n",funcKey.length());
	for(int i=0;i<funcKey.length();i++)
			fprintf(fp,"%c,",funcKey[i]);
	if((funcKey.length()>=4)&&("ctrl"==funcKey.substr(0,4))){
fprintf(fp,"hello");
		keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0), 0 ,0);
		keybd_event(charKey,(BYTE)0, 0 ,0); //此处可以用  'A', (BYTE)65, 用'a'不起作用.
		Sleep(5);

		keybd_event(charKey,(BYTE)0, KEYEVENTF_KEYUP,0);
		keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0), KEYEVENTF_KEYUP,0);

	}else             //单键：如space、enter、delete等
	{
		//keybd_event(VK_RETURN, 0, 0,0);
		//keybd_event(VK_SPACE, 0, 0,0);
		//map<string,int>::iterator iter = funcKeyMap.find(funcKey);
		//Sleep(10);
		//keybd_event(iter->second, MapVirtualKey(iter->second,0), KEYEVENTF_EXTENDEDKEY,0);
		int vKey = getFunckey(funcKey);
		fprintf(fp,"%s,%d",strstr(funcKey.c_str(),"enter"),vKey);
		if(vKey!=NULL)
		keybd_event(vKey, MapVirtualKey(vKey,0), KEYEVENTF_EXTENDEDKEY,0);
			//fprintf(fp,"%d\n",iter->second);
//fprintf(fp,"hello1");
	}
	Sleep(30);

	::AttachThreadInput(::GetCurrentThreadId(), ::GetWindowThreadProcessId(::GetForegroundWindow(), NULL), FALSE);

	::SetFocus(hnow);
	::SetForegroundWindow(hnow);

	fclose(fp);
	//::SendMessage(hServer, WM_CHAR, 'A', 0);
}