#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
#include <ReportTree.h>
#include <report_utils.h>
#include <event_utils.h>
//put additional include files here
///////////////////////////////////////////////////
// Modification log
///Jasmine 08/14/07 QA80-10207 REPORT_SHEET_CHECK_CONFLICT
///Kyle 01/05/2009 CORR_OUTPUT_MESSAGE_ABOUT_COLUMNS_MISSING_VALUES
///////////////////////////////////////////////////

//put your own support static functions here

static int _plotmatrix_check_input(TreeNode trGetN)
{
	int nErrCode = check_input_range(trGetN.irng, trGetN.missing.nVal);
	if (!check_conf_level(trGetN.conflevel.dVal) && nErrCode == CER_NO_ERROR)
		nErrCode = CER_INVALID_CONF_LEV;	
		
	return nErrCode;
}



void plotmatrix(const Range& irng, const int& ellipse, const double& conflevel, const int& fit, const int& missing, ReportData& rd, ReportTree& rt)
{
	
	int nRet = check_input_range(irng, missing);
	if (OE_NOERROR != nRet)
		XF_THROW(nRet);
	
	if (!check_conf_level(conflevel))
		XF_THROW(CER_INVALID_CONF_LEV);
	
	vector<string> vstrFactors;
	matrix mData;
	///Kyle 01/05/2009 CORR_OUTPUT_MESSAGE_ABOUT_COLUMNS_MISSING_VALUES
	//if (!corr_get_data(irng, mData, vstrFactors, missing))
	if (!corr_get_data(irng, mData, vstrFactors, NULL, missing))
	///End CORR_OUTPUT_MESSAGE_ABOUT_COLUMNS_MISSING_VALUES
		XF_THROW(CER_COL_NUM_TOO_FEW);
	
	int nCols = mData.GetNumCols();
	
	
	matrix mFitStat;
	scatter_matrix_update_report_data(mData, vstrFactors, conflevel, ellipse, fit,rd, mFitStat);
	
	int nPlotCntrl = PLOT_MATTRIX_SCATTER;
	if (ellipse)	nPlotCntrl += PLOT_MATTRIX_ELLIPSE;
	if (fit)		nPlotCntrl += PLOT_MATTRIX_FIT;
	plotmatrix_rd_plotting(rt, rd, nCols, mFitStat, nPlotCntrl, vstrFactors)
	
	
}

//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 plotmatrix_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return plotmatrix_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	
	trGetN.conflevel.Enable = trGetN.ellipse.nVal;
	//bool bEnabled = trGetN.ellipse.nVal || trGetN.fit.nVal;
	//if(trGetN.rd) rd_set_optional(nEventID, bOKEnable, lpcszNodeName, trGetN.rd, bEnabled, "PlotData");
	
	int nErrCode = CER_NO_ERROR;
	nErrCode = _plotmatrix_check_input(trGetN);	
	///Jasmine 08/14/07 QA80-10207 REPORT_SHEET_CHECK_CONFLICT
	if (nErrCode == CER_NO_ERROR && is_same_sheet(trGetN.rd, trGetN.rt))
		nErrCode = XFERR_REPORT_SHEET_CONFLICT;
	///End REPORT_SHEET_CHECK_CONFLICT
	if (nErrCode != CER_NO_ERROR)
	{
		strErrMsg = nErrCode;
		bOKEnable = false;
	}
	//DynaCntrlContainer.PostMessage(WM_USER_MSG_ON_DLG, nErrCode);
	
	return true;
	

}

int plotmatrix_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 plotmatrix_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if (nGetNDialog <= 0)
	{
		//if (CER_NO_ERROR !=  _plotmatrix_check_input(trGetN))///Jasmine 09/05/07 max said it's no need to check this here, and dialog opens only when xf is executed from fly-out menu
		//{
			//nRet = XFEVT_PROCEED_WITH_DLG;			
		//}
		///Jasmine 08/14/07 QA80-10207 REPORT_SHEET_CHECK_CONFLICT
		if(is_same_sheet(trGetN.rd, trGetN.rt))
		{
			int nErr = XFERR_REPORT_SHEET_CONFLICT;
			warning_msg_box(nErr, false, 'E');
			nRet = XFEVT_ABORT;
		}
		///End REPORT_SHEET_CHECK_CONFLICT
	}
	
		
}
//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 plotmatrix_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
