#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	method_linear,
	method_spline,
	method_bspline,
};
enum{
	boundary_natural,
	boundary_notaknot,
};
#include <GetNBox.h> 
#include <event_utils.h>
#include <o8dlg.h>
//#include <..\originlab\XFunctionEx.h>

/***************************************************************************************
/// Modification Log :
/// Folger 07/23/08 BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS
/// 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
/// Folger 02/23/10 CLEAR_OLD_DYNAUSECHECK_STATUS_ON_THEME_CHANGE
***************************************************************************************/

#define MIN_SMOOTHING_FACTOR 0

#define STR_NODE_METHOD "method"

#define STR_ERR_THROW _L("Smoothing Factor")

#define STR_ATTRIB_OLD_CHK	"oldDynaCheck"///Cheney 2007-8-15 FIX_SOME_BU_OF_INTERP

static void _update_method_show(TreeNode& trGetN)
{
	switch(trGetN.method.nVal)
	{
	case INTERP_TYPE_LINEAR:
		trGetN.boundary.Show=false;	/// Max 4/24/08 QA70-11435 v8.0849 ADD_BOUNDARY_CONDITION_TO_MATCH_SPLINE_CONNECTION_PLOTTING
		trGetN.sf.Show = false;
		trGetN.coef.Show = false;
		break;
	case INTERP_TYPE_SPLINE:
		trGetN.boundary.Show=true;	/// Max 4/24/08 QA70-11435 v8.0849 ADD_BOUNDARY_CONDITION_TO_MATCH_SPLINE_CONNECTION_PLOTTING
		trGetN.sf.Show = false;
		trGetN.coef.Show = true;
		break;
	case INTERP_TYPE_BSPLINE:
		trGetN.boundary.Show=false;	/// Max 4/24/08 QA70-11435 v8.0849 ADD_BOUNDARY_CONDITION_TO_MATCH_SPLINE_CONNECTION_PLOTTING
		trGetN.sf.Show = true;		
		trGetN.coef.Show = true;
		break;
	default:
	}
}

/*
static bool _init_tree_show(TreeNode& trGetN)
{
	//GraphLayer gl = Project.ActiveLayer();
	//if(!gl.IsValid())
		//trGetN.color.Show = false;
	
	_update_method_show(trGetN);
	
	return update_xvector_show(trGetN);
}
*/
static int _check_pts_enough(const TreeNode& trGetN, int nSize)
{
	switch(trGetN.method.nVal)
	{
	case INTERP_TYPE_LINEAR:
		if(nSize < MIN_LINEAR_PTS)
			return MIN_LINEAR_PTS;
		break;
	case INTERP_TYPE_SPLINE:
		if(nSize < MIN_SPLINE_PTS)
			return MIN_SPLINE_PTS;
		break;
	case INTERP_TYPE_BSPLINE:
		if(nSize < MIN_BSPLINE_PTS)
			return MIN_BSPLINE_PTS;
		break;
	default:
	}
	return OE_NOERROR;
}

///Cheney 2007-6-7 ADD_AN_ERR_CHECK_FUNC
static int _check_err_condition(TreeNode& trGetN, int nSize)
{
	int nErr = CER_NO_ERROR;
	/// Cloud 6/12/2007 DEAL_WITH_MISSING_VALUE
	TreeNode trxi = trGetN.ix;
	vector vxi;
	/// End DEAL_WITH_MISSING_VALUE
	
	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;
	
	else if(_check_pts_enough(trGetN, nSize) != OE_NOERROR)
		nErr = CER_DATA_TOO_FEW;

	/// Cloud 6/12/2007 DEAL_WITH_MISSING_VALUE
	//else if(check_input_double_vector(trGetN.ix) != OE_NOERROR)
	else if (!okxf_resolve_tree_get_data_into_vector(&trxi, &vxi) || vxi.GetSize()<=0)
	/// End DEAL_WITH_MISSING_VALUE
		nErr = CER_FFT_GET_INPUT_FAIL;
	
	else if(trGetN.sf.dVal < 0)
		nErr = CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE;
	
	return nErr;
}
///end ADD_AN_ERR_CHECK_FUNC

///Cheney 2007-8-15 FIX_SOME_BU_OF_INTERP
static void _set_coef_disable_if_linear(TreeNode& trGetN)
{
	if(trGetN.method.nVal == INTERP_TYPE_LINEAR)
		trGetN.coef.SetAttribute( STR_ATTRIB_DYNACONTROL_USE_CHECK, false );
}

static void _update_coef_attrib(TreeNode& trGetN, LPCSTR lpcszAttrib1, LPCSTR lpcszAttrib2)
{
	TreeNode trCoef = trGetN.coef;
	int nVal = 0;
	trCoef.GetAttribute( lpcszAttrib1, nVal );
	trCoef.SetAttribute( lpcszAttrib2, nVal );
}

static void _update_coef_enable(TreeNode& trGetN)
{
	if(trGetN.method.nVal != INTERP_TYPE_LINEAR)
	{
		_update_coef_attrib(trGetN, STR_ATTRIB_OLD_CHK, STR_ATTRIB_DYNACONTROL_USE_CHECK);
		_update_coef_attrib(trGetN, STR_ATTRIB_DYNACONTROL_USE_CHECK, STR_ATTRIB_OLD_CHK);
	}
	else 
		trGetN.coef.SetAttribute( STR_ATTRIB_DYNACONTROL_USE_CHECK, false );
}
///end FIX_SOME_BU_OF_INTERP

//------ Folger 07/23/08 BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS
static	void	_set_output_range_comments(Range& oy, const XYRange& iy, int nMethod, vector& coef)
{
	vector<string>		vsMethods = {"Linear", "Spline", "B-Spline"};
	string	strComments;
	strComments.Format("%s interp of %s", vsMethods[nMethod], get_input_data_range_string(iy));
	oy.SetOutputComment(strComments);
	if ( coef )
		coef.SetOutputComment(strComments);
}
//------ End BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS


void interp1(const vector& ix, const XYRange& iy, const int& method, const int& boundary, const double& sf, vector& ox, vector& coef)
{
		
	if (method<INTERP_TYPE_LINEAR || method >INTERP_TYPE_BSPLINE)
		XF_THROW(XFERR_WRONG_PARAM_NUMBER);
	if (sf<0)
		XF_THROW_EX2(CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE, STR_ERR_THROW, MIN_SMOOTHING_FACTOR);
	
	int npts, nSize;
	vector vx, vy, vyout, vcof;
	//------ Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	//nSize = interpolation_preprocess(ix, iy, vx, vy, npts, vyout, method, vcof);
	vector<int>		vnMissingValsIndices;
	nSize = interpolation_preprocess(ix, iy, vx, vy, npts, vyout, method, vcof, DRR_BAD_WEIGHT_TREATMENT, &vnMissingValsIndices);
	//------ End MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	/// Max 4/24/08 QA70-11435 v8.0849 ADD_BOUNDARY_CONDITION_TO_MATCH_SPLINE_CONNECTION_PLOTTING
	//int nRet = ocmath_interpolate(ix, vyout, npts, vx, vy, nSize, method, sf, NULL, vcof);
	DWORD dwCntrl = OCMATH_SRCDATA_X_MONOTONIC;	// interpolation_preprocess() sorts the input	
	int nRet = ocmath_interpolate(ix, vyout, npts, vx, vy, nSize, method, sf, NULL, vcof, dwCntrl, boundary);
	/// END ADD_BOUNDARY_CONDITION_TO_MATCH_SPLINE_CONNECTION_PLOTTING
	
	//------ Folger 11/27/08 QA80-12356 v8.0980 MORE_TOOLS_NEED_TO_HANDLE_INPUTS_WITH_MISSING_VALUE
	vcof.InsertValue(vnMissingValsIndices, NANUM);
	//------
	
	if (nRet!=OE_NOERROR)
	{
		switch (method)
		{
		case INTERP_TYPE_LINEAR:
			XF_THROW(XFERR_LINEAR_INTERP_FAILED);
			break;
		case INTERP_TYPE_SPLINE:
			XF_THROW(XFERR_SPLINE_INTERP_FAILED);
			break;
		case INTERP_TYPE_BSPLINE:
			XF_THROW(XFERR_BSPLINE_INTERP_FAILED);
			break;
		}
	}
	
	if (ox)
		ox = vyout;
	///Cheney 2007-8-15 FIX_SOME_BU_OF_INTERP
	//if (coef)
	if (coef && method > INTERP_TYPE_LINEAR)
	///end FIX_SOME_BU_OF_INTERP
		coef = vcof;
	
	//------ Folger 07/23/08 BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS
	_set_output_range_comments(ox.GetSourceDataRange(), iy, method, coef);
	//------
	
}

//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 interp1_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return interp1_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	//if(GETNE_ON_INIT==nEventID || GETNE_ON_THEME==nEventID)
	//	bOKEnable = _init_tree_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);
		///------ Folger 02/23/10 CLEAR_OLD_DYNAUSECHECK_STATUS_ON_THEME_CHANGE
		trGetN.coef.RemoveAttribute(STR_ATTRIB_OLD_CHK);
		///------ End CLEAR_OLD_DYNAUSECHECK_STATUS_ON_THEME_CHANGE
	}
	///------ End OPTIONAL_SHOWN_INSTEAD_OF_NEW_AFTER_RESET_THEME
		
	bool bIsChangingData2 = lstrcmpi(lpcszNodeName,STR_NODE_METHOD) == 0? true:false;
	//bOKEnable = update_xvector_show(trGetN);
	
	if(GETNE_ON_INIT==nEventID || GETNE_ON_THEME==nEventID || bIsChangingData2)
		_update_method_show(trGetN);
	/// 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 nSize = get_input_xyrange_data_size(trGetN.iy);
	
	int nErr = CER_NO_ERROR;
	
	///Cheney 2007-6-7 ADD_AN_ERR_CHECK_FUNC
	//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;
	//
	//else if(_check_pts_enough(trGetN, nSize) != OE_NOERROR)
		//nErr = CER_DATA_TOO_FEW;
	//
	//else if(check_input_double_vector(trGetN.ix) != OE_NOERROR)
		//nErr = CER_FFT_GET_INPUT_FAIL;
	//
	//else if(trGetN.sf.dVal < 0)
		//nErr = CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE;
	nErr = _check_err_condition(trGetN, nSize);
	/////end ADD_AN_ERR_CHECK_FUNC
	
	///Cheney 2007-8-15 FIX_SOME_BU_OF_INTERP
	if( nEventID == GETNE_ON_INIT)
	{
		_update_coef_attrib(trGetN, STR_ATTRIB_DYNACONTROL_USE_CHECK, STR_ATTRIB_OLD_CHK);
		_set_coef_disable_if_linear(trGetN);
	}
	
	if(lstrcmpi(lpcszNodeName, "method")==0)
	{
		_update_coef_enable(trGetN);
	}
	
	if(lstrcmpi(lpcszNodeName, "coef")==0)
		_update_coef_attrib(trGetN, STR_ATTRIB_DYNACONTROL_USE_CHECK, STR_ATTRIB_OLD_CHK);
	///end FIX_SOME_BU_OF_INTERP
	
	if(nErr != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr;
		///Arvin 07/30/07 WRONG_ERR_MASSAGE
		//if(trGetN.sf.dVal < 0)
		if(nErr == CER_DATA_TOO_FEW)
		{
			int nPts = _check_pts_enough(trGetN, nSize);
			strErrMsg += ":";
			strErrMsg += nPts;
		}
		if(nErr == CER_INPUT_ARGUMENT_LESS_THAN_SPECIFIED_VALUE)
		///end WRONG_ERR_MASSAGE
		{
			strErrMsg += ":";
			strErrMsg += STR_ERR_THROW;
			strErrMsg += "|";
			strErrMsg += MIN_SMOOTHING_FACTOR;
		}			
	}
	
	return true;	
	

}

int interp1_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 interp1_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	//------ fisher 11/16/07 ADD_MAP_ID_TO_CHM
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_interp1);
	
	/*
	if(nGetNDialog >= XF_GETN_SIMPLE) // before dialog is open
	{
		XYRange xyTemp;
		if(xy_range_from_GetN_data_node(trGetN.iy, xyTemp))
			_check_reset_to_data_x_range(trGetN, xyTemp, true);
	}
	*/
	//	if(0 == nGetNDialog || -1 == nGetNDialog)
	//		update_xvector_show(trGetN);
	
	/// 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
	
	///Cheney 2007-6-7 ADD_AN_ERR_CHECK_FUNC
	///Cheney 2007-6-14 PROCEED_WITH_DLG_SHOULD_IF_ERR
	//if(0 == nGetNDialog && (dwCntrl&LTXF_THEME_USED) )
	if(0 == nGetNDialog && (dwCntrl&LTXF_THEME_USED) && (dwCntrl&LTXF_FROM_GUI_MENU))
	///end PROCEED_WITH_DLG_SHOULD_IF_ERR
	{
		int nSize = get_input_xyrange_data_size(trGetN.iy);
		if(_check_err_condition(trGetN, nSize) != CER_NO_ERROR)
			nRet = XFEVT_PROCEED_WITH_DLG;
		_set_coef_disable_if_linear(trGetN); ///Cheney 2007-8-15 FIX_SOME_BU_OF_INTERP
	}
	///end ADD_AN_ERR_CHECK_FUNC
	
	///------ 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
	
}
//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 interp1_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
