#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <event_utils.h>

/***************************************************************************************
/// Modification Log :
/// Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
***************************************************************************************/

#define MIN_OUTPUT_INTERPOLATION_POINTS 2

#define STR_ERR_THROW "Minimum interpolation ponits"


void interp1q(const vector& ix, const XYRange& iy, vector& ox)
{
	vector vxIn, vyIn, vyOut;
	
	int npts, nSize;
	
	nSize = interpolation_preprocess(ix, iy, vxIn, vyIn, npts, vyOut, INTERP_TYPE_LINEAR);
	
	DWORD dwCntrl = OCMATH_SRCDATA_X_MONOTONIC;
	
	int nResult = ocmath_interpolate(ix, vyOut, npts, vxIn, vyIn, vxIn.GetSize(), INTERP_TYPE_LINEAR, 0, NULL, NULL, dwCntrl);
	
	if( nResult != OE_NOERROR )
	{
		XF_THROW(XFERR_LINEAR_INTERP_FAILED);
	}
	
	if(ox)
		ox = vyOut;
	
}

//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 interp1q_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return interp1q_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	int nErr = CER_NO_ERROR;
	
	if(get_input_xyrange_num(trGetN.iy) != 1)
		nErr = CER_NOT_ONE_XYRANGE;
	
	else if(0>= get_input_xyrange_data_size(trGetN.iy))
		nErr = CER_NO_DATA;
	
	//------ Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	//else if(check_input_double_vector(trGetN.ix) != OE_NOERROR)
	TreeNode	trix = trGetN.ix;
	vector		vx;
	if ( nErr == CER_NO_ERROR && (!okxf_resolve_tree_get_data_into_vector(&trix, &vx) || vx.GetSize()<=0) )
		nErr = CER_FFT_GET_INPUT_FAIL;
	//------ End MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
		if(nErr == CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE)
		{
			strErrMsg += ":";
			strErrMsg += STR_ERR_THROW;
			strErrMsg += "|";
			strErrMsg += MIN_OUTPUT_INTERPOLATION_POINTS;
		}
	}
	
	return true;
	

}

int interp1q_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 interp1q_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 interp1q_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
