#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	tail_two,
	tail_upper,
	tail_lower,
};
#include <event_utils.h>


//put additional include files here
#include <stats_utils.h>
#include <ReportTree.h>
#include <report_utils.h>
#include <GetNBox.h>
#include <o8dlg.h>
//put your own support static functions here

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL						*
 *------------------------------------------------------------------------------*/



void sign2(const Range& irng, const string& null, const int& tail, const double& alpha, ReportTree& rt)
{
	
	vector vData1, vData2;
	vector<string> vstrLab;
	
	int nMissing, nRet;
	
	nRet = get_2_data_in_var(irng, vData1, vData2, vstrLab, nMissing, true);
	if (nRet != STATS_NO_ERROR)
	    XF_THROW(nRet);
	
	int nSize1 = vData1.GetSize();
	int nSize2 = vData2.GetSize();
	
	//Compute descriptive statistics
	DescStatResults pDescStats[2];
	QuantileOptions stQuanOpt;
	QuantileResults pQuantiles[2];
	nRet = stats_descriptive(vData1, pDescStats[0]);
	nRet += stats_descriptive(vData2, pDescStats[1]);
	if (nRet != STATS_NO_ERROR)
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	
	InitQuantiles(stQuanOpt);
	nRet = stats_quantiles(vData1, stQuanOpt, pQuantiles[0]);
	nRet += stats_quantiles(vData2, stQuanOpt, pQuantiles[1]);
	if (nRet != STATS_NO_ERROR)
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	
	
	LPCSTR lpsczLabel = _L("Signed Test");
	report_tree_create(rt, lpsczLabel, "irng");
	rt_add_nph_descriptive(rt, pDescStats, pQuantiles, vstrLab);
	
	
	int nTies;
	NPHFreqs SignFreqs;
	NPHStats stSiStats;
	nRet = ocmath_sign_test(vData1, vData2, vData1.GetSize(), tail, &nTies, &SignFreqs, &stSiStats);  
	if (nRet != STATS_NO_ERROR)
	{
	    XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	//--- Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
	//rt_add_sign_test(rt, SignFreqs, nTies, stSiStats, vstrLab, tail);
	rt_add_sign_test(rt, SignFreqs, nTies, stSiStats, vstrLab, tail,alpha);
	//--- end USER_CAN_SET_SIGNIFICANCE_LEVEL
}

//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 sign2_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return sign2_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	///Kyle 09/24/08 SET_NO_NEED_TO_LOCALIZE
	if ( GETNE_ON_INIT == nEventID )
	{
		trGetN.tail.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);
	}
	///End SET_NO_NEED_TO_LOCALIZE
	
	int nErr = check_2_data_in_var(trGetN.irng, true);
	if (CER_NO_ERROR != nErr)
	{
		bOKEnable = false;
		strErrMsg = nErr;
	}
	//--- Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
	int nErr2 = hyt_check_input(trGetN);
	if (nErr2 != CER_NO_ERROR)
	{
		bOKEnable = false;
		strErrMsg = nErr2;
	}
	//--- end USER_CAN_SET_SIGNIFICANCE_LEVEL
	return nph_add_null_event(trGetN);
	         
	         

}

int sign2_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 sign2_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	//------ fisher 11/16/07 ADD_MAP_ID_TO_CHM
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_sign2);
	
	if ( (dwCntrl&LTXF_THEME_USED) && nGetNDialog <= 0 )
	{
		if ( CER_NO_ERROR != check_2_data_in_var(trGetN.irng, true) )
			nRet = XFEVT_PROCEED_WITH_DLG;
	}
	
}
//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 sign2_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
