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


//put additional include files here
#include <wks2mat.h>
#include <OC_nag8.h>
//put your own support static functions here
#define MIN_GRID_POINTS_FOR_NAG_SHEP 3


void xyz_shep_nag(const XYZRange& iz, const int& rows, const int& cols, const int& q, const int& w, MatrixObject& om)
{
		vector vX, vY, vZ;
		if (!iz.IsValid() || !iz.GetData(vZ, vY, vX))
		{
			XF_THROW(XFERR_INVALID_XYZ_RANGE);
		}
			
		double dXMin, dXMax, dYMin, dYMax;
		vX.GetMinMax(dXMin, dXMax);
		vY.GetMinMax(dYMin, dYMax);
		
		Matrix& mat = om.GetDataObject();
		mat.SetSize(rows, cols);
		om.SetXY(dXMin, dYMin, dXMax, dYMax);
		
		vector vXGrid(rows * cols), vYGrid(rows * cols);
		int iRet = ocmath_mat_to_regular_xyz(NULL, rows, cols, dXMin, dXMax, dYMin, dYMax, vXGrid, vYGrid, NULL);
		if (iRet <= 0)
		{ 
			XF_THROW(CER_FAILED_TO_CREATE_XY_GRIDDING_DATA);
		}
		
		if (vX.GetSize() < MIN_GRID_POINTS_FOR_NAG_SHEP)
		{
			XF_THROW_EX(CER_DATA_TOO_FEW, MIN_GRID_POINTS_FOR_NAG_SHEP);
		}
		
		Nag_2d_Scat_Method method = Nag_Shep;
		Nag_Scat_Struct comm;
		Nag_E01_Opt optional; //For Nag_Shep only.
		NagError error;
		
		//INIT_FAIL
		error.code = NE_NOERROR;
		error.print = 0;
		
		optional.nq = q;
		optional.nw = w;
		optional.rnq = -1.0;
		
		//nag_2d_scat_interpolant 
		e01sac(method, vX.GetSize(), vX, vY, vZ, &comm, &optional, &error);	
		
		if (error.code != NE_NOERROR)
		{
			printf("Error in e01sac: %s\n", error.message);
			e01szc(&comm);
			return;
		}
		
		//nag_2d_scat_eval 	
		e01sbc(&comm, rows*cols, vXGrid, vYGrid, mat, &error);
		
		if (error.code != NE_NOERROR)
		{
			printf("Error in e01sbc: %s\n", error.message);
		}
		
		//nag_2d_scat_free 
		e01szc(&comm);
}

//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 xyz_shep_nag_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return xyz_shep_nag_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

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