#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
/// Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
/// Folger 04/10/09 QA80-13262-P2 OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
***************************************************************************************/

#define MIN_SMOOTHING_FACTOR 0

#define STR_ERR_THROW _L("Smoothing Factor")



void bspline(const vector& ix, const XYRange& iy, const double& sf, vector& ox, vector& coef)
{
	if(sf < 0)
		XF_THROW_EX2(CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE, STR_ERR_THROW, MIN_SMOOTHING_FACTOR);
	
	vector vxIn, vyIn, vyOut, vCof;
	int npts, nSize;
	//------ Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	//nSize = interpolation_preprocess(ix, iy, vxIn, vyIn, npts, vyOut, INTERP_TYPE_BSPLINE, vCof);
	vector<int>		vnMissingValsIndices;
	nSize = interpolation_preprocess(ix, iy, vxIn, vyIn, npts, vyOut, INTERP_TYPE_BSPLINE, vCof, DRR_BAD_WEIGHT_TREATMENT, &vnMissingValsIndices);
	//------
	
	DWORD dwCntrl = OCMATH_SRCDATA_X_MONOTONIC;
	int nResult = ocmath_interpolate(ix, vyOut, npts, vxIn, vyIn, nSize, INTERP_TYPE_BSPLINE, sf, NULL, vCof, dwCntrl);
	
	//------ Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	vCof.InsertValue(vnMissingValsIndices, NANUM);
	//------
	
	if( nResult != OE_NOERROR )
		XF_THROW(XFERR_BSPLINE_INTERP_FAILED);
	
	if(coef)
		coef = vCof;
	
	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 bspline_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return bspline_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//if(GETNE_ON_INIT == nEventID)
		//bOKEnable = _init_tree_show(trGetN);
	//
	//bOKEnable = update_xvector_show(trGetN);
	
	///------ Folger 04/10/09 QA80-13262-P2 OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
	if ( GETNE_ON_THEME == nEventID )
		check_convert_optional_outputs_to_new(trGetN);
	///------ End OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
	
	/// Cloud 07/23/07 GET_SMOOTH_FACTOR_AUTOMATICALLY
	bool bChangeSF = lstrcmpi(lpcszNodeName, "sf")==0 ? true : false;
	if (GETNE_ON_INIT==nEventID || GETNE_ON_THEME==nEventID || bChangeSF || lstrcmpi(lpcszNodeName, "method")==0 || lstrcmpi(lpcszNodeName, "iy")==0)
	{
		TreeNode trsf = trGetN.sf;
		/// Max 3/19/08 REWRITE_AND_RENAME_GET_SMOOTH_FACTOR
		//if (1==octree_get_auto_support(&trsf))
		if (trsf.IsValid() && 1==octree_get_auto_support(&trsf))
		{
			//get_smooth_factor(trGetN);
			trsf.dVal = get_smooth_factor_for_bspline(trGetN.iy);
		/// END REWRITE_AND_RENAME_GET_SMOOTH_FACTOR
		}
	}
	/// End GET_SMOOTH_FACTOR_AUTOMATICALLY
	
	int nErr = CER_NO_ERROR;
	
	//------ Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	TreeNode	trix = trGetN.ix;
	vector		vx;
	//------
	
	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)
	else if ( !okxf_resolve_tree_get_data_into_vector(&trix, &vx) || vx.GetSize()<=0 )
		nErr = CER_FFT_GET_INPUT_FAIL;
	//------
	
	else if(trGetN.sf.dVal < 0)
		nErr = CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE;
	
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
		
		if(trGetN.sf.dVal < 0)
		{
			strErrMsg += ":";
			strErrMsg += STR_ERR_THROW;
			strErrMsg += "|";
			strErrMsg += MIN_SMOOTHING_FACTOR;
		}		
	}
	
	return false;
	

}

int bspline_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 bspline_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	/// Cloud 07/23/07 GET_SMOOTH_FACTOR_AUTOMATICALLY
	TreeNode trsf = trGetN.sf;
	/// Max 3/19/08 REWRITE_AND_RENAME_GET_SMOOTH_FACTOR
	//if (1==octree_get_auto_support(&trsf))
	if (trsf.IsValid() && 1==octree_get_auto_support(&trsf))
	{
		//get_smooth_factor(trGetN);
		trsf.dVal = get_smooth_factor_for_bspline(trGetN.iy);
	/// END REWRITE_AND_RENAME_GET_SMOOTH_FACTOR
	}
	/// End GET_SMOOTH_FACTOR_AUTOMATICALLY
	
	///------ Folger 03/13/09 QA80-13262 CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
	if ( nGetNDialog > 0 )		/// open dialog
		check_convert_optional_outputs_to_new(trGetN);
	///------ End CONVERT_OBJECT_VARIABLE_OPTIOANL_TO_NEW_IN_XF_DIALOG
	
	//	if(0 == nGetNDialog || -1 == nGetNDialog)
	//		update_xvector_show(trGetN);	
	
}
//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 bspline_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
