#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	mode_get,
	mode_add,
};

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 * Kenny 9/25/2009 QA81-11561 WANT_LT_ACCESS_TO_DATA_INDEX_OF_DATA_MARKERS_IN_XF_MKS*
 *	Folger 11/06/09 MKS_FAILS_TO_CREATE_OUTPUT_LOOSE_DATASET					*
 *------------------------------------------------------------------------------*/

//put additional include files here

//put your own support static functions here

/// Kenny 9/25/2009 QA81-11561 WANT_LT_ACCESS_TO_DATA_INDEX_OF_DATA_MARKERS_IN_XF_MKS
static void _get_markers(DataPlot& dp, vector<int>& vnBegin, vector<int>& vnEnd, const int& sort)
{
	dp.GetDataMarkers(vnBegin, vnEnd);
	if( sort )
	{
		vector<uint> vnIndeces;
		vnBegin.Sort(SORT_ASCENDING, TRUE, vnIndeces);
		vnEnd.Reorder(vnIndeces);
	}
}

static void _add_markers(DataPlot& dp, vector<int>& vnBegin, vector<int>& vnEnd)
{
	const int nBeginSize = vnBegin.GetSize();
	const int nEndSize = vnEnd.GetSize();
	if ( nBeginSize != nEndSize )
	{
		XF_THROW("The sizes of begin and end markers do not match!");
		return;
	}
	if (0 == nBeginSize || 0 == nEndSize)
	{
		XF_THROW("No markers!");
		return;
	}
	int bRet = dp.AddDataMarkers(vnBegin, vnEnd);
}
/// End WANT_LT_ACCESS_TO_DATA_INDEX_OF_DATA_MARKERS_IN_XF_MKS



void mks(const XYRange& iy, vector& ob, vector& oe, const int& sort, const int& mode)
{
	DataPlot dp;
	if( !iy.GetPlot(dp) )
		XF_THROW("DataPlot is not available");
	
	if( dp )
	{
		// local vector<int> variables are needed
		// as vector<int> XVariable type does not exist
		vector<int> vLocalBegin;
		vector<int> vLocalEnd;
		/// Kenny 9/25/2009 QA81-11561 WANT_LT_ACCESS_TO_DATA_INDEX_OF_DATA_MARKERS_IN_XF_MKS
		//dp.GetDataMarkers(vLocalBegin, vLocalEnd);
		//if( sort )
		//{
			//vector<uint> vnIndeces;
			//vLocalBegin.Sort(SORT_ASCENDING, TRUE, vnIndeces);
			//vLocalEnd.Reorder(vnIndeces);
		//}
		//
		//ob = vLocalBegin;
		//oe = vLocalEnd;
		if (mode_get == mode)
		{
			_get_markers(dp, vLocalBegin, vLocalEnd, sort);
			ob = vLocalBegin;
			oe = vLocalEnd;
		}
		else
		{
			vLocalBegin = ob;
			vLocalEnd = oe;
			_add_markers(dp, vLocalBegin, vLocalEnd);
		}
		GraphLayer gl;
		dp.GetParent(gl);
		ASSERT( gl.IsValid() );
		GraphPage gp;
		gl.GetParent(gp);
		ASSERT( gp.IsValid() );
		gp.Refresh(TRUE);
		/// End WANT_LT_ACCESS_TO_DATA_INDEX_OF_DATA_MARKERS_IN_XF_MKS
	}
}

//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 mks_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	///------ Folger 11/06/09 MKS_FAILS_TO_CREATE_OUTPUT_LOOSE_DATASET
	if ( 0 == nGetNDialog || -1 == nGetNDialog )
	{
		if ( mode_get == trGetN.mode.nVal )
		{
			trGetN.ob.SetAttribute(STR_XF_VAR_IO_ATTRIB, IO_OUTPUT);
			trGetN.oe.SetAttribute(STR_XF_VAR_IO_ATTRIB, IO_OUTPUT);
		}
		else
		{
			trGetN.ob.SetAttribute(STR_XF_VAR_IO_ATTRIB, IO_INPUT);
			trGetN.oe.SetAttribute(STR_XF_VAR_IO_ATTRIB, IO_INPUT);
		}
	}
	///------ End MKS_FAILS_TO_CREATE_OUTPUT_LOOSE_DATASET
}
