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

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 07/09/09 QA80-13904 MEXPAND_FAILS_TO_COPY_MATRIX_XY_RANGE_TO_OUTPUT	*
 *	Sophy 6/24/2010 MEXPAND_ON_MAT_OBJ_CAUSE_OTHER_MATS_FILLED_WITH_ZEROS		*
 *------------------------------------------------------------------------------*/

#include <event_utils.h>
//put additional include files here
 

//put your own support static functions here
static void _pre_expand_small_matrix_to_big(matrix& mSrc, matrix& mDest)
{
	int nCols = mSrc.GetNumCols();
	int nRows = mSrc.GetNumRows();
	int nNewCols = (nCols < 3)?3 : nCols;
	int nNewRows = (nRows < 3)?3 : nRows;
	
	mDest.SetSize(nNewRows, nNewCols);
	mDest.SetSubMatrix(mSrc);
	if(nCols ==1)
	{
		for(int ii = 0; ii < nRows; ii++)
		{
			mDest[ii][1] = mDest[ii][0];
			mDest[ii][2] = mDest[ii][0];			
		}
	}
	if(nCols == 2)
	{
		for(int ii =0; ii < nRows; ii++)
		{
			mDest[ii][2] = mDest[ii][1];
			mDest[ii][1] = (mDest[ii][0] + mDest[ii][2]) / 2;
		}
	}
	if(nRows == 1 )
	{
		for(int ii = 0; ii < nNewCols; ii++)
		{
			mDest[1][ii] = mDest[0][ii];
			mDest[2][ii] = mDest[0][ii];
		}
	}
	if(nRows == 2)
	{
		for(int ii =0; ii < nNewCols; ii++)
		{
			mDest[2][ii] = mDest[1][ii];
			mDest[1][ii] = (mDest[0][ii] + mDest[2][ii]) / 2;
		}		
	}
}

int _check_factors(int nCols, int nRows)
{
	if(nCols <1 || nRows < 1)
	    return XFERR_INVALID_ARG;
	return CER_NO_ERROR;
}

void mexpand(const MatrixObject& im, const int& cols, const int& rows, MatrixObject& om)
{
	///------ Folger 07/09/09 QA80-13904 MEXPAND_FAILS_TO_COPY_MATRIX_XY_RANGE_TO_OUTPUT
	if(!matobj_copy(om,im))
	{
		XF_THROW(MATOBJ_COPY_FAILED);
	}
	///------ End MEXPAND_FAILS_TO_COPY_MATRIX_XY_RANGE_TO_OUTPUT
	
	//if( cols <1 || rows < 1 )
	//{
		//XF_THROW(XFERR_INVALID_ARG);
	//}
	int nErr = _check_factors(cols, rows);
	if(CER_NO_ERROR != nErr)
		XF_THROW(nErr);
	
	//justin 09/08/2006 SUPPORT_MANY_DATA_TYPES
	//matrix& mat = im.GetDataObject();
	int nType = im.GetInternalData();
	matrix mat;
	int nRet = im.GetDataObject().CastToDouble(mat);
	if(nRet)
	{
		XF_THROW(CER_UNKNOWN_ERROR) //XF_THROW(nRet);	//Justin 2007-07-11 HADLE_MORE_MEANINGFULL_ERROR
	}
	//end SUPPORT_MANY_DATA_TYPES
	
	int nNewCols = mat.GetNumCols() * cols;
	int nNewRows = mat.GetNumRows() * rows; 
	
	//prepare xy coordinats
	vector vX, vY, vZ, vTmp1, vTmp2;
	vTmp1.Data(0., 1., (nNewCols ==1)? 1.0: 1./(nNewCols -1.) );
	vTmp2.SetSize(nNewCols);
	vTmp2 = 0;
	for(int ii = 0; ii < nNewRows; ii++)
	{
		vX.Append(vTmp1);
		vY.Append(vTmp2);
		vTmp2 += 1.0/(nNewRows-1);
	}
	vZ.SetSize(nNewCols*nNewRows);
	
	matrix mTmp;
	_pre_expand_small_matrix_to_big(mat, mTmp);
	
	nRet = ocmath_2d_interpolate(nNewCols*nNewRows, vX, vY, vZ, mTmp.GetNumRows(), mTmp.GetNumCols(), mTmp, 0., 1., 0., 1., INTERP2_BIQUADRATIC);
	if(nRet)
	{
			XF_THROW_EX(CER_OCMATH_ERR_CODE, nRet);  //XF_THROW(nRet);	 ///Justin 2007-07-11 HADLE_MORE_MEANINGFULL_ERROR
	}
	
	//output
	///Sophy 6/24/2010 MEXPAND_ON_MAT_OBJ_CAUSE_OTHER_MATS_FILLED_WITH_ZEROS we do NOT allow matrixobjects with different dimensions on same layer, so it is reasonable to set size on layer level.
	//matrixbase& matDest = om.GetDataObject();
	//matDest.SetSize(nNewRows, nNewCols);
	MatrixLayer ml;
	om.GetParent(ml);
	ml.SetSize(-1, nNewRows, nNewCols);
	matrixbase& matDest = om.GetDataObject();
	///end MEXPAND_ON_MAT_OBJ_CAUSE_OTHER_MATS_FILLED_WITH_ZEROS
	matDest.SetByVector(vZ);
	om.SetInternalData(nType);
	
}

//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 mexpand_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return mexpand_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	int nErr =  check_numeric_matrix(trGetN.im.strVal);
	if(CER_NO_ERROR != nErr)
	{
		strErrMsg = nErr;
		bOKEnable = false;
	}	
	
	nErr = _check_factors(trGetN.cols.nVal, trGetN.rows.nVal);
	if ( CER_NO_ERROR != nErr)
	{
		strErrMsg = nErr;
		bOKEnable = false;
	}
		
	return true;
	

}

int mexpand_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 mexpand_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if ( nGetNDialog == 0 )
	{
		int nErr = check_numeric_matrix(trGetN.im.strVal);
		if ( nErr != CER_NO_ERROR )
		{
			warning_msg_box(nErr, false);
			nRet = XFEVT_ABORT;			
		}
		
		nErr = _check_factors(trGetN.cols.nVal, trGetN.rows.nVal);	
		if ( nErr != CER_NO_ERROR )
		{
			warning_msg_box(nErr, false);
			nRet = XFEVT_ABORT;			
		}
	}
}
//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 mexpand_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
