#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

#include <event_utils.h>

//put your own support static functions here

///Kyle 07/05/2010 QA80-15092-P2  CHECK_INPUT_RANGE
static int _check_input_range(const TreeNode& trRange)
{
	if ( !trRange )
		return CER_INVALID_TREENODE;

	Range rData;
	okxf_resolve_tree_construct_range(&trRange, &rData);
	return _check_input_range(rData);
}

static int _check_input_range(const Range& rng)
{
	if( !rng )
		return XFERR_INVALID_RANGE;

	int nErrCode = check_1_column(rng, true);
	if( nErrCode != CER_NO_ERROR )
		return nErrCode;

	int nMinSize, nMaxSize;
	nErrCode = check_1_data(rng, true, false, &nMinSize, &nMaxSize);
	
	if( nErrCode != CER_NO_ERROR )
		return nErrCode;
	
	if( nMinSize == 0 )
		return CER_NO_DATA;

	return CER_NO_ERROR;
}
///End CHECK_INPUT_RANGE


void reflection(const Range& irng, const double& lower, const double& upper, Range& orng)
{
	if (irng == NULL)
	{
		XF_THROW(XFERR_NO_ACTIVE_COLUMN);
	}
	if (lower > upper)
	{
		XF_THROW(XFERR_LOW_LARGER_THAN_HEIGH);
	}
	///Kyle 07/05/2010 QA80-15092-P2  CHECK_INPUT_RANGE
	int nErrCode = _check_input_range(irng);
	if( nErrCode != CER_NO_ERROR )
	{
		if( CER_COL_NUM_TOO_FEW_EX == nErrCode )
		{
			XF_THROW_EX(nErrCode, 1);
		}
		else
		{
			XF_THROW(nErrCode);
		}
	}
	///End CHECK_INPUT_RANGE
	DWORD dwRules = DRR_NO_WEIGHTS | DRR_NO_FACTORS | DRR_GET_MISSING;
	int nNumRanges = irng.GetNumData(dwRules);
	for (int ii=0; ii<nNumRanges; ii++)
	{
		vector vInput, vOutput;
		int nDataCol = irng.GetData(dwRules, ii, NULL, NULL, &vInput, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
		double dMin, dMax;
		vInput.GetMinMax(dMin, dMax);
		vOutput = (vInput - dMin) * (upper - lower) / (dMax - dMin) + lower;
		if(!orng.SetColumnData(vOutput, true, ii)) return;
	}	
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int reflection_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return reflection_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErrCode = CER_NO_ERROR;
	///Kyle 07/05/2010 QA80-15092-P2 CHECK_INPUT_RANGE
	//nErrCode = check_1_column(trGetN.irng, true);
	nErrCode = _check_input_range(trGetN.irng);
	///End CHECK_INPUT_RANGE
	if (!check_min_max(trGetN.lower.dVal, trGetN.upper.dVal) )
	{
		if(CER_NO_ERROR == nErrCode)	nErrCode = CER_MIN_MAX;
	}
	if (nErrCode != CER_NO_ERROR)
	{
		strErrMsg = nErrCode;
		///Kyle 07/05/2010 QA80-15092-P2 CHECK_INPUT_RANGE
		if( CER_COL_NUM_TOO_FEW_EX == nErrCode )
			strErrMsg += ":" + (string)1;
		///End CHECK_INPUT_RANGE
		bOKEnable = false;
	}
	return true;

}

int reflection_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void reflection_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int reflection_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
