#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_morlet,
	type_dgauss,
	type_mexhat,
};
//#include <getnbox.h> // temp solution

#include <..\OriginLab\wavelet_utils.h>
//put your own support static functions here
#include <event_utils.h>



void cw_evaluate(const int& type, const int& param, const double& lb, const double& ub, const int& npts, XYRange& oy)
{
	
	
	NagError fail;
	//INIT_FAIL
	fail.code = NE_NOERROR;
	fail.print = 0;
	
	
	Nag_WavType nagType = g_nCwtWaveletType[type];
	if(nagType == Nag_Morlet && (param<5 || param>20))
		XF_THROW(INTOPTN_OUT_OF_RANGE);
	if(nagType == Nag_DGauss && (param<0 || param>8))
		XF_THROW(INTOPTN_OUT_OF_RANGE);
	
	double xstep = (ub - lb)/(double)(npts-1);
	vector vx, vwav;
	vx.Data(lb, ub, xstep);
	
	int n = vx.GetSize();
	vwav.SetSize(n);
	if(nagType == Nag_Morlet)
	{
		nag_morlet(param, n, vx, vwav, &fail);
	}
	else if(nagType == Nag_DGauss)
	{
		nag_dgauss(param, n, vx, vwav, &fail);
	}
	else if(nagType == Nag_MexHat)
	{
		nag_mexhat(n, vx, vwav, &fail);
	}
	
	if (fail.code != NE_NOERROR)
	{
	      XF_THROW_EX(XFERR_WT_FUNCTION_UNKNOWN_ERR, fail.code);
	      //XF_THROW_EX(CER_UNKNOWN_ERROR_EX, fail.code);
	}
	
	if(!oy.SetData(&vwav, &vx))
		XF_THROW(XFERR_FAIL_IN_SET_XYRANGE_DATA);
	
	
}

//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 cw_evaluate_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return cw_evaluate_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.type.SetAttribute(STR_OCNTRL_OPTNS, OCNTRLPARAM1_NOT_LOCALIZED);
	}
	///End SET_NO_NEED_TO_LOCALIZE
	
	Nag_WavType nagType = g_nCwtWaveletType[trGetN.type.nVal];
	if(nagType == Nag_DGauss)
	{
		trGetN.param.Show = true;			
		trGetN.param.SetAttribute(STR_LABEL_ATTRIB, _L("Derivative Order"));
		trGetN.param.SetAttribute(STR_COMBO_ATTRIB, "1|2|3|4|5|6|7|8");
		
		if( 0 == lstrcmp(lpcszNodeName, "type"))
		{
			trGetN.param.nVal = 3;
		}
	}
	else if(nagType == Nag_MexHat)
		trGetN.param.Show = false;
	else
	{
		trGetN.param.Show = true;			
		trGetN.param.SetAttribute(STR_LABEL_ATTRIB, _L("Wave Number"));
		trGetN.param.SetAttribute(STR_COMBO_ATTRIB, "5|6|7|8|9|10|15|20");
		if( 0 == lstrcmp(lpcszNodeName, "type"))
		{
			trGetN.param.nVal = 5;
		}
	}
	
	
	
	if(nagType == Nag_Morlet && (trGetN.param.nVal<5 || trGetN.param.nVal>20))
	{
		strErrMsg = INTOPTN_OUT_OF_RANGE;
		bOKEnable = false;
		return true;
	}
	if(nagType == Nag_DGauss && (trGetN.param.nVal<0 || trGetN.param.nVal>8))
	{
		strErrMsg = INTOPTN_OUT_OF_RANGE;
		bOKEnable = false;
		return true;
	}
	
	return true;

}

int cw_evaluate_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return true;
}
//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 cw_evaluate_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
}
//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 cw_evaluate_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
