#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	type_Haar,
	type_DB2,
	type_DB3,
	type_DB4,
	type_DB5,
	type_DB6,
	type_DB7,
	type_DB8,
	type_DB9,
	type_DB10,
	type_B11,
	type_B13,
	type_B15,
	type_B22,
	type_B24,
	type_B26,
	type_B28,
	type_B31,
	type_B33,
	type_B35,
	type_B37,
};
enum{
	ext_P,
	ext_Z,
};
#include <event_utils.h>
#include <o8dlg.h>

//put additional include files here
//#include	<nag8\OC_nag_ex.h>
#include <xfutils.h>
#include <..\OriginLab\wavelet_utils.h>
//put your own support static functions here
//enum {
	//CREATE_WAVELET_FILTER_ERROR = XF_LOCAL_ERR_BEGIN,
//};

/***************************************************************************************
/// Modification Log :
///	Folger 07/28/08 BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS
/// Max 4/15/09 v8.0981_new NO_POP_UP_DLG_WHEN_ERR_OCCURS_FROM_LT
***************************************************************************************/

//------ Folger 07/28/08 BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS
static	void	_set_output_range_comments(vector& ox, const vector& ca, const vector& cd, int nType)
{
	string	strComments;
	strComments.Format("%s idwt of %s, %s", get_wavelet_type_string(nType), get_input_data_range_string(ca.GetSourceDataRange()), get_input_data_range_string(cd.GetSourceDataRange()));
	ox.SetOutputComment(strComments);
}
//------ End BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS


void idwt(const vector& ca, const vector& cd, const int& type, const int& ext, vector& ox)
{
	
	waitCursor junk;
	
	Nag_WavFilt filter;
	//Nag_WavType wavname = nWaveletType[type];
	int i, mshift, n, nc, ny;
	//double maxerr;
	
	
	/* Initialise filter coefficients */
	if(!create_wavelet_filter(g_nWaveletType[type], filter))
		XF_THROW(XFERR_WT_FAILED_TO_CREATE_FILTER);
	//nag_wavfilter(nWaveletType[type], &filter, &fail);
	//
	//if (fail.code != NE_NOERROR)
	//{
		//nag_free_wavfilter(&filter);
		////printf(fail.message);
		//XF_THROW(CREATE_WAVELET_FILTER_ERROR);
	//
	//}
	
	/* Read end extension mode */
	//vector<string> vWaveMode = {"Periodic", "Zero-padded"};
	
	Nag_WavModeType mode = g_nWaveModes[ext];
	
	
	//if (!strcmp("Periodic",vWaveMode[ext]))
	//{
	  ////mode = Nag_PerWavExt;
	  //nc = (n & 1 ? (n + 1)/2 : n/2);
	//}
	//else if (!strcmp("Zero-padded",vWaveMode[ext]))
	//{
	  ////mode = Nag_ZeroWavExt;
	  //nc = ((n + nf - 1) & 1 ? (n + nf)/2 : (n + nf - 1)/2);
	//}
	//else
	//{
	  //printf("mode type not recognised\n"); 
	  //goto END; 
	//}
	
	nc = ca.GetSize();
	
	/* Reconstruction: Allocate array y */
	ny = get_reconstruct_size(mode, nc, filter);
	
	
	ox.SetSize(ny);
	
	/* 1D Inverse Discrete Wavelet Transform (DWT) (one level) */
	NagError fail;
	fail.code = NE_NOERROR;
	nag_idwt(mode, nc, ca, cd, &filter, ny, ox, &fail);
	if (fail.code != NE_NOERROR)
	{
		//XF_THROW_EX("Xfunction failed, Code = %d", fail.code);
		XF_THROW_EX(XFERR_WT_FUNCTION_UNKNOWN_ERR, fail.code);
	  //printf("Error from nag_idwt.\n%s\n", fail.message);
	  //goto END;
	}
	
	
	//printf("Reconstruction Results\n");
	//if (mode == Nag_ZeroWavExt)
	//{
	  //if (wavname == Nag_Bior3_1 || wavname == Nag_Bior3_3 || wavname == Nag_Bior3_5 || wavname == Nag_Bior3_7)
			//mshift = - MIN(filter.lrsh, filter.hrsh) + 1;
	  //else
			//mshift =  MAX(filter.nlr-1+filter.lrsh,filter.nhr-1+filter.hrsh);
	//}
	//else
		//mshift = 0;
	
	//for (i = 0; i < ny; i++)
	//	printf("ox[%ld] = %f\n", i, ox[i]);
	
	//maxerr = 0.0;
	//for (i = 0; i < n; i++)
	//maxerr = MAX(maxerr, FABS(x[i] - ox[i+mshift]));
	//printf("MAX ERR = %e\n", maxerr);
	//
	//END:
	
	nag_free_wavfilter(&filter);
	
	//------ Folger 07/28/08 BETTER_OPREATION_OUTPUT_COLUMN_COMMENTS
	_set_output_range_comments(ox, ca, cd, type);
	//------	
}

//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 idwt_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return idwt_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
	
	if(!get_error_for_input_double_vector(trGetN.ca, strErrMsg) || !get_error_for_input_double_vector(trGetN.cd, strErrMsg) )
	{
		bOKEnable = false;
	}
	return true;
	

}

int idwt_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 idwt_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_idwt);
	/// Max 4/15/09 v8.0981_new NO_POP_UP_DLG_WHEN_ERR_OCCURS_FROM_LT
	// if(nGetNDialog == 0)
	if(0 == nGetNDialog && (dwCntrl&LTXF_THEME_USED) && (dwCntrl&LTXF_FROM_GUI_MENU) )
	/// ---
	{
		string strErrMsg;
		if(!get_error_for_input_double_vector(trGetN.ca, strErrMsg) || !get_error_for_input_double_vector(trGetN.cd, strErrMsg) )
		{
			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 idwt_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
