
#include "stdafx.h"
#include "MultiZebrasDemo.h"
#include "MultiZebrasDemoDlg.h"
#include "FileOpenCombo.h"
#include "afxdialogex.h"

#include "AppRunTestStepRegister.h"

LPTSTR lpAnsi2lpWChar(LPSTR lpstr);
LPSTR  lpWChar2lpAnsi(LPTSTR lptstr);

#ifndef ZEBRAZ4U3DLLWIN32_EXPORTS
#include "ZebraZ4U3DllWin32.h"
#else
int getAliasRunStepsCntJustDownload(__HZHANDLE__ hZhandle);
char *get1stDownloadStepAliasName(__HZHANDLE__ hZhandle);
char *getNextDownloadStepAliasName(__HZHANDLE__ hZhandle);
int getRunStepIdFromDownloadAliasList(__HZHANDLE__ hZhandle, char *pAliasStepName);
int RunStepByAliasName(__HZHANDLE__ hZhandle, char *pAliasStepName, int nOption);

int getRegisterStepsCntJustDownload(__HZHANDLE__ hZhandle);
char *get1stDownloadRegisterStepName(__HZHANDLE__ hZhandle);
char *getNextDownloadRegisterStepName(__HZHANDLE__ hZhandle);
int getRunStepIdFromDownloadRegisterStepList(__HZHANDLE__ hZhandle, char *pRegisterStepName);
int RunTestStepFile(__HZHANDLE__ hZhandle, char *pTestStepName, int nOption);

BOOL KickoffRunStep(__HZHANDLE__ hZhandle, int iRunId, int ExAct, int nOption);

int getRunStepstate(__HZHANDLE__ hZhandle, PSRUNSTEPSTATE pRunStepState);
#endif

void RunTestStepOrRegister(void *pThis, __HZHANDLE__ hZhandle, int myId, int RunType, TCHAR *AliasTestRegisterName, int *imgsCnt, int *runStepId, int nStrmOpt)
{
	char *pszBuf;
	CMultiZebrasDemoDlg *pMain = (CMultiZebrasDemoDlg *)pThis;
	int nHadDownloadItems;

	if (RunType == __Run_Step_by_AliasName)
		nHadDownloadItems = getAliasRunStepsCntJustDownload(hZhandle);
	else if (RunType == __Run_Step_by_FileName)
		nHadDownloadItems = getRegisterStepsCntJustDownload(hZhandle);

	pMain->UpdateProgressMsg(myId, 10, NULL);
	// IDC_STC_RUN_STEP_ID
	if (nHadDownloadItems)
	{
		int iRunId;
		SRUNSTEPSTATE sRunResult;
		TCHAR szShowBuffer[16];

		pszBuf = lpWChar2lpAnsi(AliasTestRegisterName);
		if (RunType == __Run_Step_by_AliasName)
			iRunId = getRunStepIdFromDownloadAliasList(hZhandle, pszBuf);
		else if (RunType == __Run_Step_by_FileName)
			iRunId = getRunStepIdFromDownloadRegisterStepList(hZhandle, pszBuf);
		free(pszBuf);

		if (iRunId >= 0)
		{
			*runStepId = iRunId;
			swprintf(szShowBuffer, L"%d", iRunId);
			pMain->SetDlgItemText(IDC_STC_RUN_STEP_ID, szShowBuffer);

			pMain->UpdateProgressMsg(myId, 20, NULL);

			KickoffRunStep(hZhandle, iRunId, 0, nStrmOpt);
			// int RunStepByAliasName (__HZHANDLE__ hZhandle, char *pAliasStepName, int nOption);
			// int RunTestStepFile(__HZHANDLE__ hZhandle, char *pTestStepName, int nOption);

			pMain->UpdateProgressMsg(myId, 50, NULL);

			*imgsCnt = 0;
			if (getRunStepstate(hZhandle, &sRunResult))
			{
				if (sRunResult.runResultState == __Sta_step_snapping__ && sRunResult.resultType == __AppendData__ && sRunResult.appDataLen)
				{
					// snapping count
					swprintf(szShowBuffer, L"%d", *imgsCnt = sRunResult.appendBuffer[0]);
					pMain->SetDlgItemText(IDC_STC_RUN_IMG_CNT, szShowBuffer);
					pMain->SetDlgItemInt(IDC_EDT_GRAB_IMG_END, *imgsCnt - 1);
				}
				else if (sRunResult.runResultState == __Sta_step_succeed__ && sRunResult.resultType == __SnapPicture__)
				{
					swprintf(szShowBuffer, L"%d", *imgsCnt = sRunResult.snapCnt);
					pMain->SetDlgItemText(IDC_STC_RUN_IMG_CNT, szShowBuffer);
					pMain->SetDlgItemInt(IDC_EDT_GRAB_IMG_END, *imgsCnt - 1);
				}
				else if (sRunResult.runResultState == __Sta_step_succeed__ && sRunResult.resultType == __AppendData__)
				{
					if (sRunResult.appDataLen)
					{
						int i,k;
						TCHAR wzdatabuf[256 * 5];

						// dump as ansi string to TCHAR (wide-char) directly at first line
						for (k = 0; k < sRunResult.appDataLen; k++)
						{
							wzdatabuf[k] = sRunResult.appendBuffer[k];
						}
						// switch to second line
						wzdatabuf[k++] = '\r';
						wzdatabuf[k++] = '\n';
						wzdatabuf[k++] = 0;

						// dump data as hexdecimal at second line
						for (i = 0; i < sRunResult.appDataLen; i++)
						{
							k = wcslen(wzdatabuf);
							swprintf(wzdatabuf + k, L"0x%02x ", sRunResult.appendBuffer[i]);
						}

						pMain->MessageBox(wzdatabuf);
					}
				}
			}
		}
		else
		{
			pMain->UpdateProgressMsg(myId, -1, L"Selected test step isn't found");
		}

	}
	else
	{
		// no runable item message...
		pMain->UpdateProgressMsg(myId, -1, L"No runable test (register) found!");
	}
	pMain->UpdateProgressMsg(myId, 100, NULL);
}