#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <GetNBox.h>
#include <report_utils.h>
#include <event_utils.h>
#include <stats_utils.h>
#include <o8dlg.h>
///////////////////////////////////////////////////
// Modification log
///Jasmine 08/14/07 QA80-10207 REPORT_SHEET_CHECK_CONFLICT
/// Max 3/7/08 SHOULD_PASS_WHOLE_STRING_OF_LPCSZRDLABEL_FOR_LOCALIZATION
/// Kyle 06/05/2009 QA70-13733 RADIX_POINT_SHOULD_BE_COMMA_IN_GOS
/// Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
/// Jacky 08/27/10 ORG-920-P3 ONE_SAMPLE_TTEST_BETTER_HANDLING_OF_OUTPUT_PLOT_AND_REPORT_DATA
///////////////////////////////////////////////////

//put your own support static functions here

static void _set_ttest2_stats_unequal_to_vector(tTestResults &stTTStats, vector &vTTStats)
{
	vTTStats.SetSize(3);
	vTTStats[0] = stTTStats.tValue;
	vTTStats[1] = stTTStats.DOF;
	vTTStats[2] = stTTStats.Prob;	
	
}


void TwoSampletTest(const string& hint_indexed, const string& hint_raw, const int& type, const Range& irng, const double& mean, const string& null, const int& tail, const double& alpha, const int& conf, const string& level, const int& actpower, const double& siglevel, const int& hypotpower, const string& hypotsize, const int& histogram, const int& box, ReportData& rd, ReportTree& rt)
{
	
	
	waitCursor junk;
	
	int nMissing, nRet;
	vector vData1, vData2;
	vector<string> vstrLab;
	
	/// Trim out missing values before calculation
	nRet = (0 == type)? get_2_data_in_group(irng, vData1, vData2, vstrLab, nMissing) : get_2_data_in_var(irng, vData1, vData2, vstrLab, nMissing);	
	if (nRet != STATS_NO_ERROR)			
		XF_THROW(nRet);
						
	if (vData1.GetSize() == 0 || vData2.GetSize() == 0)
		XF_THROW(CER_NO_DATA);	
	
	//int tail = trtTest.TailType.nVal;
		
	/// *****		Descriptive Statistics		***** ///
	DescStatOptions stStatsOpt;
	set_ttest_desc_stats_opt(stStatsOpt);
	DescStatResults pDescStats[2];
	
	nRet = stats_desc_stats(vData1, stStatsOpt, pDescStats[0]);
	if (nRet != STATS_NO_ERROR)
	{
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	nRet += stats_desc_stats(vData2, stStatsOpt, pDescStats[1]);
	if (nRet != STATS_NO_ERROR)
	{
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
		
	LPCSTR lpsczLabel = _L("Two sample t Test");
	report_tree_create(rt, lpsczLabel, "irng");	
	rt_add_ttest_desc_stats(rt, pDescStats, vstrLab);
		
	/// *****		Two-Sample t-test		***** ///
	
	HypotTestOptions HTO;
	tTestMean2SampleResults tRes;
	HTO.HypotValue = mean;
	HTO.TailType=tail;
	tTestResults tResUneqVar;
	nRet = ocmath_two_sample_t_test(vData1, vData1.GetSize(), vData2, vData2.GetSize(), &HTO, &tRes, &tResUneqVar);
	if (nRet != STATS_NO_ERROR)
	{
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	
	string strHYPT, strConclusion;
	int nHYPT, nConclusion, nConAccept, nConReject;
	switch (tail)
	{
		case TAILED_TEST_TWO:
		default:
			nHYPT = FOOTNOTE_TTEST2_TWO_TAIL;
			nConAccept = FOOTNOTE_TTEST2_CON_DIFF;
			nConReject = FOOTNOTE_TTEST2_CON_SAME;
			break;
		case TAILED_TEST_UPPER:
			nHYPT = FOOTNOTE_TTEST2_UPPER_TAIL;
			nConAccept = FOOTNOTE_TTEST2_CON_GRT;
			nConReject = FOOTNOTE_TTEST2_CON_NOT_GRT;
			break;
		case TAILED_TEST_LOWER:
			nHYPT = FOOTNOTE_TTEST2_LOWER_TAIL;
			nConAccept = FOOTNOTE_TTEST2_CON_LES;
			nConReject = FOOTNOTE_TTEST2_CON_NOT_LES;
			break;
	}
	
	string strMean = ftoa(mean);		///Kyle 06/05/2009 QA70-13733 RADIX_POINT_SHOULD_BE_COMMA_IN_GOS
	
	ocu_load_err_msg_str(nHYPT, &strHYPT);
	///Kyle 06/05/2009 QA70-13733 RADIX_POINT_SHOULD_BE_COMMA_IN_GOS
	//strHYPT.Format(strHYPT, mean, mean);
	strHYPT.Format(strHYPT, strMean, strMean);
	///End RADIX_POINT_SHOULD_BE_COMMA_IN_GOS
	//--- Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
	//nConclusion = (tRes.Prob< 0.05) ? nConAccept : nConReject;
	nConclusion = (tRes.Prob< alpha) ? nConAccept : nConReject;
	//--- end USER_CAN_SET_SIGNIFICANCE_LEVEL
	ocu_load_err_msg_str(nConclusion, &strConclusion);
	///Kyle 06/05/2009 QA70-13733 RADIX_POINT_SHOULD_BE_COMMA_IN_GOS
	//strConclusion.Format(strConclusion, mean);
	//--- Jacky 07/22/10 ORG-102 USER_CAN_SET_SIGNIFICANCE_LEVEL
	//strConclusion.Format(strConclusion, strMean);
	strConclusion.Format(strConclusion,alpha,strMean);
	//--- end USER_CAN_SET_SIGNIFICANCE_LEVEL
	///End RADIX_POINT_SHOULD_BE_COMMA_IN_GOS
	vector vTTStats, vTTStatsUnql;
	set_ttest_stats_to_vector(tRes, vTTStats);
	_set_ttest2_stats_unequal_to_vector(tResUneqVar, vTTStatsUnql);
	
	rt_add_ttest2_stats(rt, tail, vTTStats, vTTStatsUnql,strHYPT, strConclusion);
	
	if (conf)
	{
		vector vLevels, vPercents;
		int nLevels = level.GetTokens(vPercents);//put the confidence levels in vector
		vLevels = vPercents / 100.0;
		
		vector vLCLs, vUCLs;
		vLCLs.SetSize(nLevels);
		vUCLs.SetSize(nLevels);		
		
		nRet = ocmath_two_sample_confidence_levels_for_mean_equal_var(vLevels, vLevels.GetSize(), &tRes, vLCLs, vUCLs);
		if (nRet != STATS_NO_ERROR)
		{
			XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
		}
		
		rt_add_ttest_conf_intvl(rt, vPercents, vLCLs, vUCLs);
	}
	
	if (actpower || hypotpower)
	{
		vector vSampleSizes, vPowers;
		int nSS = hypotsize.GetTokens(vSampleSizes);
		vPowers.SetSize(nSS + 1);
		
		vSampleSizes.InsertAt(0, vData1.GetSize() + vData2.GetSize());
		
		nRet = ocmath_two_sample_tTest_powers(siglevel, &HTO, vSampleSizes, vSampleSizes.GetSize(), &tRes, vPowers);
		if (nRet != STATS_NO_ERROR)
		{
			XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
		}
		
		rt_add_ttest_power(rt, siglevel, vSampleSizes, vPowers, actpower, hypotpower);
	}
	
	if (rd)
	{
		vector vGroups(2);
		vGroups[0] = vData1.GetSize();
		vGroups[1] = vData2.GetSize();
		vector vData;
		vData = vData1;
		vData.Append(vData2);
		
		ttest_update_report_data(rd, vData,vGroups,vstrLab);
		
		int nPlotType; 
		if (histogram) nPlotType += HYPO_TEST_HISTOGRAM;
		if (box) nPlotType += HYPO_TEST_BOX;
		
		hypo_test_rd_plotting(rd, vstrLab, nPlotType, rt);
		
	}
	
	
}

//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 TwoSampletTest_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return TwoSampletTest_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	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;
	}
	/// Max 3/7/08 SHOULD_PASS_WHOLE_STRING_OF_LPCSZRDLABEL_FOR_LOCALIZATION
	//rd_set_optional(nEventID, bOKEnable, lpcszNodeName, trGetN.rd, trGetN.box.nVal||trGetN.histogram.nVal, "PlotData1");
	/// Max 4/8/08 move to event_utils.h
	//string strRDvalue = "[<input>]<new>";
	//--- Jacky 08/27/10 ORG-920-P3 ONE_SAMPLE_TTEST_BETTER_HANDLING_OF_OUTPUT_PLOT_AND_REPORT_DATA
	//string strRDvalue = STR_DEFAULT_TEXT_FOR_REPORT_DATA_NODE;
	//--- end ONE_SAMPLE_TTEST_BETTER_HANDLING_OF_OUTPUT_PLOT_AND_REPORT_DATA
	//------
	//--- Jacky 08/27/10 ORG-920-P3 ONE_SAMPLE_TTEST_BETTER_HANDLING_OF_OUTPUT_PLOT_AND_REPORT_DATA
	//okutil_convert_localized_PDS(&strRDvalue);
	//tr_set_optional(nEventID, lpcszNodeName, trGetN.rd, trGetN.box.nVal||trGetN.histogram.nVal, strRDvalue);
	tr_set_optional_ex(nEventID, lpcszNodeName, trGetN.rd, xf_event_is_change_param(trGetN), trGetN.box.nVal || trGetN.histogram.nVal);
	//--- end ONE_SAMPLE_TTEST_BETTER_HANDLING_OF_OUTPUT_PLOT_AND_REPORT_DATA
	/// END SHOULD_PASS_WHOLE_STRING_OF_LPCSZRDLABEL_FOR_LOCALIZATION
	
	int nErr = ( 0 == nType ) ? check_2_data_in_group(trGetN.irng, false) : check_2_data_in_var(trGetN.irng, false);
	if ( CER_NO_ERROR != nErr )
	{
		strErrMsg = nErr;
		bOKEnable = false;		
	}	
	
	///Jasmine 08/14/07 QA80-10207 REPORT_SHEET_CHECK_CONFLICT
	//return ttest_event(trGetN, bOKEnable, strErrMsg, _L("Mean1 - Mean2"));	
	bool bRet = ttest_event(trGetN, bOKEnable, strErrMsg, _L("Mean1 - Mean2"));	
	if(bOKEnable && is_same_sheet(trGetN.rd, trGetN.rt))
	{
		bOKEnable = false;
		nErr = XFERR_REPORT_SHEET_CONFLICT;
		strErrMsg = nErr;
	}
	return bRet;
	///End REPORT_SHEET_CHECK_CONFLICT
	

}

int TwoSampletTest_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 TwoSampletTest_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_TwoSampletTest);
	
	///Jasmine 08/14/07 QA80-10207 REPORT_SHEET_CHECK_CONFLICT
	//if ((dwCntrl&LTXF_THEME_USED) && nGetNDialog <= 0)
	if (nGetNDialog <= 0)
	{
		int nErr;
		bool bOKEnable; 
		string strErrMsg;
		ttest_event(trGetN, bOKEnable, strErrMsg, _L("Mean1 - Mean2"));
		//if (CER_NO_ERROR != check_1_data(trGetN.irng, true) ||  strErrMsg != "")
		if(strErrMsg.IsEmpty() && CER_NO_ERROR == nErr)
		{
			nErr = check_1_data(trGetN.irng, true);
			if(CER_NO_ERROR == nErr && is_same_sheet(trGetN.rd, trGetN.rt))
				nErr = XFERR_REPORT_SHEET_CONFLICT;
		}
		if(!strErrMsg.IsEmpty() || CER_NO_ERROR != nErr)
		{
			if((dwCntrl&LTXF_THEME_USED))
				nRet = XFEVT_PROCEED_WITH_DLG;		
			else
			{
				if(strErrMsg.IsEmpty())
					warning_msg_box(nErr, false, 'E');
				else
					warning_msg_box(strErrMsg, false, 'E');
				nRet = XFEVT_ABORT;
			}
		}
	}
	///End REPORT_SHEET_CHECK_CONFLICT
}
//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 TwoSampletTest_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
