#include "ProcessList.h"

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

//-------------------------------------------------------------------------------
// デストラクタ
CProcessList::~CProcessList(void)
{
	SAFE_DELETEA(m_Param);
}

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

	return true;
}

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

	if (++ m_CurCount   < m_Param[m_CurDate].count  ) return true;
	m_CurCount   = 0;

	if (++ m_CurSubject < m_Param[m_CurDate].subject) return true;
	m_CurSubject = 0;
	m_FirstTOD   = true;

	if (++ m_CurTOD     < m_Param[m_CurDate].tod    ) return true;
	m_CurTOD     = 0;

	if (++ m_CurDate    < m_NumParam                ) return true;
	m_EndList    = true;

	return false;
}

//-------------------------------------------------------------------------------
// フレーム画像のパスを返す
bool CProcessList::GetFrameDir(char *pathStr)
{
	const char *TOD[3] = TOD_STRING;

	if (m_Param[m_CurDate].dateStr[4] == '-')
	{
		sprintf_s(pathStr, PATH_LEN, "%s\\%s\\%s\\S%02d_C%02d", 
			m_Param[m_CurDate].baseDir, m_Param[m_CurDate].dateStr, 
			TOD[m_CurTOD], m_CurSubject + 1, m_CurCount + 1);
	}
	else
	{
		sprintf_s(pathStr, PATH_LEN, "%s\\%s\\%s%d-%d", 
			m_Param[m_CurDate].baseDir, m_Param[m_CurDate].dateStr, 
			TOD[m_CurTOD], m_CurSubject + 1, m_CurCount + 1);
	}

	return true;
}

//-------------------------------------------------------------------------------
// 参照画像のパスを返す
bool CProcessList::GetRefDir(char *pathStr)
{
	const char *TOD[3] = TOD_STRING;


	if (m_Param[m_CurDate].dateStr[4] == '-')
	{
		sprintf_s(pathStr, PATH_LEN, "%s\\%s\\%s", 
			m_Param[m_CurDate].baseDir, m_Param[m_CurDate].dateStr, TOD[m_CurTOD]);
	}
	else
	{
		sprintf_s(pathStr, PATH_LEN, "%s\\%s\\macbeth_%s", 
			m_Param[m_CurDate].baseDir, m_Param[m_CurDate].dateStr, TOD[m_CurTOD]);
	}

	return true;
}

//-------------------------------------------------------------------------------
// 処理リストの読み込み
bool CProcessList::ReadFile()
{
	// 処理リストファイルを開く
	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;
	while (!feof(fp))
	{
		fgets(buffer, sizeof(buffer), fp);

		// コメントやコマンドは数えない
		if (!strncmp(buffer, "//", 2))			continue;
		if (!strncmp(buffer, "BASEDIR=", 8))	continue;
		if (!strncmp(buffer, "IMGFILE=", 8))	continue;
		if (!strncmp(buffer, "REFFILE=", 8))	continue;
		if (!strncmp(buffer, "END", 3))	break;

		Param p;
		sscanf_s(buffer, "%s %d %d %d", 
			p.dateStr, DATE_STRING, &p.tod, &p.subject, &p.count);
		if (p.tod > 0) num ++;
	}
	if (num < 1) ERROR_RET("No parameter data is found in file.");
	m_NumParam = num;
//	SPRLOG "Parameter list: %d", m_numParam); WriteLog(true);

	// 全処理リストを読み込み
	SAFE_DELETEA(m_Param);
	m_Param = new Param [m_NumParam];
	int i = 0;
	char curBaseDir[PATH_LEN] = DEFAULT_BASEDIR;
	char curImgFile[PATH_LEN] = DEFAULT_IMGFILE;
	char curRefFile[PATH_LEN] = DEFAULT_REFFILE;
	fseek(fp, 0, SEEK_SET);
	while (!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, "IMGFILE=", 8) 
			|| !strncmp(buffer, "REFFILE=", 8))
		{
			char *pt = buffer + 8;
			GTrimStr(&pt);
			switch (*buffer)
			{
			case 'B': strcpy_s(curBaseDir, PATH_LEN, pt); break;
			case 'I': strcpy_s(curImgFile, PATH_LEN, pt); break;
			case 'R': strcpy_s(curRefFile, PATH_LEN, pt); break;
			}
			continue;
		}

		sscanf_s(buffer, "%s %d %d %d", 
			m_Param[i].dateStr, DATE_STRING, 
			&m_Param[i].tod, &m_Param[i].subject, &m_Param[i].count);
		strcpy_s(m_Param[i].baseDir, PATH_LEN, curBaseDir);
		strcpy_s(m_Param[i].imgFile, PATH_LEN, curImgFile);
		strcpy_s(m_Param[i].refFile, PATH_LEN, curRefFile);
		if (m_Param[i].tod > 0) i ++;
	}

	// ファイルを閉じる
	fclose(fp);

	return true;
}

//-------------------------------------------------------------------------------
// CSV形式パラメータ文字列を作成
bool CProcessList::MakeParamStr(char *buffer, int len)
{
	const char *TOD[3] = TOD_STRING;

	sprintf_s(buffer, len, "%s, %s, %d, %d", 
		m_Param[m_CurDate].dateStr, TOD[m_CurTOD], 
		m_CurSubject + 1, m_CurCount + 1);

	return true;
}
