#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>


//put additional include files here
#include <fft_utils.h>
#include <fftEx_utils.h>
///////////////////////////////////////////////////////////////////////////////
// This function computes the mean and standard deviation of the data in 
// the matrix and then sets the display range of the matrix to be between
// (mean - sd) and (mean + sd)
//





void ifft2(const MatrixObject& im, const int& sc, MatrixObject& om)
{
	waitCursor junk;
	if( !im.IsValid() )
	{
		XF_THROW(XFERR_INVALID_MATOBJ);
	}	
	//if( !check_matrix_has_data(im) )
		//throw(ERR_INVALID_MATRIXOBJECT);	
	//matrixbase<complex>& mat = im.GetDataObject();
	matrixbase& mat = im.GetDataObject();
	
									
	int nRows = mat.GetNumRows();
	int nCols = mat.GetNumCols();
	
	om.SetNumRows(nRows);
	om.SetNumCols(nCols);
	
	
	om.SetInternalData(FSI_COMPLEX);
	matrix<complex>& mout = om.GetDataObject();
	mout = mat;	
	
	/// Fisher	07/29/09 QA80-14034 SET_2DFFT_FREQUENCY_DOMAIN
	double dx, dy;
	get_sampling_interval_2d(im, dx, dy);
	set_frequency_domain_2d(om, dx, dy, 0);
	/// end SET_2DFFT_FREQUENCY_DOMAIN
	
	int iRet;
	
	if (sc)
	{
		if (0 != nRows%2)
			mout.Wrap(nRows-1, 0);
		if (0 != nCols%2)
			mout.Wrap(0, nCols-1);
		iRet = mout.FFTShift(mout);
		if (-2 == iRet)
		{
			//XF_THROW_EX("Xfunction failed, Code = %d", iRet);
			XF_THROW(iRet);
		}
	}
	iRet = fft_2d_complex(nRows, nCols, mout, FFT_BACKWARD);
	
	
	mout /= pow(sqrt(nRows*nCols),2);
	
	if( 0 != iRet )
	{
		//XF_THROW_EX("Xfunction failed, Code = %d", iRet);
		XF_THROW(iRet);
	} 
	
	
	MatrixLayer ml;
	om.GetParent(ml);
	//ml.SetName("Complex");
	MatrixPage mp = ml.GetPage();
	
	/// use option string N
	/*
	if(mp)
	{
		string strLabel = "Inverse FFT of " ;
		MatrixLayer mlIn;
		im.GetParent(mlIn);
		MatrixPage mpIn = mlIn.GetPage();
		
		strLabel.Insert(strLabel.GetLength(), mpIn.GetName());
	
		mp.SetLongName(strLabel);
		mp.TitleShow = WIN_TITLE_SHOW_BOTH;
	}
	*/
	
}

//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 ifft2_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return ifft2_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	
	//printf("now = %d\n, trGetN.norm.show = %d, trGetN.amp.Enable = %d\n", nRow, trGetN.norm.Show, trGetN.amp.nVal);
	//if (nRow == 2 || nRow < 0) // the amplitude row
	//{
	//trGetN.norm.Show = trGetN.amp.nVal;
	//trGetN.aout.Show = trGetN.amp.nVal;
	//return true;
	//}
	//if (nRow == 4 || nRow < 0) // the phase row
	//{
	//trGetN.pout.Show = trGetN.phase.nVal;
	//return true;
	//}
	
	//Sandy 2007-7-11 add for check input
	MatrixObject im;
	if(!okxf_resolve_string_get_origin_object(trGetN.im.strVal, &im)||!im.HasData(false))
	{
		///Sophy 6/24/2010 ORG-322-P1 BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
		//strErrMsg = CER_INVALID_MATOBJ;
		if ( !im.IsValid() )
			strErrMsg = CER_INVALID_MATOBJ;
		else
		{
			strErrMsg = CER_NEED_CVT_TO_DATA;
		}
		///end BETTER_ERR_MSG_INFORM_CVT_IMAGE_TO_DATA
		bOKEnable = false;
		return true;
	}	
	
	
	return false;
	
	

}

int ifft2_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 ifft2_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	//Sandy 2007-7-11 add for check input
	
	if(0 == nGetNDialog)
	{
		MatrixObject im;
		if(!okxf_resolve_string_get_origin_object(trGetN.im.strVal, &im)||!im.HasData(false))
		{
			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 ifft2_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
