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

#include "AppLoadRegisterFiles.h"


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

#ifndef ZEBRAZ4U3DLLWIN32_EXPORTS
#include "ZebraZ4U3DllWin32.h"
#else
typedef  void(*LOG_HOOK_FUNC)(int hZhandle, char *pszLogMsg, unsigned long logCbArg);
extern int LoadRegisterFiles(__HZHANDLE__ hZhandle, char *pRegFilesDir, LOG_HOOK_FUNC log_cbFunc, unsigned long logCbArg);
#endif

static CMultiZebrasDemoDlg *pMain = NULL;

void LoadRegisterStatusCb(int hZhandle, char *pLogMsg, unsigned long logCbArg)
{
	LPTSTR pzwMsg;
	int cbMyId = (int)logCbArg;

	pMain->UpdateProgressMsg(cbMyId, pMain->m_StatusPercent[cbMyId], pzwMsg = lpAnsi2lpWChar(pLogMsg));
	pMain->m_StatusPercent[cbMyId] += 4;
	free(pzwMsg);
}

void LoadTestStepsOrRegisterFilesToZebra(void *pThis, __HZHANDLE__ hZhandle, int myId, TCHAR *pDownLoadName)
{
	//*CMultiZebrasDemoDlg *pMain = (CMultiZebrasDemoDlg *)pThis;
	char *pszBuf;

	// protect by thread!
	pMain = (CMultiZebrasDemoDlg *)pThis;

	// Check load configuration (steps configuration) is setting!
	if (wcslen(pDownLoadName) == 0)
	{
		pMain->OnBnClickedBtnTestCfg();
		return;
	}

	if (hZhandle < 0) // Check Zebra is open!
	{
		pMain->UpdateProgressMsg(myId, -1, L"Zebra hasn't open!");
		return;
	}

	// Converts to ansi string
	pszBuf = lpWChar2lpAnsi(pDownLoadName);

	pMain->UpdateProgressMsg(myId, 0, NULL);
	pMain->m_StatusPercent[myId] = 5;

	LoadRegisterFiles(hZhandle, pszBuf, LoadRegisterStatusCb, (unsigned long)myId);
	
	pMain->UpdateProgressMsg(myId, 100, L"load completed");
	free(pszBuf);
}