#include "ProcessList.h"

//-------------------------------------------------------------------------------
// コンストラクタ
CProcessList::CProcessList(void)
{
	m_Param = NULL;
	m_Find = INVALID_HANDLE_VALUE;
}

//-------------------------------------------------------------------------------
// デストラクタ
CProcessList::~CProcessList(void)
{
	SAFE_DELETEA(m_Param);
	if (m_Find != INVALID_HANDLE_VALUE) FindClose(m_Find);
}

//-------------------------------------------------------------------------------
// 処理リストの読み込み
bool CProcessList::Init()
{
	m_CurDate	= 0;
	m_FirstTOD	= true;
	m_EndList	= false;
	if (!this->ReadFile()) return false;

	return true;
}

//-------------------------------------------------------------------------------
// 次の処理対象に移動する
bool CProcessList::MoveNext()
{
	if (m_EndList) return false;

	m_FirstTOD	= false;
	if (this->FindNext()) return true;

	m_FirstTOD	= true;
	if (++ m_CurDate < m_NumParam) {
		this->FindFirst();
		return true;
	}
	m_EndList	= true;

	return false;
}

//-------------------------------------------------------------------------------
// フレーム画像のパスを返す
bool CProcessList::GetFrameDir(char *pathStr)
{
	sprintf_s(pathStr, PATH_LEN, "%s\\%s",
		m_Param[m_CurDate].fullDir, m_FindFile.cFileName);
	return true;
}

//-------------------------------------------------------------------------------
// 処理リストの読み込み
bool CProcessList::ReadFile()
{
	printf("ProcessList読み込み中\n");

	// 処理リストファイルを開く
	FILE *fp = NULL;
	fopen_s(&fp, PROCESS_LIST, "r");
	if (!fp) ERROR_RET("Can't open Process List file.");

	// 処理リストの件数取得
	char buffer[1024];
	int num = 0;
	char curBaseDir[PATH_LEN] = DEFAULT_BASEDIR;
	while (!feof(fp))
	{
		fgets(buffer, sizeof(buffer), fp);

		// コメントやコマンドは数えない
		if (!strncmp(buffer, "//", 2))			continue;
		if (!strncmp(buffer, "REFFILE=", 8))	continue;
		if (!strncmp(buffer, "END", 3))			break;
		if (!strncmp(buffer, "BASEDIR=", 8)) {
			char *pt = buffer + 8;
			GTrimStr(&pt);
			strcpy_s(curBaseDir, PATH_LEN, pt);
			continue;
		}

		char dirPath[PATH_LEN];
		char *pt = buffer;
		GTrimStr(&pt);
		sprintf_s(dirPath, PATH_LEN, "%s\\%s", curBaseDir, pt);
		if (PathFileExists(dirPath)) num ++;
	}
	if (num < 1) ERROR_RET("No parameter data is found in file.");
	m_NumParam = num;
	printf("処理日数 %d\n", num);
//	SPRLOG "Parameter list: %d", m_numParam); WriteLog(true);

	// 全処理リストを読み込み
	SAFE_DELETEA(m_Param);
	m_Param = new PParam [m_NumParam];
	fseek(fp, 0, SEEK_SET);
	strcpy_s(curBaseDir, PATH_LEN, DEFAULT_BASEDIR);
	char curRefFile[PATH_LEN] = DEFAULT_REFFILE;
	for (int i = 0; !feof(fp); )
	{
		fgets(buffer, sizeof(buffer), fp);

		if (!strncmp(buffer, "//", 2)) continue;	// コメント行
		if (!strncmp(buffer, "END", 3))	break;		// 終了コマンド

		// コマンド処理
		if (   !strncmp(buffer, "BASEDIR=", 8)
			|| !strncmp(buffer, "REFFILE=", 8)) {
			char *pt = buffer + 8;
			GTrimStr(&pt);
			switch (*buffer) {
			case 'B': strcpy_s(curBaseDir, PATH_LEN, pt); break;
			case 'R': strcpy_s(curRefFile, PATH_LEN, pt); break;
			}
			continue;
		}

		char dirPath[PATH_LEN];
		char *pt = buffer;
		GTrimStr(&pt);
		sprintf_s(dirPath, PATH_LEN, "%s\\%s", curBaseDir, pt);
		if (strlen(pt) > 0 && PathFileExists(dirPath)) {
			strcpy_s(m_Param[i].dateStr, DATE_STRING, pt);
			strcpy_s(m_Param[i].baseDir, PATH_LEN, curBaseDir);
			strcpy_s(m_Param[i].refFile, PATH_LEN, curRefFile);
			strcpy_s(m_Param[i].fullDir, PATH_LEN, dirPath);
			this->FindCalib(dirPath, m_Param[i].refFile, PATH_LEN);
			printf("%s : %s\n", m_Param[i].dateStr, m_Param[i].refFile);
			i ++;
		}
	}

	// ファイルを閉じる
	fclose(fp);
	printf("ProcessList読み込み終了\n\n");
	printf("解析開始\n\n");

	if (!this->FindFirst()) return false;

	return true;
}

void CProcessList::FindCalib(char *dir, char *ref, int refLen)
{
	HANDLE hFind;
	WIN32_FIND_DATA win32fd;
	char searchPath[PATH_LEN];
	sprintf_s(searchPath, PATH_LEN, "%s\\*", dir);
	hFind = FindFirstFile(searchPath, &win32fd);
	if (hFind == INVALID_HANDLE_VALUE) return;
	std::vector<String> refdirs;
	do {
		if (win32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			if (strstr(win32fd.cFileName, "校正") != NULL) refdirs.push_back(win32fd.cFileName);
		}
	} while (FindNextFile(hFind, &win32fd));
	FindClose(hFind);
	if (refdirs.size() < 1) return;

	String refdir = refdirs[0];
	for (UINT i=1; i < refdirs.size(); i ++) {
		if (refdirs[i] > refdir) refdir = refdirs[i];
	}
	char check[PATH_LEN];
	sprintf_s(check, PATH_LEN, "%s\\%s\\Shot0001.bmp", dir, refdir.c_str());
	if (!PathFileExists(check)) return;
	strcpy_s(ref, refLen, check);
}

//-------------------------------------------------------------------------------
// CSV形式パラメータ文字列を作成
bool CProcessList::MakeParamStr(char *buffer, int len)
{
	sprintf_s(buffer, len, "%s, %s", 
		m_Param[m_CurDate].dateStr, m_FindFile.cFileName);

	return true;
}

//-------------------------------------------------------------------------------
// 出力ファイル名用文字列を作成
bool CProcessList::MakeFileStr(char *buffer, int len)
{
	sprintf_s(buffer, len, "%s %s", 
		m_Param[m_CurDate].dateStr, m_FindFile.cFileName);

	return true;
}
//-------------------------------------------------------------------------------
// 最初のフォルダを探す
bool CProcessList::FindFirst()
{
	char searchstr[PATH_LEN];
	sprintf_s(searchstr, PATH_LEN, "%s\\*", m_Param[m_CurDate].fullDir);
		
	if (m_Find != INVALID_HANDLE_VALUE) FindClose(m_Find);
	m_Find = FindFirstFile(searchstr, &m_FindFile);
	if(m_Find == INVALID_HANDLE_VALUE) return false;

	char paramFile[PATH_LEN];
	do {
		if (m_FindFile.cFileName[0] == '.') continue; // ドットで始まる名称は無視
		sprintf_s(paramFile, PATH_LEN, "%s\\%s\\%s", 
			m_Param[m_CurDate].fullDir, m_FindFile.cFileName, PARAM_FILE);
		if (PathFileExists(paramFile)) {
			// データフォルダ発見
			printf("\n処理中　%s\n", paramFile);
			return true;
		}
	} while(FindNextFile(m_Find, &m_FindFile));

	FindClose(m_Find);
	m_Find = INVALID_HANDLE_VALUE;
	return false;
}

//-------------------------------------------------------------------------------
// 次のフォルダを探す
bool CProcessList::FindNext()
{
	char searchstr[PATH_LEN];
	sprintf_s(searchstr, PATH_LEN, "%s\\*", m_Param[m_CurDate].fullDir);
		
	char paramFile[PATH_LEN];
	while(FindNextFile(m_Find, &m_FindFile)) {
		if (m_FindFile.cFileName[0] == '.') continue; // ドットで始まる名称は無視
		sprintf_s(paramFile, PATH_LEN, "%s\\%s\\%s", 
			m_Param[m_CurDate].fullDir, m_FindFile.cFileName, PARAM_FILE);
		if (PathFileExists(paramFile)) {
			// データフォルダ発見
			printf("\n処理中　%s\n", paramFile);
			return true;
		}
	}

	FindClose(m_Find);
	m_Find = INVALID_HANDLE_VALUE;
	return false;
}
