#include "k_util.h"
#define MAX_PRINTLINE_LENGTH 8000
#define MAX_PATH_LENGTH 250
#define ITK_err 919099
#define GUID_LEN 64
#include <objbase.h>

extern "C" int POM_AM__set_application_bypass(logical bypass);

void set_bypass(logical bypass)
{
	POM_AM__set_application_bypass(bypass);
}


int CreateTempFolder(string& dir_path) {
	char buf[GUID_LEN] = { 0 };
	GUID guid;

	time_t t;
	struct tm* lt;
	time(&t);//获取Unix时间戳。
	lt = localtime(&t);//转为时间结构。
	sprintf(buf, "%d%d%d%d%d%d", lt->tm_year, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
	Log("3.18buf：%s", buf);

	/*CoCreateGuid(&guid);
	sprintf(buf, "%08X%04X%04x%02X%02X%02X%02X%02X%02X%02X%02X",
		guid.Data1, guid.Data2, guid.Data3,
		guid.Data4[0], guid.Data4[1], guid.Data4[2],
		guid.Data4[3], guid.Data4[4], guid.Data4[5],
		guid.Data4[6], guid.Data4[7]);*/
	std::move(std::string(buf));
	string uuidStr;
	uuidStr.assign(buf);
	//uuidStr.erase(std::remove(uuidStr.begin(), uuidStr.end(), '-'), uuidStr.end());
	char logFileDir[MAX_PATH_LENGTH];
	memset(logFileDir, 0, sizeof(logFileDir));
	sprintf(logFileDir, "%s", getenv("TEMP"));
	strcat(logFileDir, "\\");
	strcat(logFileDir, uuidStr.c_str());
	struct stat info;
	if (stat(logFileDir, &info) != 0) {
		Log("创建临时目录: %s", logFileDir);
		mkdir(logFileDir);
	}
	if (stat(logFileDir, &info) != 0) {
		Log("创建临时目录失败");
		return ITK_err;
	}
	dir_path.assign(logFileDir);
	return ITK_ok;
}
int CreateTempFolder1(string& dir_path) {
	char buf[GUID_LEN] = { 0 };
	GUID guid;

	time_t t;
	struct tm* lt;
	time(&t);//获取Unix时间戳。
	lt = localtime(&t);//转为时间结构。
	sprintf(buf, "%d%d%d%d%d%d", lt->tm_year, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
	Log("3.18buf：%s", buf);
	//CoCreateGuid(&guid);
	/*sprintf(buf, "%08X%04X%04x%02X%02X%02X%02X%02X%02X%02X%02X",
		guid.Data1, guid.Data2, guid.Data3,
		guid.Data4[0], guid.Data4[1], guid.Data4[2],
		guid.Data4[3], guid.Data4[4], guid.Data4[5],
		guid.Data4[6], guid.Data4[7]);*/
	std::move(std::string(buf));
	string uuidStr;
	uuidStr.assign(buf);
	//uuidStr.erase(std::remove(uuidStr.begin(), uuidStr.end(), '-'), uuidStr.end());
	char logFileDir[MAX_PATH_LENGTH];
	memset(logFileDir, 0, sizeof(logFileDir));
	sprintf(logFileDir, "%s", getenv("TEMP"));
	strcat(logFileDir, "\\");
	strcat(logFileDir, uuidStr.c_str());
	struct stat info;
	if (stat(logFileDir, &info) != 0) {
		Log("创建临时目录: %s", logFileDir);
		mkdir(logFileDir);
	}
	if (stat(logFileDir, &info) != 0) {
		Log("创建临时目录失败");
		return ITK_err;
	}
	dir_path.assign(logFileDir);
	return ITK_ok;
}

char* U2G(const char* utf8)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}



char* G2U(const char* gb2312)
{
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}

bool isClass(tag_t class_id, char* className) {
	tag_t* pclass_ids = NULL;
	char* class_name = NULL;
	int pclass_count = 0, ifail = ITK_ok;
	ITKCALL(POM_name_of_class(class_id, &class_name));
	bool isType = false;
	if (strcmp(class_name, className) == 0) {
		isType = true;
	}
	DOFREE(class_name);
	if (isType) {
		return true;
	}
	ITKCALL(ifail = POM_superclasses_of_class(class_id, &pclass_count, &pclass_ids));
	for (int i = 0; i < pclass_count; i++) {
		isType = isClass(pclass_ids[i], className);
		if (isType) {
			return true;
		}
	}
	return false;
}

int GetDatasets(tag_t target_tag, string dataset_rel, string dataset_type, vector<tag_t>& dataset_tags) {
	int ifail = ITK_ok, child_cnt = 0;
	tag_t* child_tags = NULL;
	char* object_type = NULL, * object_string = NULL;;
	ITKCALL(ifail = AOM_ask_value_tags(target_tag, dataset_rel.c_str(), &child_cnt, &child_tags));
	if (ifail != ITK_ok) {
		return ifail;
	}
	for (int i = 0; i < child_cnt; i++) {
		DOFREE(object_string);
		DOFREE(object_type);
		tag_t child_tag = child_tags[i];
		ITKCALL(ifail = AOM_ask_value_string(child_tag, "object_string", &object_string));
		ITKCALL(ifail = WSOM_ask_object_type2(child_tag, &object_type));
		Log(">> %d. %s [%s]", i + 1, object_string, object_type);
		//if (dataset_type._Equal(object_type)) {
		if (dataset_type == (object_type)) {
			dataset_tags.push_back(child_tag);
		}
	}
	DOFREE(object_string);
	DOFREE(object_type);
	DOFREE(child_tags);
	return ifail;
}

bool isType(tag_t item, const char* type) {
	int ifail = ITK_ok;
	tag_t item_type = NULLTAG, target_type = NULLTAG;
	logical answer = false;
	ITKCALL(ifail = TCTYPE_ask_object_type(item, &item_type));
	if (ifail != ITK_ok) {
		return false;
	}
	ITKCALL(ifail = TCTYPE_ask_type(type, &target_type));
	if (ifail != ITK_ok) {
		return false;
	}
	ITKCALL(ifail = TCTYPE_is_type_of(item_type, target_type, &answer));
	if (ifail == ITK_ok) {
		return answer;
	}
	return false;
}


int WriteToFile(const char* file_path, const char* content) {
	ofstream file;
	file.open(file_path);
	file << content;
	file.close();
	/*FILE *file = fopen(file_path, "a");
	if (!file) {
		Log("打开文件失败：%s", file_path);
		return ITK_err;
	}
	Log("1");
	fprintf(file, content);
	Log("2");
	fflush(file);
	Log("3");
	fclose(file);
	Log("4");
	file = NULL;*/
	return ITK_ok;
}

void Log(const char* format, ...)
{
	va_list arg;
	char tmp[MAX_PRINTLINE_LENGTH];
	time_t now;
	struct tm* p;

	memset(tmp, 0, sizeof(tmp));
	va_start(arg, format);
	vsprintf(tmp, format, arg);
	va_end(arg);
	printf("%s\n", tmp);
	TC_write_syslog("%s\n", tmp);
}

void Splits(string strArg, string spliter, vector<string>& ans)
{
	ans.clear();
	size_t index0 = 0;
	string one_arg;
	if (strArg.find_first_not_of(' ') == string::npos)
		strArg = "";
	int len = spliter.size();
	while (strArg.size() > 0)
	{
		index0 = strArg.find(spliter);
		if (index0 != string::npos)
		{
			one_arg = strArg.substr(0, index0);
			strArg = strArg.substr(index0 + len);
			ans.push_back(one_arg);
			if (strArg.size() == 0) {
				ans.push_back(strArg);
			}
		}
		else
		{
			ans.push_back(strArg);
			break;
		}
	}
}