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



/***************************************************************************************
/// Modification Log :
/// Max 11/19/08 v8.0975b NEED_HANDLE_BOUNDARY_CASES
***************************************************************************************/

//put your own support static functions here



void diststats(const XYRange& iy, const vector& percent, const int& dir, int& ipeak, double& xpeak, double& ypeak, double& mean, double& median, vector& quantile)
{
		vector vx, vy;
		if(!iy.IsValid() || iy.GetNumData(DRR_GET_DEPENDENT) != 1 || !iy.GetData(vy, vx))
			XF_THROW(CER_NOT_ONE_XYRANGE);
	
		int nRet, nSize = vx.GetSize(), nSizePer = percent.GetSize();
		
		if(nSizePer > 0)
		{
			quantile.SetSize(nSizePer);
			for(int ii=0; ii<nSizePer; ++ii)
				quantile[ii] = NANUM;
			nRet = ocmath_percentiles_distribution(nSize, vx, vy, percent*0.01, nSizePer, quantile);
		}
		
		double	dxPeak = NANUM, dyPeak = NANUM, dMean = NANUM, dMedian = NANUM;
		int ixpeak = -1;
		nRet = ocmath_basic_stats_distribution(nSize, vx, vy, &dxPeak, &dyPeak, &ixpeak, &dMean, &dMedian, NULL, dir);
		if(nRet != OE_NOERROR)
		{
			dxPeak = dyPeak = dMean = dMedian = NANUM;
			ixpeak = -1;
		}
		
		ipeak = ixpeak+1;
		xpeak = dxPeak;
		ypeak = dyPeak;
		mean = dMean;
		median = dMedian; 
	
}

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

}

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