#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

//put your own support static functions here


void ttest1(const Range& irng, const double& mean, const int& tail, const double& alpha, double& stat, double& df, double& prob, double& lcl, double& ucl)
{
			
	/// *****		Terminate if error setting		***** ///
	
	int nRet = check_1_data(irng);
	if (CER_NO_ERROR != nRet)
		XF_THROW(nRet);
			
	vector vData;
	irng.GetData(vData, 0);	
	
	int nMissing = ocmath_count(NANUM, vData.GetSize(), vData);
	if (0 < nMissing)
		vData.Trim();
	
	if (tail < 0 || tail > 2)
	{
		XF_THROW(CER_TAIL_SETTING);
	}
	
	if (!check_sig_level(alpha))
		XF_THROW(CER_INVALID_SIG_LEV);
		
	/// *****		One sample ttest		***** ///
	HypotTestOptions HTO;
	tTestMean1SampleResults tRes;
	HTO.HypotValue = mean;
	HTO.TailType=tail;
	nRet = ocmath_one_sample_t_test(vData, vData.GetSize(), &HTO, &tRes );
	if (nRet != STATS_NO_ERROR)
	{
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	
	//Output results
	//if (nMissing > 0)
	//{
		//XF_TRACE(MISSING_IN_DATA_MSG, nMissing);
	//}
	
	if (stat != NULL)	stat = tRes.tValue;
	if (df != NULL)	df = tRes.DOF;
	if (prob != NULL)	prob = tRes.Prob;
	if (lcl != NULL)	lcl = NANUM;
	if (ucl != NULL)	ucl = NANUM;
		
	vector vLevels(1);
	vLevels[0] = 1 - alpha;
	vector vLCLs(1); 
	vector vUCLs(1);
	
	nRet = ocmath_confidence_levels_for_mean(vLevels, vLevels.GetSize(), &tRes, vLCLs, vUCLs);
	if (nRet != STATS_NO_ERROR)
	{
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	}
	
	if (lcl != NULL)	lcl = vLCLs[0];
	if (ucl != NULL)	ucl = vUCLs[0];
	
	// cancel the conclusion to simplify the XF
	/*
	string strNul = "Null Hypothesis: Mean   ";
	string strAlt = "Alternative Hypothesis: Mean  ";
	switch (tail)
	{
	case 0:
		strNul += "= " + (string)(mean);
		strAlt += "<> " + (string)(mean);
		break;
	case 1:
		strNul += "<= " + (string)(mean);
		strAlt += "> " + (string)(mean);
		break;
	case 2:
		strNul += ">= " + (string)(mean);
		strAlt += "< " + (string)(mean);
		break;
	default:
		break;		
	}
	string strRes1, strRes2, strRes;
	strRes1 = "At the 0.05 level, the population mean is";
	strRes2 = "significantly different than the test mean (" + (string)mean + ")";
	strRes = (prob < 0.05) ? strRes1 + strRes2 : strRes1 + " not " + strRes2;
		
	conclusion = strNul + "\r\n" + strAlt + "\r\n" + strRes;
	*/
	
}

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

}

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