#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
//--------Folger 07/09/07 IMPROVE_XF_FILLTEXT_IS_NEED
#include <event_utils.h>
//put your own support static functions here
//static string _get_cell_text(int nr, int nc)
//{
	//// random, use only the following for now
	//vector<string> vs = {"east", "south", "west", "north"};
	//
	//double rr = rnd();
	//int nSize = vs.GetSize();
	//int nn = 0.5 + rr * (nSize-1);
	//
	//return vs[nn];
//}

#define ALPHABET_NUM 	26
#define ALPHABET_A		'A'

static void _construct_group_string(int nGroup, vector<string> &vsGroup)
{
	int nBound = nGroup > ALPHABET_NUM ? ALPHABET_NUM : nGroup;
	vsGroup.SetSize(nGroup);
	for (int ii=0; ii<nBound; ii++)
		vsGroup[ii] = (char)(ALPHABET_A + ii);
	char nFirst = ALPHABET_A, nSecond = ALPHABET_A;
	for (; ii<nGroup; ii++)
	{
		string str = nFirst;
		str += nSecond;
		vsGroup[ii] = str;
		if (++nSecond - ALPHABET_A == ALPHABET_NUM)
		{
			nFirst++;
			nSecond = ALPHABET_A;
		}
	}
}

static int _get_cell_text(int nGroup)
{
	double rr = rnd();
	return 0.5 + rr * (nGroup-1);
}
//--------end IMPROVE_XF_FILLTEXT_IS_NEED


void filltext(const int& group, Range& rng)
{
	//--------Folger 07/09/07 IMPROVE_XF_FILLTEXT_IS_NEED
	vector<string> vsGroup;
	_construct_group_string(group, vsGroup);
	//--------end IMPROVE_XF_FILLTEXT_IS_NEED
	///---Sim 12-14-2007 FIX_INPUT_ACTIVE_RANGE
	if ( !rng || rng.GetNumRanges() <= 0 )
		XF_THROW(CER_INVALID_RANGE);
	///---END FIX_INPUT_ACTIVE_RANGE
	Worksheet wks;
	int c1, c2, r1, r2;
	rng.GetRange(0, r1, c1, r2, c2, wks);
	if(!wks)
		return;
	if(r2 < r1)
		r2 = wks.GetNumRows()-1;
	///---Sim 12-14-2007 FIX_INPUT_ACTIVE_RANGE
	if(c2 < c1)
		c2 = wks.GetNumCols()-1;
	///---END FIX_INPUT_ACTIVE_RANGE
	
	for(int nc = c1; nc <= c2; nc++)
	{
		for(int nr = r1; nr <= r2; nr++)
		{
			//--------Folger 07/09/07 IMPROVE_XF_FILLTEXT_IS_NEED
			//string str = _get_cell_text(nr, nc);
			//wks.SetCell(nr, nc, str);
			wks.SetCell(nr, nc, vsGroup[_get_cell_text(group)]);
			//--------end IMPROVE_XF_FILLTEXT_IS_NEED
		}
	}
	
}

//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 filltext_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return filltext_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//--------Folger 07/09/07 IMPROVE_XF_FILLTEXT_IS_NEED
	if (trGetN.group.IsEmpty())
	{
		strErrMsg = CER_EMPTY_VARIABLE;
		strErrMsg += ":" + _L("Group number");
		bOKEnable = false;
		return true;
	}
	if (trGetN.group.nVal < 0)
	{
		strErrMsg = CER_INVALID_POSITIVE_INTEGER;
		strErrMsg += ":" + _L("Group number");
		bOKEnable = false;
		return true;
	}
	int nErrMsg;
	if ((nErrMsg = check_1_data(trGetN.rng, false)) == CER_COL_NUM_NOT_ONE)
	{
			strErrMsg = nErrMsg;
			strErrMsg += ":" + _L("1");
	}
	//--------end IMPROVE_XF_FILLTEXT_IS_NEED
	return false;

}

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