#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <..\OriginLab\curve_utils.h>
#define _MIN_BASE_PTS 2
//put your own support static functions here


void blauto(const XYRange& iy, const double& thres, int& number, XYRange& oy)
{
	if ( !iy || !oy )
		XF_THROW(CER_NOT_ONE_XYRANGE); // need specify error type
	
	vector vx, vy;
	vector vebx, veby;
	
	if( !iy.GetData(vy, vx) )
		XF_THROW(XFERR_INVALID_RANGE);
	
	if(number < _MIN_BASE_PTS)
	{
		XF_THROW(XFERR_WRONG_PARAM_NUMBER);
	}
	
	if(thres <= 0|| thres>=1)
	{
		XF_THROW(CER_INPUT_OPTION_OUT_OF_RANGE);
	}
	
	if ( !bl_auto_find_baseline_points(vx, vy, vebx, veby, number, thres) )
		XF_THROW(CER_UNKNOWN_ERROR); // need specify error type
	
	if( !oy.SetData(&veby, &vebx) )
		XF_THROW(XFERR_FAIL_IN_SET_XYRANGE_DATA);
	
	DataPlot dpBL, dpFP;
	if ( iy.GetPlot(dpBL) && !oy.GetPlot(dpFP) )
	{
		GraphLayer gl;
		dpBL.GetParent(gl);
		if ( !gl )
			XF_THROW(CER_UNKNOWN_ERROR); // need specify error type
		
		//gl.SetMarkerLines(vebx);
		int nPlot = gl.AddPlot(oy, IDM_PLOT_SCATTER);
		dpFP = gl.DataPlots(nPlot);
		if ( !dpFP )
			XF_THROW(CER_UNKNOWN_ERROR); // need specify error type
		dpFP.SetColor(SYSCOLOR_RED);
		legend_append_plot(gl, nPlot);
	}
	
}

//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 blauto_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return blauto_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	TreeNode trInputData= trGetN.iy;
	XYZRange dr;
	if(!okxf_resolve_tree_construct_range(&trInputData, &dr)||!dr.IsValid())
	{
		strErrMsg = "";
		bOKEnable = false;
		strErrMsg = XFERR_NO_DATA_SELECT;
		return true;
	
	}
	
	if ( trGetN.number.nVal < _MIN_BASE_PTS )
	{
		strErrMsg = "";
		bOKEnable = false;
		strErrMsg = XFERR_WRONG_PARAM_NUMBER;
		return true;
	}
	
	if ((trGetN.thres.dVal <= 0||trGetN.thres.dVal>=1 ))
	{
		strErrMsg = "";
		bOKEnable = false;
		strErrMsg = CER_INPUT_OPTION_OUT_OF_RANGE;
		return true;
	}
		
		
	return false;

}

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