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

#include <o8dlg.h>
#include <..\OriginLab\PAWizCore.h>
#include <..\OriginLab\XFWizard_utils.h>
#include <ReportTree.h>

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 10/20/09 PA_SUBTRACT_BASELINE_FAILS_TO_KEEP_DATA_OUT_OF_SELECTED_RANGE
 *------------------------------------------------------------------------------*/

///Sandy 2009-1-24 MOVE_TO_H_FILE_FOR_SHARE_WITH_PAWIZCORE_C
//#define _STR_BUTTON_SUBTRACT ("Subtract")
//#define _STR_BUTTON_NO_SUBTRACT ("No Subtract")
#define _BASELINE_RDID_TABLE   200
#define _BASELINE_X_RDID_TABLE 201
#define _BASELINE_Y_RDID_TABLE 202
#define _SUBTRACTED_RDID_TABLE   300
#define _SUBTRACTED_X_RDID_TABLE 301
#define _SUBTRACTED_Y_RDID_TABLE 302

///Kyle 12/24/2008 ADD_COMMENTS_FOR_OUTPUT_DATA
static	void	_set_output_range_comments(const XYRange& iy, XYRange& oy, XYRange& oyb)
{
	string strRng = get_input_data_range_string(iy);
	string	strComments;
	if(oy)
	{
		strComments.Format(_L("Subtracted from %s"), strRng);
		oy.SetOutputComment(strComments);
	}
	if(oyb)
	{
		strComments.Format(_L("Baseline to Subtract for %s"), strRng);
		oyb.SetOutputComment(strComments);
	}
}
///End ADD_COMMENTS_FOR_OUTPUT_DATA


void pa_basesubtr(const XYRange& iy, const int& btn_subtract, const int& subtract, const int& nXFCorePointer, XYRange& oy, XYRange& oyb)
{
	///---Sim 01-07-2008 QA80-12833 FIX_APPLY_XF_WIZ_XF_TWICE
	//PAWizCore* paCore = (PAWizCore*)get_xf_core_handler(getnhandler);
	PAWizCore* paCore = (PAWizCore*)get_xf_core_handler(NODE_NAME_XF_WIZ_CORE_POINTER);
	///---END QA80-12833 FIX_APPLY_XF_WIZ_XF_TWICE
	if(!paCore || !create_subtracted_plot(paCore, false))
		XF_THROW(XF_ERR_INVALID_PA_INSTANCE);	
		
	XYRange xyBaseline, xySubtracted; 
	int nTableFormat = GETNBRANCH_OPEN | GETNBRANCH_HIDE_COL_HEADINGS| GETNBRANCH_HIDE_ROW_HEADINGS | GETNBRANCH_FIT_COL_WIDTH | GETNBRANCH_FIT_ROW_HEIGHT;	
	//if(rd_oys)
	//{
		//vector vsy, vsx;
	    //if(!paCore->GetSubtractedRange(xySubtracted)||!xySubtracted.GetData(vsy, vsx))
	    		//XF_THROW(XF_ERR_INVALID_PA_INSTANCE);	
		//ReportTable rt;
		//rt = rd_oys.CreateTable("Subtracted", _L("Subtracted"), _SUBTRACTED_RDID_TABLE);
		//rt.AddColumn(vsx, "X", _SUBTRACTED_X_RDID_TABLE, "X", OKDATAOBJ_DESIGNATION_X);
		//rt.AddColumn(vsy, "Y", _SUBTRACTED_Y_RDID_TABLE, "Y", OKDATAOBJ_DESIGNATION_Y);
	//}
	
	if(oy)
	{
		vector vsy, vsx;
	    if(!paCore->GetSubtractedRange(xySubtracted)||!xySubtracted.GetData(vsy, vsx))
	    		XF_THROW(XF_ERR_INVALID_PA_INSTANCE);	
	    ///------ Folger 10/20/09 PA_SUBTRACT_BASELINE_FAILS_TO_KEEP_DATA_OUT_OF_SELECTED_RANGE
		//oy.SetData(&vsy, &vsx, 0, NULL, DRS_SET_COL_DESIGNATIONS);
		oy.SetData(&vsy, &vsx, 0, NULL, DRS_KEEP_LARGER_DEST_SIZE | DRS_SET_COL_DESIGNATIONS);
		///------ End PA_SUBTRACT_BASELINE_FAILS_TO_KEEP_DATA_OUT_OF_SELECTED_RANGE
	}
	
	//Sandy 2008-11-11 change to XYRange output, keep same format with subtracted data
	//if(rd_oyb)
	//{
		//vector vby, vbx;
	    //if(!paCore->GetBaselineRange(xyBaseline) || !xyBaseline.GetData(vby, vbx))
	    		//XF_THROW(XF_ERR_INVALID_PA_INSTANCE);	
	//
		//ReportTable rt;
		//rt = rd_oyb.CreateTable("Baseline", _L("Baseline"), _BASELINE_RDID_TABLE);
		//rt.AddColumn(vbx, "X", _BASELINE_X_RDID_TABLE, "X", OKDATAOBJ_DESIGNATION_X);
		//rt.AddColumn(vby, "Y", _BASELINE_Y_RDID_TABLE, "Y", OKDATAOBJ_DESIGNATION_Y);
	//}
	if(oyb)
	{
		vector vby, vbx;
	    if(!paCore->GetBaselineRange(xyBaseline) || !xyBaseline.GetData(vby, vbx))
	    		XF_THROW(XF_ERR_INVALID_PA_INSTANCE);	
	
		oyb.SetData(&vby, &vbx, 0, NULL, DRS_SET_COL_DESIGNATIONS);
	}	
	///Kyle 12/24/2008 ADD_COMMENTS_FOR_OUTPUT_DATA
	_set_output_range_comments(iy, oy, oyb);
	///End ADD_COMMENTS_FOR_OUTPUT_DATA
	
}

//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 pa_basesubtr_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return pa_basesubtr_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	PAWizCore* paCore = (PAWizCore*)get_xf_core_handler(trGetN);
	if(!paCore)
		return false;
		
	int goal = paCore->GetCurrGoal();
	if(  0 == lstrcmp(lpcszNodeName, "btn_subtract") || nEventID == GETNE_ON_UNDO)
	{
		bool bUndoSubtract = ((nEventID == GETNE_ON_UNDO)? true : (bool)(trGetN.subtract.nVal));
		if(!create_subtracted_plot(paCore, bUndoSubtract))
			error_report("fail to create subtracted plot");
			
		if(bUndoSubtract)
		{
			trGetN.subtract.nVal = false;
			//trGetN.btn_subtract.SetAttribute(STR_COMBO_ATTRIB, _STR_BUTTON_SUBTRACT);///Sandy 2009-1-24 MOVE_TO_H_FILE_FOR_SHARE_WITH_PAWIZCORE_C
			trGetN.btn_subtract.SetAttribute(STR_COMBO_ATTRIB, PA_BASELINE_STR_BUTTON_SUBTRACT);
		}
		else
		{
			trGetN.subtract.nVal = true;
			//trGetN.btn_subtract.SetAttribute(STR_COMBO_ATTRIB, _STR_BUTTON_NO_SUBTRACT);///Sandy 2009-1-24 MOVE_TO_H_FILE_FOR_SHARE_WITH_PAWIZCORE_C
			trGetN.btn_subtract.SetAttribute(STR_COMBO_ATTRIB, PA_BASELINE_STR_BUTTON_NO_SUBTRACT);
		}
		return true;
	}	
	
	return false;

}

int pa_basesubtr_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 pa_basesubtr_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if(nGetNDialog>=0)
	{
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_PA_BASELINE_SUBTRACT);
		
		PAWizCore* paCore = (PAWizCore*)get_xf_core_handler(trGetN);
		if(!paCore)
		{
			nRet = XFEVT_ABORT;
			return;
		}		
		
		//Sandy 2008-10-23 add for output "<input>,<input>" range to source wks
		paCore->SetOriginalRangeToGetN(trGetN.iy);	
		
		///sandy 2009-1-19 FOR_SNOW_SUGGEST_IGNORE_BASELINE_OUTPUT_IF_EXISTING
		int goal = paCore->GetCurrGoal();
		if(PA_BM_USE_EX != paCore->GetBaseMode())
		{
			
			///Sandy 2009-1-21 Improve again
			//trGetN.oyb.Show = true;
			bool		bPreShow = trGetN.oyb.Show;
			trGetN.oyb.Show = true;
			
			if ( !bPreShow )
				trGetN.oyb.SetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, 1);
			//if(!(dwCntrl & LTXF_CHANGE_PARAM) && !(dwCntrl & LTXF_EXEC_RECALCULATE) )
			//{
				//trGetN.oyb.SetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, 1);
			//}
			
		}
		else
		{
			trGetN.oyb.SetAttribute(STR_ATTRIB_DYNACONTROL_USE_CHECK, 0);
			trGetN.oyb.Show = false;
		}
		///end FOR_SNOW_SUGGEST_IGNORE_BASELINE_OUTPUT_IF_EXISTING
	
		if(nGetNDialog>0)
		{	
			///subtract preview button format
			TreeNode tr = trGetN.btn_subtract;
			if(tr)
			{
				tr.SetAttribute(STR_ID_ATTRIB, ONODETYPE_PUSHBUTTON_GROUP);
				//tr.SetAttribute(STR_COMBO_ATTRIB, _STR_BUTTON_SUBTRACT);///Sandy 2009-1-24 MOVE_TO_H_FILE_FOR_SHARE_WITH_PAWIZCORE_C			
				tr.SetAttribute(STR_COMBO_ATTRIB, PA_BASELINE_STR_BUTTON_SUBTRACT);			
				DWORD dwDisplyBits = DISPLAY_EDITOR_HORZ;
				tr.SetAttribute(STR_ATTRIB_DISPFMT, (string)dwDisplyBits);
				trGetN.btn_subtract.nVal = 0;
			}
		}
	}
	
}
//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 pa_basesubtr_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
