#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	show_fraction,
	show_percent,
};
#include <event_utils.h>
#include <ReportTree.h>
#include <ocu.h>
#include <o8dlg.h>
//put additional include files here

#include	<report_utils.h>

/***************************************************************************************
/// Modification Log :
///Echo QA70-12013 v8.0921  LABEL_NOT_CONSISTENT_WITH_FREQ_COUNT
///Kyle 11/24/2008 SORT_DEGRESSIVELY_BY_FREQ_COUNT
/// Folger 04/22/09 QA80-13420 PARETOCHART_XF_BASED_PLOTTING
/// Kyle 07/09/2009 QA80-13902-S1 FREQUENCY_SUPPORT_FRACTION_AND_PERCENT
/// Kyle 07/09/2009 QA80-13902-P1 DISCRETE_FREQUENCY_SET_DATA_AS_CATEGORICAL_AUTOMATICALLY
/// Kyle 11/02/2009 QA80-13902 DISCRETE_FREQUENCY_COPY_SOURCE_COLUMN_FORMAT
***************************************************************************************/


///------ Folger 04/22/09 QA80-13420 PARETOCHART_XF_BASED_PLOTTING
//put your own support static functions here
//#define FREQ_REPORT_TABLE	0x00000010
//enum _DISC_FREQS_RES_ID
//{
	//_DISC_FREQS_DATA	= 0x00010010,
	//_DISC_FREQS_FREQS,
	//_DISC_FREQS_PERC,
	//_DISC_FREQS_CUMUL_PERC,
	//
//};
//
//
//
//static bool _freqs_update_report_data(ReportData& outData, const vector<string>& vstrData, const vector* pvFreqs, const vector* pvPerc, const vector* pvCulmulPerc, LPCSTR lpsczLabel)
//{
	//if (!outData)
		//return false;
	//
	////string strFreqCounts = "Frequency Counts of " + _L(lpsczLabel);	
	//int jj = outData.GetNodeCount()+1;
	//ReportTable rtFreqs = outData.CreateTable("FreqCount"+ftoa(jj),  lpsczLabel, FREQ_REPORT_TABLE);
	//rtFreqs.AddColumn(vstrData, "Data"+ftoa(jj),_DISC_FREQS_DATA+jj, _L("Data"), OKDATAOBJ_DESIGNATION_X);
	/////Echo QA70-12013 v8.0921  LABEL_NOT_CONSISTENT_WITH_FREQ_COUNT
	///*
	//if (pvFreqs)	rtFreqs.AddColumn(*pvFreqs, "Frequencies"+ftoa(jj),_DISC_FREQS_FREQS+jj, _L("Frequencies"), OKDATAOBJ_DESIGNATION_Y);
	//if (pvPerc)	rtFreqs.AddColumn(*pvPerc, "Percent"+ftoa(jj),_DISC_FREQS_PERC+jj, _L("Percent"), OKDATAOBJ_DESIGNATION_Y);
	//if (pvCulmulPerc)	rtFreqs.AddColumn(*pvCulmulPerc, "CumulPerc"+ftoa(jj),_DISC_FREQS_CUMUL_PERC+jj, _L("Cumulative Percent"), OKDATAOBJ_DESIGNATION_Y);
	//*/
	//if (pvFreqs)	rtFreqs.AddColumn(*pvFreqs, "Count"+ftoa(jj),_DISC_FREQS_FREQS+jj, _L("Count"), OKDATAOBJ_DESIGNATION_Y);
	//if (pvPerc)	rtFreqs.AddColumn(*pvPerc, "RelatFreq"+ftoa(jj),_DISC_FREQS_PERC+jj, _L("Relative Frequency"), OKDATAOBJ_DESIGNATION_Y);
	//if (pvCulmulPerc)	rtFreqs.AddColumn(*pvCulmulPerc, "CumulFreq"+ftoa(jj),_DISC_FREQS_CUMUL_PERC+jj, _L("Cumulative Frequency"), OKDATAOBJ_DESIGNATION_Y);
	/////end LABEL_NOT_CONSISTENT_WITH_FREQ_COUNT
	//
	//return true;
	//
//}
///------ End PARETOCHART_XF_BASED_PLOTTING

void _vec_no_case(vector<string>& vstrData)
{
	for (int ii = 0; ii < vstrData.GetSize(); ii++ )
	{
		vstrData[ii].TrimLeft();
		vstrData[ii].TrimRight();
		vstrData[ii].MakeLower();		
	}
}



void discfreqs(const Range& irng, const int& freq, const int& perc, const int& cumul, const int& show, const int& sensitive, ReportData& rd)
{
	
	///------ Folger 04/22/09 QA80-13420 PARETOCHART_XF_BASED_PLOTTING
	//vector<string> vstrData;
	//DWORD dwRules = DRR_NO_WEIGHTS|DRR_GET_MISSING;
	//DWORD dwPlotID;
	//
	//int nErrCode = check_1_data(irng, true, true);
	//if (CER_NO_ERROR != nErrCode)
	//{
		//if (nErrCode == CER_COL_NUM_TOO_FEW_EX)
			//XF_THROW_EX(nErrCode, 1)
		//else
			//XF_THROW(nErrCode);
	//}
	//
	//int nNumRanges = irng.GetNumData(dwRules);	
	/////Arvin 06/22/07 ADD_CASE_SPACE_SENSITIVE
	//DWORD dwCtrl = DF_TRIM_LEFT_RIGHT_SPACE;
	//if(sensitive)
		//dwCtrl |= DF_CASE_SENSITIVE;
	/////end ADD_CASE_SPACE_SENSITIVE
	//for (int ii = 0; ii < nNumRanges; ii++)
	//{
		//int nSize = 0, nFreqs;
		//vector vFreq, vCumulFreqs, vPerc, vCumulPerc;
		//vector<string> vstrDiscData;
		//string strLabel = range_get_col_name(irng, ii);		
		//irng.GetData(dwRules, ii, &dwPlotID, NULL, &vstrData);
		//nSize = vstrData.GetSize();
		//if (nSize == 0)
			//break;
	//
		//vFreq.SetSize(nSize);
		//if (0 != dr_get_col_percent_text(irng, ii))
		//{
			/////Arvin 06/22/07 ADD_CASE_SPACE_SENSITIVE
			////if (!sensitive)
			////	_vec_no_case(vstrData);
			////ocu_discrete_frequencies(&vstrData, &vstrDiscData, vFreq, &nFreqs); 
			//ocu_discrete_frequencies(&vstrData, &vstrDiscData, vFreq, &nFreqs, dwCtrl); 
			/////end ADD_CASE_SPACE_SENSITIVE
		//}else
		//{
			//vector vData, vDiscData(nSize);
			//irng.GetData(dwRules, ii, &dwPlotID, NULL, &vData);
			//vData.Trim();
			//int nMissing = nSize - vData.GetSize();
			//if (nMissing != nSize)
			//{
				//ocmath_discrete_frequencies(vData, vData.GetSize(), vDiscData, vFreq, &nFreqs);
				//vDiscData.Trim();
				//convert_double_vector_to_string_vector(vDiscData, vstrDiscData, nFreqs);
			//}
			//
			////if missing values
			//if (nMissing > 0)
			//{
				//nFreqs++;
				//vstrDiscData.InsertAt(0, "--");
				//vFreq.InsertAt(0, nMissing);
			//}
			//
		//}
		//vFreq.SetSize(nFreqs);
		/////Kyle 11/24/2008 SORT_DEGRESSIVELY_BY_FREQ_COUNT
		//vector<uint> vnIndices;
		//vFreq.Sort(SORT_DESCENDING, true, vnIndices);
		//vstrDiscData.Reorder( vnIndices );
		/////End SORT_DEGRESSIVELY_BY_FREQ_COUNT
	//
		//vCumulFreqs.SetSize(nFreqs);
		//ocmath_d_cumulative_sum(vFreq, 0, vFreq.GetSize(), vCumulFreqs);
		//vPerc = vFreq / nSize;
		//vCumulPerc = vCumulFreqs / nSize;
		//
		//vector* pvFreqs = NULL;
		//vector* pvPerc = NULL;
		//vector* pvCulmulPerc = NULL;
		//
		//if (freq )	pvFreqs = &vFreq;
		//if (perc )	pvPerc = &vPerc;
		//if (cumul )	pvCulmulPerc = &vCumulPerc;
		//
		//_freqs_update_report_data(rd, vstrDiscData, pvFreqs, pvPerc, pvCulmulPerc, strLabel);
		//
	//}	
	///Kyle 07/09/2009 QA80-13902-S1 FREQUENCY_SUPPORT_FRACTION_AND_PERCENT, mispell
	//int		nErrCode = discrite_frequency_update_report_data(rd, irng, freq, perc, cumul, sensitive);
	int nFreqOption = FREQUENCY_NONE;
	int nCumulOption = FREQUENCY_NONE;
	if(perc)
	{
		switch(show)
		{
		case show_fraction:
			nFreqOption = FREQUENCY_IN_FRACTION;
			break;
		case show_percent:
			nFreqOption = FREQUENCY_IN_PERCENT;
			break;
		}
	}
	if(cumul)
	{
		switch(show)
		{
		case show_fraction:
			nCumulOption = FREQUENCY_IN_FRACTION;
			break;
		case show_percent:
			nCumulOption = FREQUENCY_IN_PERCENT;
			break;
		}
	}
	///Kyle 07/09/2009 QA80-13902-P1 DISCRETE_FREQUENCY_SET_DATA_AS_CATEGORICAL_AUTOMATICALLY
	//int		nErrCode = discrete_frequency_update_report_data(rd, irng, freq, nFreqOption, nCumulOption, sensitive);
	///Kyle 11/02/2009 QA80-13902 DISCRETE_FREQUENCY_COPY_SOURCE_COLUMN_FORMAT, by default it will copy the source format in discrete_frequency_update_report_data()
	//int		nErrCode = discrete_frequency_update_report_data(rd, irng, freq, nFreqOption, nCumulOption, sensitive, OKCOLTYPE_CATEGORICAL);
	int		nErrCode = discrete_frequency_update_report_data(rd, irng, freq, nFreqOption, nCumulOption, sensitive);
	///End DISCRETE_FREQUENCY_COPY_SOURCE_COLUMN_FORMAT
	///End DISCRETE_FREQUENCY_SET_DATA_AS_CATEGORICAL_AUTOMATICALLY
	///End FREQUENCY_SUPPORT_FRACTION_AND_PERCENT
	if (CER_NO_ERROR != nErrCode)
	{
		if (nErrCode == CER_COL_NUM_TOO_FEW_EX)
			XF_THROW_EX(nErrCode, 1)
		else
			XF_THROW(nErrCode);
	}
	///------ End PARETOCHART_XF_BASED_PLOTTING
}

//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 discfreqs_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return discfreqs_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	///Kyle 07/09/2009 QA80-13902-S1 FREQUENCY_SUPPORT_FRACTION_AND_PERCENT
	trGetN.show.Enable = (trGetN.perc.nVal || trGetN.cumul.nVal) ? true : false;
	///FREQUENCY_SUPPORT_FRACTION_AND_PERCENT
	int nErrCode;
	nErrCode = check_1_data(trGetN.irng, true, true);
	
	if (nErrCode != CER_NO_ERROR)
	{
		strErrMsg = nErrCode;
		if (nErrCode == CER_COL_NUM_TOO_FEW_EX)
			strErrMsg += ":" + 1;
		bOKEnable = false;
	}
	
	return true;
	

}

int discfreqs_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 discfreqs_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_discfreqs);
	
	if (CER_NO_ERROR != check_1_data(trGetN.irng, true, true) && (dwCntrl&LTXF_THEME_USED) && nGetNDialog <= 0)
		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 discfreqs_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
