#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_data,
	type_screen,
};
//put additional include files here

#include <event_utils.h>

//put your own support static functions here

static BOOL		_check_error(TreeNode& trGetN, string& strErrMsg)
{
	GraphPage	gp;
	if ( !okxf_resolve_string_get_origin_object(trGetN.gp.strVal, &gp) || !gp )
	{
		strErrMsg = "Invalid Graph Page";
		return FALSE;
	}
	
	if ( trGetN.npts.nVal <= 0 )
	{
		strErrMsg = "Points must be positive";
		return FALSE;
	}
	
	if ( is_treenode_optional(trGetN.x)
		&& is_treenode_optional(trGetN.y)
		&& is_treenode_optional(trGetN.plot)
		&& is_treenode_optional(trGetN.index)
		)
	{
		strErrMsg = "Please select at least one output";
		return FALSE;
	}
	
	return TRUE;
}

static	BOOL	_check_xf_body_error(string& strErrMsg)
{
	XFExeContext*	pXFExecCtxt = Project.GetCurrentXFExeCtxt();
	if ( !pXFExecCtxt )
	{
		ASSERT(FALSE);
		strErrMsg = "Fail to get XF Execution Context!!!";
		return FALSE;
	}
	
	Tree		tr;
	TreeNode	trGetN;
	pXFExecCtxt->GetGUI(tr, trGetN);
	return _check_error(trGetN, strErrMsg);
}

static	void	_check_show_plot_name_and_point_index(TreeNode& trGetN)
{
	trGetN.plot.Show = trGetN.index.Show = type_data == trGetN.type.nVal;
}


void pickpts(const GraphPage& gp, const int& npts, const int& type, vector& x, vector& y, vector<string>& plot, vector& index)
{
	string		strErrMsg;
	if ( !_check_xf_body_error(strErrMsg) )
		XF_THROW(strErrMsg);
	
	string		strPage = gp.GetName();	
	if ( 0 == GetPoints(strPage, npts, type, plot, x, y, index) )
		XF_THROW("\r\n");
}

//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 pickpts_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return pickpts_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if ( lpcszNodeName && 0 == lstrcmpi(lpcszNodeName, "type") )
	{
		_check_show_plot_name_and_point_index(trGetN);
	}
	
	if( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID || lpcszNodeName && *lpcszNodeName )
	{
		if ( nEventID < 0 )
		{
			_check_show_plot_name_and_point_index(trGetN);
		}
		
		bOKEnable = _check_error(trGetN, strErrMsg);
	}
	
	return true;

}

int pickpts_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 pickpts_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( is_xf_before_execute_body(nGetNDialog, dwCntrl) )
	{
		if ( type_screen == trGetN.type.nVal )
		{
			trGetN.plot.SetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, 0);
			trGetN.index.SetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, 0);
		}
	}
	
	if ( nGetNDialog > 0 )		/// open dialog
		check_convert_optional_outputs_to_new(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 pickpts_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
