#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

//put your own support static functions here
//---------CPY 12/4/2007 CHANGE_N_TO_DOUBLE_AND_ADDED_PTS
//---CPY 12/16/07 FIT_LR_R_VALUE_SHOULD_NOT_USE_ADJ_R_SUMSQR
///------ Folger 10/20/2010 ORG-1286-P1 MORE_TOOLS_FAILED_TO_COPY_SAMPLING_INTERVAL_FROM_INPUT


void fitLR(const XYRange& iy, const int& N, double& a, double& b, double& aerr, double& berr, double& r, int& pts, XYRange& oy)
{
	
	vector vInputX, vInputY, vOutputY;
	int nSize;
	
	// Copy X, Y data from XYRange into vectors
	if( !iy.GetData(vInputY, vInputX) )
		XF_THROW(XFERR_GET_DATA_FAILED);
	
	if( 0==N )
		nSize=vInputX.GetSize();
	else if( 0<N )
		nSize=N;
	else{	// 0>N
		nSize=N*(-1);
		// Get the last (-N) elements, here N<0
		vector vTmp;
		vInputX.GetSubVector(vTmp, vInputX.GetSize()+N);
		vInputX=vTmp;
		vInputY.GetSubVector(vTmp, vInputY.GetSize()+N);
		vInputY=vTmp;
	}
	
	// Perform simple linear regression
	FitParameter sFitParameter[2];
	RegStats sRegStats;
	int nRet = ocmath_linear_fit(vInputX, vInputY, nSize, sFitParameter, NULL, 0, NULL, &sRegStats);
	if( OE_NOERROR != nRet )
		XF_THROW_EX(CER_UNKNOWN_ERROR_EX, nRet);
	
	a = sFitParameter[0].Value;		//Intercept
	aerr = sFitParameter[0].Error;
	b = sFitParameter[1].Value;		//Slope
	berr = sFitParameter[1].Error;
	//---CPY 12/16/07 FIT_LR_R_VALUE_SHOULD_NOT_USE_ADJ_R_SUMSQR
	//R = sRegStats.AdjRSq;			//Adjusted residuel sum of squares
	r = sRegStats.Rvalue;
	if(b < 0)
		r = -sRegStats.Rvalue;
	//---
	pts = sRegStats.N; // ------ CPY 12/4/2007 CHANGE_N_TO_DOUBLE_AND_ADDED_PTS	
	// Set the output dataset
	if(oy){
		vInputX.SetSize(nSize);
		vOutputY=b*vInputX+a;
		///------ Folger 10/20/2010 ORG-1286-P1 MORE_TOOLS_FAILED_TO_COPY_SAMPLING_INTERVAL_FROM_INPUT
		//oy.SetData(&vOutputY, &vInputX, 0, NULL, DRS_SET_COL_DESIGNATIONS);
		set_xyrange_data_ex(oy, &vOutputY, &vInputX, 0, iy);
		///------ End MORE_TOOLS_FAILED_TO_COPY_SAMPLING_INTERVAL_FROM_INPUT
	}
	
}

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

}

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