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



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

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

#define IDST_NPH_WILCOXON_PAIRS_TEST   0x00000001
#define IDST_NPH_SISTATS_TEST          0x00000010


enum
{
	RES_MW,
	RES_KS,
	RES_BOTH,
};


static bool _check_no_input(TreeNode& tr)
{
	if ( !tr.wilcoxon.nVal && !tr.sign.nVal )
		return false;
	
	return true;		
}



void NPHPairSample(const Range& irng, const string& null, const int& tail, const double& alpha, const int& wilcoxon, const int& sign, ReportTree& rt)
{
	
	vector vData1, vData2;
	vector<string> vstrLab;
	
	int nMissing, nRet;
	
	//nRet = (0 == type)? get_2_data_in_group(irng, vData1, vData2, vstrLab, nMissing, true) : get_2_data_in_var(irng, vData1, vData2, vstrLab, nMissing, true);	
	nRet = get_2_data_in_var(irng, vData1, vData2, vstrLab, nMissing, true);	
	if (nRet != STATS_NO_ERROR)
	    XF_THROW(nRet);
	
	/// *****		Terminate if error setting		***** ///
	int nSize1 = vData1.GetSize();
	int nSize2 = vData2.GetSize();
	
	
	/// *****Begin Two Sample Non-parametric analysis Computation***** //
	//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("Nonparametric Analysis (Two Sample)");
	report_tree_create(rt, lpsczLabel, "irng");
	rt_add_nph_descriptive(rt, pDescStats, pQuantiles, vstrLab);
	
	//Begin wilcoxon test	
	NPHSummary pWilcSummary[2];
	NPHRanks pWilcRanks[2];
	NPHFreqs SignFreqs;
	NPHStats stWilcStats, stSignStats;
	
	string strErr;
	nRet = ocmath_wilcoxon_sign_rank(vData1, vData1.GetSize(), vData2, vData2.GetSize(), tail, pWilcSummary, pWilcRanks, &stWilcStats);
	if (nRet != STATS_NO_ERROR)
	{
	 	XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	
	//Begin signed test
	int nTies;
	nRet = ocmath_sign_test(vData1, vData2, vData1.GetSize(), tail, &nTies, &SignFreqs, &stSignStats);	
	if (nRet != STATS_NO_ERROR)
	{
	    XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	
	
	//nph2_update_report_tree(rt, pWilcRanks, stWilcStats, stSignStats, vstrLab, tail, wilcoxon, sign);
	if (wilcoxon)
	{
		ReportTable rtWilcoxon = rt.CreateTable("Wilcoxon", _L("Wilcoxon test"), IDST_NPH_WILCOXON_PAIRS_TEST);
		//--- Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
	    //rt_add_signed_rank(rtWilcoxon, pWilcRanks, stWilcStats, vstrLab, tail);
	    rt_add_signed_rank(rtWilcoxon, pWilcRanks, stWilcStats, vstrLab, tail,alpha);
	    //--- end USER_CAN_SET_SIGNIFICANCE_LEVEL
		
	}
	if (sign)
	{
		ReportTable rtSign = rt.CreateTable("SignTest", _L("Signed test"), IDST_NPH_SISTATS_TEST);
		//--- Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
		//rt_add_sign_test(rtSign, SignFreqs, nTies, stSignStats, vstrLab, tail);
		rt_add_sign_test(rtSign, SignFreqs, nTies, stSignStats, 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 NPHPairSample_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return NPHPairSample_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
	
	tree_stats_convert_input_data(trGetN.irng, SIDT_INVAR);
	//int nType = trGetN.type.nVal;
	//switch( nType )
	//{
	//case 0:
		//tree_stats_convert_input_data(trGetN.irng, SIDT_INGROUP);
		//break;
	//
	//case 1:
		//tree_stats_convert_input_data(trGetN.irng, SIDT_INVAR);
		//break;
	//}
	//
	if (trGetN.tail.nVal == 0)	
	   trGetN.null.strVal = "F(x) = G(x)";
	else if (trGetN.tail.nVal == 1)	
	   trGetN.null.strVal = "F(x) <= G(x)";
	else if (trGetN.tail.nVal == 2)
	   trGetN.null.strVal = "F(x) >= G(x)";
	   
	trGetN.null.Enable = 0;
	
	//int nErr = ( 0 == nType ) ? check_2_data_in_group(trGetN.irng, true) : check_2_data_in_var(trGetN.irng, true);
	int nErr = check_2_data_in_var(trGetN.irng, true);
	
	if (!_check_no_input(trGetN) && CER_NO_ERROR == nErr)
	{
		nErr = CER_SUPPRESS_ALL_OUTPUT; 		
	}
	
	if (CER_NO_ERROR != nErr)
	{
		bOKEnable = false;
		strErrMsg = nErr;
	}
	
	//DynaCntrlContainer.PostMessage(WM_USER_MSG_ON_DLG, 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;
	}
	return true;
	//--- end USER_CAN_SET_SIGNIFICANCE_LEVEL
	  

}

int NPHPairSample_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 NPHPairSample_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_XF_NPHPairSample);
	
	if ( (dwCntrl&LTXF_THEME_USED) && nGetNDialog <= 0 )
	{
		int nErr = check_2_data_in_var(trGetN.irng, true);
		
		if (!_check_no_input(trGetN) && CER_NO_ERROR == nErr)
		{
			nErr = CER_SUPPRESS_ALL_OUTPUT; 		
		}
		if ( CER_NO_ERROR != nErr )
			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 NPHPairSample_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
