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

//put your own support static functions here
static void _add_one_value(TreeNode& tr, LPCSTR lpcszTag, double v, int nColDesignation, LPCSTR lpcszName, LPCSTR lpcszUnits, LPCSTR lpcszStrVal = NULL)
{
	TreeNode trn;
	if(lpcszStrVal)
	{
		trn = tr.AddTextNode(lpcszStrVal, lpcszTag);
		trn.SetAttribute(STR_TYPE_ATTRIB, TNVAL_TYPE_CSTRING); // Hong, requied by out_tree_to_wks
	}
	else
	{
		trn = tr.AddNumericNode(0, lpcszTag);
		trn.dVal = v;
	}
	trn.SetAttribute(STR_LABEL_ATTRIB, lpcszName);
	if(lpcszUnits)
		trn.SetAttribute("Units", lpcszUnits);
	if(nColDesignation >= 0)
		trn.SetAttribute(STR_COL_DESIGNATION_ATTRIB, nColDesignation);
		
}
static void _get_wks_col_info(DataPlot& dp, string& strName, string& strUnits)
{
	string strDataset = dp.GetDatasetName();
	Column cc;
	Worksheet wks;
	if(get_wks_col(strDataset, cc, wks))
	{
		strName = cc.GetLongName();if(strName.IsEmpty()) strName = cc.GetName();
		strUnits = cc.GetUnits();
	}
}
	
static void _add_all_curves_in_layer(TreeNode& tr, double x, GraphLayer& gl, int& nCoffset)
{
	foreach(DataPlot dp in gl.DataPlots)
	{
		string strName;
		string strUnits;
		_get_wks_col_info(dp, strName, strUnits);
		string strTag;
		strTag.Format("B%d", nCoffset++);
		int nIndex;
		int nRet = dp.XIndex(x, nIndex);
		double y = NANUM;
		if(nRet == XIR_WITHIN_RANGE)
		{
			dp.GetDataPoint(nIndex, NULL, &y);
		}
		_add_one_value(tr, strTag, y, OKDATAOBJ_DESIGNATION_Y, strName, strUnits);
	}
}


void gxy2w(const double& x, const GraphPage& igp, const string& bname, const int& n1, const int& n2, const string& xname, const string& xunits)
{
	
	if(!igp)
		XF_THROW(XFERR_NO_ACTIVE_GRAPHPAGE);
	
	int i1 = n1-1, i2 = n2 - 1;
	if(i2 < 0) // use all layers
	{
		int nLayers = igp.Layers.Count();
		i2 = nLayers-1;
	}
	GraphLayer gl1 = igp.Layers(i1);
	if(!gl1)
		XF_THROW(XFERR_INTOPTN_OUT_OF_RANGE);
	
	GraphLayer gl2 = igp.Layers(i2);
	if(!gl2)
		XF_THROW(XFERR_INTOPTN_OUT_OF_RANGE);
	Tree tr;
	_add_one_value(tr, "C", 0, -1, _L("Comments"), NULL, "");
	_add_one_value(tr, "A", x, OKDATAOBJ_DESIGNATION_X, xname, xunits);
	int nC = 1;
	for(int ii = i1; ii <= i2; ii++)
	{
		GraphLayer gl = igp.Layers(ii);
		_add_all_curves_in_layer(tr, x, gl, nC);
	}
	out_tree_to_wks(tr, bname);
	
		
}

//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 gxy2w_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return gxy2w_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int gxy2w_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	
	return -1;
	
}
//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 gxy2w_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 gxy2w_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
