#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
//put your own support static functions here
///////////////////////////////////////////////////////
#ifdef _FOR_SMART_LOADING_ONLY
#include <event_utils.h> /// Hong 02/03/10 QA80-15016 FIX_X_DATA_TYPE_CHANGE_WHEN_RECALCUTE
#include <..\OriginLab\WksOperation.h>
#include <..\OriginLab\WizOperation.h>

#include <..\OriginLab\XFCore.h>
#include <..\OriginLab\XFWiz.h>
#include <..\OriginLab\XFWizNavigation.h>
//#include <..\OriginLab\XFWizDlgNavigation.h>
#include <..\OriginLab\XFWizManager.h>
#include <..\OriginLab\XFWizScript.h>
#include <..\OriginLab\XFWizDlg.h>
#include <..\OriginLab\FitWizCoreBase.h>

#include <..\OriginLab\Graph_utils.h>
#include <..\OriginLab\nlsf_utils.h>
#include <..\OriginLab\curve_utils.h>
#include <..\OriginLab\grobj_utils.h>
#include <..\OriginLab\XFGraph_Utils.h>
#endif

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

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

///---Sim 12-09-2008 FIX_PA_MENU_RECENT_USE_DEFAULT_THEME
//static bool _load_theme_tree(TreeNode &trTheme, LPCSTR lpcszThemeName)
//{
	//string strFileName = theme_get_settings_file(lpcszThemeName,STR_PA_CLASS_NAME);
	//return themes_tree_from_file(trTheme, strFileName);
//}
///---END FIX_PA_MENU_RECENT_USE_DEFAULT_THEME


void pa(const XYRange& iy, const string& theme, const int& smode)
{
	if(!iy.IsValid())
		XF_THROW(XFERR_NO_DATA_SELECT);
	
	///Get source wks and prepare input range
	//Worksheet wksSource;
	//int r1, c1, r2, c2;
	//if(!find_input_range_bounding_box(iy, r1, c1, r2, c2, wksSource, 0))
		//XF_THROW(CER_INVALID_WKS);
	
	
	///---Sim 01-13-2009 XFWIZ_CHANGE_PARAM_MAY_CHANGE_THEME_AND_IO_RANGE
	/*
	Tree treeIO;
	TreeNode trIO = treeIO.AddNode("Input");
	///Sandy 2008-9-9 ADD_UTIL_FUNC_TO_CONSTRUCT_IT_TREE_FOR_AVOID_FILE_AUTO_LOAD_TO_MUCH_IN_PA_OXF
	//XFWizInputOutputRange paRange;
	//Array<DataRange&> arrRange;
	//arrRange.Add(iy);
	//if(!paRange.Add(&arrRange, STR_PA_XFNAME_GOAL, true))
		//XF_THROW(XFERR_FAIL_TO_UPDATE_GETN_TREE);	
	//if(!paRange.GetTree(trIO))
		//XF_THROW(XFERR_FAIL_TO_UPDATE_GETN_TREE);
	if(!construct_tree_for_input_range_of_xf(trIO, STR_PA_XFNAME_GOAL,  iy))
	{
		XF_THROW(XFERR_FAIL_TO_UPDATE_GETN_TREE);	
	}
	//end
	
	///prepare theme tree
	///---Sim 12-09-2008 FIX_PA_MENU_RECENT_USE_DEFAULT_THEME
	//TreeNode trTheme;
	//Tree treeTheme;
	Tree trTheme;
	///---END FIX_PA_MENU_RECENT_USE_DEFAULT_THEME
	//if ( !tn.IsEmpty() )
	if ( !theme.IsEmpty() )////Sandy 2009-1-12 change variable "tn" to "theme" for continue same of SR4
	{
		///---Sim 12-09-2008 FIX_PA_MENU_RECENT_USE_DEFAULT_THEME
		//if ( _load_theme_tree(treeTheme, tn) )
			//trTheme = treeTheme;
		// ASSUME!!!, that name get from theme tree isn't full file name, just theme name
		// for construct XFWizManager
		//tree_set_theme_file_name(trTheme, tn);
		tree_set_theme_file_name(trTheme, theme);// //Sandy 2009-1-12 change variable "tn" to "theme" for continue same of SR4 
		///---END FIX_PA_MENU_RECENT_USE_DEFAULT_THEME
	}
	
	if ( smode )
	{
		run_pa_nodlg(trTheme, trIO);
	}
	else
	{
		run_pa_dlg(trTheme, trIO);
	}	
	*/
	
	XFWizInputOutputRange objXFWizIO;
	if(!xfwiz_construct_input_range(&objXFWizIO, STR_PA_XFNAME_GOAL,  iy))
	{
		XF_THROW(XFERR_FAIL_TO_UPDATE_GETN_TREE);	
	}	
	
	if ( smode )
	{
		run_pa_nodlg(theme, NULL, &objXFWizIO);
	}
	else
	{
		open_pa_dlg(theme, NULL, &objXFWizIO);
	}	
	///---END XFWIZ_CHANGE_PARAM_MAY_CHANGE_THEME_AND_IO_RANGE
	
	
}

//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_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_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

int pa_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_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	TreeNode trInputData= trGetN.iy;
	XYZRange dr;
	if(!okxf_resolve_tree_construct_range(&trInputData, &dr)||!dr.IsValid())
	{
		warning_msg_box(XFERR_NO_DATA_SELECT, true);
		nRet = XFEVT_ABORT;
	
	}
	else
		nRet = XFEVT_PROCEED_NO_DLG;
	
}
//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_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
