//******************************************************************************
///---Sim 11-07-2008 FIX_REOPEN_PA_OPJ_FILE_TO_RECALCULATE
#ifdef _FOR_SMART_LOADING_ONLY
#include <event_utils.h> /// Hong 02/03/10 QA80-15016 FIX_X_DATA_TYPE_CHANGE_WHEN_RECALCUTE
#include <..\OriginLab\XFWiz.h>

#include <..\OriginLab\WksOperation.h> ///---Sim 02-11-2009 FIX_WIZ_OPERATION_LABEL
#include <..\OriginLab\WizOperation.h>

#include <..\OriginLab\XFCore.h>

#include <..\OriginLab\XFWizNavigation.h>
#include <..\OriginLab\XFWizManager.h>
#include <..\OriginLab\XFWizScript.h>
#include <..\OriginLab\XFWizDlg.h>

#include <..\OriginLab\XFWizard_utils.h>

#include <..\OriginLab\nlsf_utils.h>
#include <..\OriginLab\curve_utils.h>
#include <..\OriginLab\grobj_utils.h>
#include <ReportTree.h>

#include "Graph_utils.h"
#include "PFM_utils.h"	/// Iris 9/17/2008 PeakFitHelper need PLOT_TO_ enum

#include <..\OriginLab\XFGraph_Utils.h>

#include <..\OriginLab\PeakFitHelper.h>
#include <..\OriginLab\FitWizCoreBase.h>	
#include <..\Originlab\PAWizCore.h>
#endif
///---END FIX_REOPEN_PA_OPJ_FILE_TO_RECALCULATE
//******************************************************************************

/*------------------------------------------------------------------------------*
 * File Name: PAWiz.c															*
 * Creation: Sim 12-08-2008														*
 * Purpose: OriginC Source C file												*
 * Copyright (c) ABCD Corp.	2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010		*
 * All Rights Reserved															*
 * 																				*
 * Modification Log:															*
 *	Sim 12-09-2008 QA80-12741 OPERATION_UNPROTECT_IO_RANGE_IN_EXEC_TIME_OF_CHANGE_PARAM_FUNC
 *	Sim 01-13-2009 XFWIZ_CHANGE_PARAM_MAY_CHANGE_THEME_AND_IO_RANGE				*
 *	Sim 02-11-2009 FIX_WIZ_OPERATION_LABEL										*
 *	Folger 03/11/09 QA80-13265 LAST_REPORT_LT_STRING_FOR_ALL_PA_GOALS_EXCEPT_FITTING
 *	Folger 03/26/09 ANALYSIS_OUTPUT_REPORT_SHEET_NAME_SHOULD_BE_EXCLAMATION_CHAR_ENDING
 *	Folger 08/03/09 QA80-13998 PA_LAST_REPORT_LT_STRING_CLEANUP					*
 *	Folger 08/07/09 QA80-13998 SUPPORT_GET_GOAL_AND_FITTING_REPORT_GRAPH_UID_FROM_PAWIZMANAGER
 *	Folger 08/07/09 QA80-13998-P1 OUTPUT_RANGE_LT_STRING_FAILS_TO_UPDATE_WHEN_RUN_PA_WITHOUT_AUTOUPDATE
 *	Sim 12-30-2009 QA81-14898 SHOW_GUI_SETTINGS_FOR_XFWIZ_OPERATION				*
 *------------------------------------------------------------------------------*/
 
////////////////////////////////////////////////////////////////////////////////////
// Including the system header file Origin.h should be sufficient for most Origin
// applications and is recommended. Origin.h includes many of the most common system
// header files and is automatically pre-compiled when Origin runs the first time.
// Programs including Origin.h subsequently compile much more quickly as long as
// the size and number of other included header files is minimized. All NAG header
// files are now included in Origin.h and no longer need be separately included.
//
// Right-click on the line below and select 'Open "Origin.h"' to open the Origin.h
// system header file.
#include <Origin.h>
////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////
// Include your own header files here.
#include <..\OriginLab\WizOperation.h>
#include <..\OriginLab\XFWizScript.h>
#include <..\OriginLab\XFWizDlg.h>

#include <..\OriginLab\XFWizard_utils.h> ///---Sim 01-13-2009 XFWIZ_CHANGE_PARAM_MAY_CHANGE_THEME_AND_IO_RANGE

#include <..\Originlab\PAWizCore.h>
#include "PAWiz.h"

////////////////////////////////////////////////////////////////////////////////////
// Start your functions here.
///---Sim 01-13-2009 XFWIZ_CHANGE_PARAM_MAY_CHANGE_THEME_AND_IO_RANGE
/*
int run_pa_wiz(UINT msg, TreeNode &tnWizTheme, TreeNode &tnWizIOs, DWORD dwOPUID, int nExeMode)
{
	switch ( msg )
	{
	case WIZOPH_EXECUTE:
		run_pa_nodlg(tnWizTheme, tnWizIOs, dwOPUID);
		return 0;
		
	case WIZOPH_DO_CHANGE_PARAMS_WIZDLG:
		run_pa_dlg(tnWizTheme, tnWizIOs, dwOPUID);
		return 0;
		
	}
	
	return -1;		// failed	
}


int run_pa_nodlg(TreeNode &tnWizTheme, TreeNode &tnWizIOs, DWORD dwOPUID) // = NULL, NULL, 0
{
	///---Sim 12-04-2008 IMPROVE_XF_WIZ_FRAMEWORK
	//PAWizScript paScript;
	//if ( !paScript.Run(tnWizTheme, tnWizIOs, dwOPUID) )
		//return -1;
	//return 0;
	PAWizManager paWizManager(tnWizTheme, tnWizIOs, dwOPUID);
	XFWizScript xfwizSpt(paWizManager);
	if ( !xfwizSpt.Run() )
		return -1;
	return 0;
	///---END IMPROVE_XF_WIZ_FRAMEWORK
}

int run_pa_dlg(TreeNode &tnWizTheme, TreeNode &tnWizIOs, DWORD dwOPUID) // = NULL, NULL, 0
{
	if ( tnWizTheme )
	{
		string strThemeName;
		tree_get_theme_file_name(tnWizTheme, strThemeName);
		if ( strThemeName.IsEmpty() ) // dialog mode default use <default> theme if existing
		{
			strThemeName = STR_DEFAULT;
			tree_set_theme_file_name(tnWizTheme, strThemeName);
		}
	}
	
	///---Sim 12-04-2008 IMPROVE_XF_WIZ_FRAMEWORK
	//PAWizDlg *paDlg = new PAWizDlg;
	//if ( NULL == paDlg )
		//return -1;
	//
	//if ( paDlg->Create(GetWindow(), tnWizTheme, tnWizIOs, dwOPUID) )
	//{
		//paDlg->Show(true);
	//}
	//
	//return 0;
	PAWizManager *pPAWizManager = new PAWizManager(tnWizTheme, tnWizIOs, dwOPUID);
	if ( NULL == pPAWizManager )
		return -1;
	
	XFWizDlg *pXFWizDlg = new XFWizDlg(pPAWizManager); // modeless dialog hold this pointer
	if ( NULL == pXFWizDlg )
		return -1;
	
	///---Sim 12-09-2008 QA80-12741 OPERATION_UNPROTECT_IO_RANGE_IN_EXEC_TIME_OF_CHANGE_PARAM_FUNC
	//if ( pXFWizDlg->Create(GetWindow()) )
	//{
		//pXFWizDlg->Show(true);
	//}
	HWND hWndParent = GetWindow();
	if ( 0 == dwOPUID )
	{
		if ( pXFWizDlg->Create(hWndParent) )
		{
			///---Sim 12-12-2008 QA80-12764-P3 PA_MODAL_DIALOG_ALSO_FORCE_SET_SHOW_FLAG_AS_ON_PAGE_CHANGE_EVENT_NEED_IT
			// Dialog OnReady will always show dialog
			//pXFWizDlg->Show(true);
			///---END QA80-12764-P3 PA_MODAL_DIALOG_ALSO_FORCE_SET_SHOW_FLAG_AS_ON_PAGE_CHANGE_EVENT_NEED_IT
		}
		// Dialog OnDestroy event will release pXFWizDlg;
	}
	else // change param must open model dialog, QA80-12741
	{
		pXFWizDlg->DoModal(hWndParent);
		delete pXFWizDlg; ///---Sim 12-09-2008 QA80-12741 OPERATION_UNPROTECT_IO_RANGE_IN_EXEC_TIME_OF_CHANGE_PARAM_FUNC
	}
	
	///---END QA80-12741 OPERATION_UNPROTECT_IO_RANGE_IN_EXEC_TIME_OF_CHANGE_PARAM_FUNC
	
	return 0;
	///---END IMPROVE_XF_WIZ_FRAMEWORK
}
*/
//int run_pa_wiz(UINT msg, const XFWizTheme *pXFWizTheme, const XFWizInputOutputRange *pXFWizIO, DWORD dwOPUID, int nExeMode)
int PA_OP_FUNC_NAME(UINT msg, const XFWizTheme *pXFWizTheme, const XFWizInputOutputRange *pXFWizIO, DWORD dwOPUID, int nExeMode)
{
	TEMPLATE_run_wiz(PAWizManager, msg, pXFWizTheme, pXFWizIO, dwOPUID, nExeMode)
}

int run_pa_nodlg(LPCSTR lpcszThemeName, const XFWizTheme *pXFWizTheme, const XFWizInputOutputRange *pXFWizIO, DWORD dwOPUID) // = NULL, NULL, NULL, 0
{
	TEMPLATE_run_wiz_nodlg(PAWizManager, lpcszThemeName, pXFWizTheme, pXFWizIO, dwOPUID)
}
int open_pa_dlg(LPCSTR lpcszThemeName, const XFWizTheme *pXFWizTheme, const XFWizInputOutputRange *pXFWizIO, DWORD dwOPUID) // = NULL, NULL, NULL, 0
{
	TEMPLATE_open_wiz_dlg(PAWizManager, lpcszThemeName, pXFWizTheme, pXFWizIO, dwOPUID)
}
///---END XFWIZ_CHANGE_PARAM_MAY_CHANGE_THEME_AND_IO_RANGE

///---Sim 02-11-2009 FIX_WIZ_OPERATION_LABEL
class OC_REGISTERED PA_OP_CLASS_NAME	:	public	WizOperation
{
public:
	///------ Folger 08/07/09 QA80-13998 SUPPORT_GET_GOAL_AND_FITTING_REPORT_GRAPH_UID_FROM_PAWIZMANAGER
	/// virtual
	void	DestroyInternal(BOOL bDeleteOutputs = FALSE);
	///------ End SUPPORT_GET_GOAL_AND_FITTING_REPORT_GRAPH_UID_FROM_PAWIZMANAGER

protected:
	///---Sim 12-30-2009 QA81-14898 SHOW_GUI_SETTINGS_FOR_XFWIZ_OPERATION
	// hard to completely finish, so go to 8.2 
	//virtual void	ShowDialogSettings();
	///---END QA81-14898 SHOW_GUI_SETTINGS_FOR_XFWIZ_OPERATION
	
	///------ Folger 03/11/09 QA80-13265 LAST_REPORT_LT_STRING_FOR_ALL_PA_GOALS_EXCEPT_FITTING
private:
	///------ Folger 08/07/09 QA80-13998-P1 OUTPUT_RANGE_LT_STRING_FAILS_TO_UPDATE_WHEN_RUN_PA_WITHOUT_AUTOUPDATE
	///// virtual
	//void updateOutputsGlobalRangeStrings(Array<DataRange&>& arrOutputs, LPCSTR lpcszXFName);
	/////------ End LAST_REPORT_LT_STRING_FOR_ALL_PA_GOALS_EXCEPT_FITTING
	///------ End OUTPUT_RANGE_LT_STRING_FAILS_TO_UPDATE_WHEN_RUN_PA_WITHOUT_AUTOUPDATE
};
///---Sim 02-11-2009 FIX_WIZ_OPERATION_LABEL

///------ Folger 08/07/09 QA80-13998-P1 OUTPUT_RANGE_LT_STRING_FAILS_TO_UPDATE_WHEN_RUN_PA_WITHOUT_AUTOUPDATE
/////------ Folger 03/11/09 QA80-13265 LAST_REPORT_LT_STRING_FOR_ALL_PA_GOALS_EXCEPT_FITTING
///// virtual
//void	PA_OP_CLASS_NAME::updateOutputsGlobalRangeStrings(Array<DataRange&>& arrOutputs, LPCSTR lpcszXFName)
//{
	///// !!! three vectors must be kept same size !!!
	//vector<string>	vsXFNames = {
		//STR_PA_XFNAME_INT,
		//STR_PA_XFNAME_BASECREATE,
		//STR_PA_XFNAME_BASESUBTR,
		//STR_PA_XFNAME_PEAKS
	//};
	//
	//vector<int>		vnOffsets = {
		//REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN,
		//INDEX_OF_NON_REPORT_TREE_VARS_IN_OUTPUT_ARRAY,
		//INDEX_OF_NON_REPORT_TREE_VARS_IN_OUTPUT_ARRAY,
		//REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN
	//};
	//
	/////------ Folger 08/03/09 QA80-13998 PA_LAST_REPORT_LT_STRING_CLEANUP
	////char	cSeparator = '|';
	////vector<string>	vsVariableNames = {
		////"__PAINTEGPEAK|__PAINTEGCURVE",
		////"__PABASELINE",
		////"__PASUBTRACTED|__PABASELINE",
		////"__PABASELINE|__PAPEAKCENTER"
	////};
	//#define	STR_SEPARATOR	"|"
	//LPCSTR	lpcszSeparator = STR_SEPARATOR;
	//char	cSeparator = lpcszSeparator[0];
	//vector<string>	vsVariableNames = {
		//LTVAR_PA_INTEGRATE_PEAK STR_SEPARATOR LTVAR_PA_INTEGRATE_CURVE,
		//LTVAR_PA_BASELINE,
		//LTVAR_PA_SUBTRACTED_DATA STR_SEPARATOR LTVAR_PA_BASELINE,
		//LTVAR_PA_BASELINE STR_SEPARATOR LTVAR_PA_PEAK_CENTER
	//};
	/////------ End PA_LAST_REPORT_LT_STRING_CLEANUP
	///// !!! three vectors must be kept same size !!!
	//
	//int		nGoal = vsXFNames.Find(lpcszXFName);
	//if ( nGoal < 0  )
		//return;
	//
	//Datasheet	ds;
	//int			c1, c2;
	//if ( REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN == vnOffsets[nGoal] )		/// report data
	//{
		//for( int ii = REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN; ii < arrOutputs.GetSize(); ++ii )
		//{
			//if ( ii - REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN >= vsVariableNames[nGoal].GetNumTokens(cSeparator) )
				//return;
			//
			//DataRange& dr = arrOutputs.GetAt(ii);
			//if ( !dr || dr.GetNumData() <= 0 )
				//continue;
			//
			//dr.GetRange(ds, c1, c2);
			/////------ Folger 03/26/09 ANALYSIS_OUTPUT_REPORT_SHEET_NAME_SHOULD_BE_EXCLAMATION_CHAR_ENDING
			////LT_set_str(vsVariableNames[nGoal].GetToken(ii - REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN, cSeparator), ds.m_strBookSheet);
			//LT_set_str(vsVariableNames[nGoal].GetToken(ii - REPORT_TREE_VARS_IN_OUTPUT_ARRAY_BEGIN, cSeparator), convert_str_with_exclamation_char_endding(ds.m_strBookSheet));
			/////------ End ANALYSIS_OUTPUT_REPORT_SHEET_NAME_SHOULD_BE_EXCLAMATION_CHAR_ENDING
		//}
	//}
	//else
	//{
		//DataRange	dr;
		//int			ii = 0;
		//do
		//{
			//dr = arrOutputs.GetAt(ii++);
		//} while ( !dr || dr.GetNumData() <= 0 );
		//
		//DWORD		dwRules = DRR_NO_FACTORS | DRR_NO_WEIGHTS | DRR_GET_DEPENDENT;
		//for ( ii=0; ii<dr.GetNumData(dwRules); ++ii )
		//{
			//if ( ii >= vsVariableNames[nGoal].GetNumTokens(cSeparator) )
				//return;
			//
			//DataRange	drSub;
			//dr.GetSubRange(drSub, dwRules, ii);
			//drSub.GetRange(ds, c1, c2);
			/////------ Folger 03/26/09 ANALYSIS_OUTPUT_REPORT_SHEET_NAME_SHOULD_BE_EXCLAMATION_CHAR_ENDING
			////LT_set_str(vsVariableNames[nGoal].GetToken(ii, cSeparator), ds.m_strBookSheet);
			//LT_set_str(vsVariableNames[nGoal].GetToken(ii, cSeparator), convert_str_with_exclamation_char_endding(ds.m_strBookSheet));
			/////------ End ANALYSIS_OUTPUT_REPORT_SHEET_NAME_SHOULD_BE_EXCLAMATION_CHAR_ENDING
		//}
	//}
//}
/////------ End LAST_REPORT_LT_STRING_FOR_ALL_PA_GOALS_EXCEPT_FITTING
///------ End OUTPUT_RANGE_LT_STRING_FAILS_TO_UPDATE_WHEN_RUN_PA_WITHOUT_AUTOUPDATE

///------ Folger 08/07/09 QA80-13998 SUPPORT_GET_GOAL_AND_FITTING_REPORT_GRAPH_UID_FROM_PAWIZMANAGER
/// virtual
void	PA_OP_CLASS_NAME::DestroyInternal(BOOL bDeleteOutputs/* = FALSE*/)
{
	if ( bDeleteOutputs )
	{
		TreeNode		trThemes = GetWizThemeTree();
		PAWizManager	paWizMngr;
		PAWizTheme*		ppaWizTheme = (PAWizTheme*)paWizMngr.GetWizTheme();
		int				nUID = ppaWizTheme->GetFittingReportGraphUID(NULL, trThemes);
		if ( nUID > 0 )
		{
			GraphLayer		gl;
			gl = (GraphLayer)Project.GetObject(nUID);
			if ( gl )
			{
				gl.GetPage().Destroy();
			}
		}
	}
	
	WizOperation::DestroyInternal(bDeleteOutputs);
}
///------ End SUPPORT_GET_GOAL_AND_FITTING_REPORT_GRAPH_UID_FROM_PAWIZMANAGER

///---Sim 12-30-2009 QA81-14898 SHOW_GUI_SETTINGS_FOR_XFWIZ_OPERATION
// hard to completely finish, so go to 8.2 
/*
/// virtual
void PA_OP_CLASS_NAME::ShowDialogSettings()
{
	//out_str("pa gui settings"); // only for testing
	ShowWizGUI();
}
*/
///---END QA81-14898 SHOW_GUI_SETTINGS_FOR_XFWIZ_OPERATION
