#pragma once
#include "VPcmdIF.h"
#include "VPif.h"
//extern void		DisplayError(int32_t err, const char* szMsg);
//extern vpcmd_context g_ctx;
//extern vpdev_hnd     g_hnd;
bool CalDisablePrompt(int& frames, CFrameRateCfg fr, eCmdActions& eAct)
{
	TCHAR szInputRange[60];
	_stprintf_s(szInputRange, 60, _T("(0-%6d)"), frames);
	tcout << _T("\r\nDisable Cal Command: Frame rate = ") << CVPcmd::frameratestr(fr.frame_rate) << endl;
	tcout << _T("You may disable the cal cycle for up to one minute, up to ") << dec << frames << _T(" frames.") << endl;
	tcout << _T(" ENTER, ESC : Cancel operation.\n");
	tcout << _T(" ") << szInputRange << _T(" : Frames to disable cal. 0 to reset the Disabled Cal state.");
	int32_t ncount = 0;
	bool bEnter = false;
	bool bChar = false;
	bool bSelectionMade = false;
	bool bRet = false;
	eAct = CMD_ACTION_SET;
	tcout << _T("\n ") << szInputRange << _T("/ESC >> ");
	while (!bSelectionMade)
	{
		bEnter = false;

		if (inintesc(ncount, bEnter, bChar, _T("")))
		{
			if (bChar)
			{
				eAct = CMD_ACTION_RESET;
				bSelectionMade = true;
				bRet = true;
			}
			else if ((ncount >= 0) && (ncount <= frames))
			{
				frames = ncount;
				if (ncount == 0)
					tcout << _T("\n You typed zero.  Disabled Cal state will be cleared.");
				bRet = true;
				bSelectionMade = true;
			}
			else
			{
				tcout << _T("\n out of range!\r\n");
				tcout << _T("\n ") << szInputRange << _T("/Rr/ESC >> ");
			}
		}
		else
		{
			tcout << _T("\n Canceling operation.");
			bRet = false;
			bSelectionMade = true;
		}
	}

	tcout << endl << endl;
	return bRet; // abort
}

void DoCalDisableTest()
{
	// first get frame rate
	CFrameRateCfg fcfg;
	int r = 0;
	r = vpcmd_dev_framerate(g_ctx, g_hnd, CMD_ACTION_GET, fcfg);
	DisplayError(r, "vpcmd_dev_framerate CMD_ACTION_GET");

	uint32_t fps = fcfg.EffectiveRate();
	int32_t maxdisabled = fps * 60; // max 1 minute worth of frames
	int32_t frames = maxdisabled;
	eCmdActions eact = CMD_ACTION_SET;
	if (CalDisablePrompt(frames, fcfg, eact))
	{
		r = vpcmd_dev_generic(g_ctx, g_hnd, eact, CMD_DISABLE_CAL, &frames, sizeof(int32_t), 0, 0);
		std::string s("vpcmd_dev_generic CMD_DISABLE_CAL ");
		s += CVPcmd::actstr(eact);
		DisplayError(r, s.c_str());
	}
}
