#include<iostream>
#include<regex>//正则表达式
#include<fstream>
#include <windows.h>
using namespace std;
#include<vector>
vector<string> v;

string xiaoyaoip = "192.168.31.109";
string xiaoyaoport = "5555";
void console_input(string ip, string port, string cmd_console) {
	string cmd = "adb -s " + ip + ":" + port + " " + cmd_console;
	const char* sql1 = cmd.c_str();
	system(sql1);

}
	
void click(string ip, string port,int x, int y)
{
	string cmd = "adb -s " + ip + ":" + port +" shell input tap "+ to_string(x)+" "+ to_string(y);
	const char* sql1 = cmd.c_str();
	system(sql1);
}
string UTF8ToGB(const char* str)
{
	string result;
	WCHAR* strSrc;
	LPSTR szRes;
	//获得临时变量的大小
	int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	strSrc = new WCHAR[i + 1];
	MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);

	//获得临时变量的大小
	i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
	szRes = new CHAR[i + 1];
	WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);

	result = szRes;
	delete[]strSrc;
	delete[]szRes;

	return result;
}

//读取文件内容 
string readfile(string filepath)
{
	ifstream in;
	in.open(filepath);
	string s;
	string s1;
	while (std::getline(in, s))
	{
		string str = UTF8ToGB(s.c_str()).c_str();	//解决中文乱码
		s1 += str;
		s1 += "\n";
	}
	in.close();
	return s1;
}


void re_math(string text, string re_text)
{
	v.clear();
	std::regex reg(re_text);
	std::string content(text);
	std::smatch m;
	auto pos = content.cbegin();
	auto end = content.cend();
	for (; std::regex_search(pos, end, m, reg); pos = m.suffix().first)
	{
		for (int i = 0; i < m.size(); ++i) {
			cout << "m.str(" << i << "): " << m.str(i) << endl;
			v.push_back(m.str(i));
		}
	}
	
}
//判断是否连接上
bool isconnect()
{
    system("adb devices>tmp.txt"); //读取cmd内容写入txt

	string sget_tr1 = readfile("tmp.txt");  // 读取txt内容

	//cout << sget_tr1 << endl;
	re_math(sget_tr1, xiaoyaoip+":\\d+.*?device");
	
	
	if (v.size() >0) {
		cout << "匹配成功l,大小为:"  << v.size() << endl;
		for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
			cout << *it << endl;

		}
		return true;
	}

	else {
		cout << "匹配失败" << endl;
		
		return false;
	}

	
}

void click_elem(string text)
//点击元素
{
	console_input(xiaoyaoip, xiaoyaoport, "shell \"uiautomator dump --compressed && cat /sdcard/window_dump.xml\">tmp2.txt");
	//system("adb shell uiautomator dump --compressed  /data/local/tmp/uidump.xml");
	//console_input(xiaoyaoip, xiaoyaoport,  "shell cat /sdcard/uidump.xml>tmp2.txt");

	string url = readfile("tmp2.txt");
	//cout <<"文本" <<url << endl;

	re_math(url, text+".*?bounds=\"\\[(\\d+),(\\d+)\\]\\[(\\d+),(\\d+)\\]\"");
	if (v.size() > 0) {
		cout << "匹配成功,大小为:" << v.size() << endl;
		//for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
		//	//cout << *it << endl;
		//}
		char* end;
		int x = (static_cast<int>(strtol(v[1].c_str(), &end, 10)) + static_cast<int>(strtol(v[3].c_str(), &end, 10))) / 2;
		int y = (static_cast<int>(strtol(v[2].c_str(), &end, 10)) + static_cast<int>(strtol(v[4].c_str(), &end, 10))) / 2;
		cout << text+"->找到坐标:x=" << x << ",y=" << y << endl;
		click(xiaoyaoip, xiaoyaoport, x, y);

	}
	else {
		cout << "匹配失败,没找到坐标" << endl;

	}

}
int main()
{
	//cout<<is_not <<endl;
	while (true) {
		bool is_not = isconnect();
		if (is_not == 0)
		{
			cout << "没找到ip端口号,重新连接" << endl;
			string cmd = "adb connect " + xiaoyaoip + ":"+ xiaoyaoport +" >tmp.txt";
			const char* sql1 = cmd.c_str();
			system(sql1);
		}

		else
		{
			break;
		}
	}
	
	click_elem("text=\"米家\"");
	

	system("pause");

	return 0;
}