#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	dialog_direct,
	dialog_options,
};
//put additional include files here
#include <xfbase.h>
#include <..\originlab\fu_utils.h>
#include <..\originlab\AscImpOptions.h> // temp let reimport impasc works
///===================end of include headers=============================

///////////////////////////////////////////////////////////////////////
//////// modification log
/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
/// Hong 03/29/07 v8.0592 FIX_ERROR_DUMP_ERROR_MSG
/// Hong 04/13/07 v8.0600 CHECK_IF_SUPPORT_FORMAT
/// Hong 04/17/07 v8.0602b FIX_IMPASC_FAIL_REIMPORT_CHANGE_OPTION
/// Sim 04-17-2007 UPDATE_LIST_OF_REPEATED_IMPORT_FILE_TYPE
/// Sim 04-17-2007 FIX_PASS_RANGE_DIRECTLY_WILL_CAUSE_PROBLEM_UNEXPECTED
/// Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
/// Sim 05-18-2007 REIMPORT_MULTI_ASC_FILE_WILL_SHOW_ERROR
/// Sim 05-31-2007 IMPROVE_REIMPORT_CONTEXT_MENUE
///	CPY 8/4/2007 QA70-10121 SET_OPTION_STR_FROM_OC
/// Hong 11/30/07 QA80-10761 IW_SUPPORT_REIMPORT_CHANGE_OPTIONS
/// Hong 03/03/09 QA80-13209 MATRIX_ALLOW_REIMPORT
/// Hong 03/05/09 QA80-13222 SWEEP_MODE_ALLOW_SHOW_TIME_COL_AS_75_ASKED_BY_USER
/// Hong 03/12/09 IMPROVE_CODE_PUT_SOME_LOGIC_IN_IMPPCLAMP
/// Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
///////////////////////////////////////////////////////////////////////
//put your own support static functions here


void reimport(const int& dialog, Range& orng)
{
	// input check
	int c1,c2; 
	Datasheet lyTarget;
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//orng.GetRange(lyTarget, c1, c2);
	Page pgTarget;
	if ( !GetRangeInfo(orng, lyTarget, pgTarget, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	/// Hong 03/03/09 QA80-13209 MATRIX_ALLOW_REIMPORT
	/*
	Worksheet wksTarget(lyTarget);
	if(!wksTarget.IsValid())
		XF_THROW(CER_INVALID_WKS);
	*/
	/// end MATRIX_ALLOW_REIMPORT
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//Page pgTarget;
	//wksTarget.GetParent(pgTarget);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	/// Hong 03/12/07 QA80-9418-S1 ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
	/* move to fu_utils.h
	Tree trBinary;
	if( !tree_get_binary_storage(trBinary, pgTarget, IFI_BINARY_STORAGE_NAME) )
		XF_THROW(0); // have no binary storage, no repeat import can be done
	
	Tree trFile;
	int nIndex = wksTarget.GetIndex();
	foreach(TreeNode tn in trBinary.Children)
	{
		TreeNode trRange = tree_get_node_by_tagname(tn, "DataRange", true);
		if(!trRange.IsValid())
			continue;
		bool bInvalidRange = 0;
		if( tn.Info.CoveredByAnotherImport ) // IW have no such node before we do clean 
			bInvalidRange = ( tn.Info.CoveredByAnotherImport.strVal.CompareNoCase("Yes") == 0 ) ? 1 : 0;
		
		Worksheet wksTemp;
		int nStartCol, nEndCol;
		DataRange dr;
		/// Hong 03/12/07 SUPPORT_RELATIVE_DATA_RANGE
		//UINT uid = trRange.RangeUID.nVal;
		//dr = (DataRange)Project.GetObject(uid);
		dr = get_and_check_range(trRange, pgTarget.GetName());
		/// end SUPPORT_RELATIVE_DATA_RANGE
		if( !dr.IsValid() && bInvalidRange )
			continue; 
		dr.GetRange(wksTemp, nStartCol, nEndCol);
		
		if( nIndex == wksTemp.GetIndex() && nStartCol <= c1 && tn.FilterInfo) // need more test for c2 later
		{
			trFile = tn;
		}
	}
	*/
	Tree trFile;
	/// Hong 03/29/07 v8.0592 FIX_ERROR_DUMP_ERROR_MSG
	//if( -1 == getFileInfoByRange(trFile, orng) )
	/// Hong 05/30/07 OUTPUT_ERROR_CODE
	//if( getFileInfoByRange(trFile, orng) <= 0 )
	///// end FIX_ERROR_DUMP_ERROR_MSG
		//XF_THROW(XFERR_REIMP_SELECT_NO_IMPORT_FILE);
	int nErrCode = XFERR_REIMP_SELECT_NO_IMPORT_FILE;
	if ( getFileInfoByRange(trFile, orng, &nErrCode) <= 0 )
		XF_THROW(nErrCode);
	/// end OUTPUT_ERROR_CODE
	/// end ONLY_UPDATE_BINARY_INFO_WHEN_REIMPORT
	
	if( !trFile && !trFile.Info.FileType )
		XF_THROW(XFERR_REIMP_SELECT_NO_IMPORT_FILE);
	
	///---Sim 05-31-2007 IMPROVE_REIMPORT_CONTEXT_MENUE
	/*
	// need special deal for impasc
	bool bImpASC = false;
	string strXFName;
	if(0 == trFile.Info.FileType.strVal.Compare(STR_FILTER_TYPE_ASCII)) // if ascii type
	{
		/// Hong 04/13/07 v8.0600 CHECK_IF_SUPPORT_FORMAT
		// no support reimport imp wiz ascii changing options now
		/// Hong 04/17/07 v8.0602b FIX_IMPASC_FAIL_REIMPORT_CHANGE_OPTION
		//if (dialog)
		if (dialog && (!trFile.impASC || trFile.impASC.nVal == 0) )
		/// end FIX_IMPASC_FAIL_REIMPORT_CHANGE_OPTION
			XF_THROW(REIMP_NOT_SUPPORT_FORMAT);
		/// end CHECK_IF_SUPPORT_FORMAT
		
		strXFName = "impasc";	
		bImpASC = true;
	}
	else
		fuGetXFunctionName(trFile.FilterInfo, strXFName);
	
	/// Hong 04/13/07 v8.0600 CHECK_IF_SUPPORT_FORMAT
	vector<string> vsReimportXF = {"impasc", "impcsv", "imppclamp", "impSPC", "impWAV", "impNIDiaDem"}; ///---Sim 04-17-2007 UPDATE_LIST_OF_REPEATED_IMPORT_FILE_TYPE
	if ( -1 == vsReimportXF.Find(strXFName) )
		XF_THROW(REIMP_NOT_SUPPORT_FORMAT);
	/// end CHECK_IF_SUPPORT_FORMAT
	*/
	string strXFName;
	if ( !isReimportable(trFile, &strXFName) )
		XF_THROW(REIMP_NOT_SUPPORT_FORMAT);
	
	// need special deal for impasc
	bool bImpASC = false;
	if ( strXFName.CompareNoCase("impAsc") == 0 )
	{
		bImpASC = true;
		/// Hong 11/30/07 QA80-10761 IW_SUPPORT_REIMPORT_CHANGE_OPTIONS
		//if (dialog && (!trFile.impASC || trFile.impASC.nVal == 0) )
		//	XF_THROW(REIMP_NOT_SUPPORT_FORMAT);
		/// end IW_SUPPORT_REIMPORT_CHANGE_OPTIONS
	}
	///---END IMPROVE_REIMPORT_CONTEXT_MENUE
	
	XFBase xf(strXFName);
	if(!xf)
		XF_THROW(XFERR_FAIL_LOAD_XF);
	
	if(bImpASC)
	{
		if( !xf.SetArg("trFilter", trFile.FilterInfo) )
			XF_THROW(XFERR_FAIL_SET_ARG);
	}
	else
	{
		///---Sim 05-08-2007 ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
		TreeNode tr1stMode = tree_get_node_by_id(trFile.FilterInfo, IDE_DISPIMP_1STMODE, true);
		tr1stMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		tr1stMode.Enable = 2;
		
		TreeNode trMode = tree_get_node_by_id(trFile.FilterInfo, IDE_IMP_IMPORTMODE, true);
		//trMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		trMode.Enable = 2;
		///---END ADD_OPTIONS_FOR_IMPORT_MODE_OF_1ST_FILE
		TreeNode trTemplate = tree_get_node_by_id(trFile.FilterInfo, IDE_DISP_IMP_PAGETEMPLATE, true);
		trTemplate.strVal = "";
		trTemplate.Enable = 2;
		/// Hong 06/04/07 v8.0632 FIX_OPTIONAL_CAN_NOT_REMEMBERED
		// XF::SetArg fail to deal with <optional> TreeNode
		//if( !xf.SetArg("options", trFile.FilterInfo) )
		//	XF_THROW(XFERR_FAIL_SET_ARG);
		Tree trJunk; // for memory of tree
		TreeNode trGetN;
		xf.GetGUI(trJunk, trGetN);
	
		trGetN.options.Replace(trFile.FilterInfo, true, true); // need to check if there is a better way
		///---Sim 10-29-2008 v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
		update_getn_LT_variables(trGetN, strXFName, false); // trGetN.options.GetNLT.* to trGetN.*
		///---END v9.926b QA80-12484 IMPORT_EXCEL_FILTER_NEED_SPECIAL_HANDLING
		xf.SetGUI(trGetN);
		/// end FIX_OPTIONAL_CAN_NOT_REMEMBERED
	}	
	
	if( !xf.SetArg("fname", trFile.Info.FilePath.strVal) )
		XF_THROW(XFERR_FAIL_SET_ARG);
	
	TreeNode trRange = tree_get_node_by_tagname(trFile, "DataRange", true);
	DataRange dr;
	/// Hong 03/12/07 SUPPORT_RELATIVE_DATA_RANGE
	//UINT uid = trRange.RangeUID.nVal;
	//dr = (DataRange)Project.GetObject(uid);
	dr = get_and_check_range(trRange, pgTarget.GetName());
		/// end SUPPORT_RELATIVE_DATA_RANGE
	///---Sim 05-18-2007 REIMPORT_MULTI_ASC_FILE_WILL_SHOW_ERROR
	// not support reimport a file where the data range is not at the top left corner of the sheet
	if ( bImpASC )
	{
		int c1, c2, r1, r2; 
		Datasheet ds;
		dr.GetRange(0, r1, c1, r2, c2, ds);
		
		if ( c1 > 0 || r1 > 0 )
			XF_THROW(XFERR_REIMP_NOT_SUPPORT_MULTI_ASC_FILE);
	}
	///---END REIMPORT_MULTI_ASC_FILE_WILL_SHOW_ERROR
	///---Sim 04-17-2007 FIX_PASS_RANGE_DIRECTLY_WILL_CAUSE_PROBLEM_UNEXPECTED
	DataRange drClone;
	dr.Clone(drClone);
	if( !xf.SetArg("orng", drClone) )
		XF_THROW(XFERR_FAIL_SET_ARG);
	///---END FIX_PASS_RANGE_DIRECTLY_WILL_CAUSE_PROBLEM_UNEXPECTED
	
	//----- CPY 8/4/2007 QA70-10121 SET_OPTION_STR_FROM_OC
	xf.SetOptionString("orng", 'I', "0x08000");
	//-----
	
	// need to centralize this Data selection for all import XF, now only add support for imppclamp for demo
	//if(trFile.Plot.trSetting)
	if(trFile.Import.trSetting) //--Hong, andrew have changed this Node's tagName
	{
		/// Hong 05/15/07 MOVE_FILE_NODE_AS_SINGLE_FILE
		//Tree trSetting;
		//TreeNode trDataSelection = trSetting.AddNode("File");
		//trDataSelection.Replace(trFile.Plot.trSetting);
		TreeNode trSetting;
		trSetting = trFile.Import.trSetting.Clone();
		/// end MOVE_FILE_NODE_AS_SINGLE_FILE
		/// Hong 03/12/09 IMPROVE_CODE_PUT_SOME_LOGIC_IN_IMPPCLAMP
		/*
		/// Hong 05/15/07 INFO_NODE_IS_NEEDED_WHEN_REIMPORT
		/// Hong 06/28/07 v8.0652 FIX_REIMPOPRT_FROM_MENU_GET_DUPLICATE_PARTIAL_IMPORT_NODE
		//if ( trSetting.InfoFileNormal )
		if ( trSetting.InfoFileNormal && !trSetting.InfoFileNormal.IsEmpty() )			
		/// end FIX_REIMPOPRT_FROM_MENU_GET_DUPLICATE_PARTIAL_IMPORT_NODE
			trSetting.InfoFileNormal.Show = 1;
		/// end INFO_NODE_IS_NEEDED_WHEN_REIMPORT
		/// Hong 03/05/09 QA80-13222 SWEEP_MODE_ALLOW_SHOW_TIME_COL_AS_75_ASKED_BY_USER
		if ( trSetting.ShowTimeCol )
			trSetting.ShowTimeCol.Show = 1;
		/// end SWEEP_MODE_ALLOW_SHOW_TIME_COL_AS_75_ASKED_BY_USER
		*/
		/// end IMPROVE_CODE_PUT_SOME_LOGIC_IN_IMPPCLAMP
		Tree trJunk; // for memory of tree
		TreeNode trGetN;
		xf.GetGUI(trJunk, trGetN);
		/// Hong 06/11/07 v8.0638 ALLOW_LT_ACCESS_FOR_STIMULUS
		// can always replace as for reimport
		//if ( trGetN.trfiles && trGetN.trfiles.GetNodeCount() > 0 )
		//{
			//xf.SetArg("trfiles", trSetting);
		//}
		//else
		/// end ALLOW_LT_ACCESS_FOR_STIMULUS
		{
			trGetN.trfiles.Replace(trSetting, true, true); // need to check if there is a better way
			xf.SetGUI(trGetN);
		}
	}
	
	if( !xf.SetArg("reimp", 1) )
		XF_THROW(XFERR_FAIL_SET_ARG);
	
	DWORD dwCntrl = dialog? LTXF_SHOW_DIALOG : 0;	
	int nErr;
	/// Hong 03/19/07 QA80-9476 TURN_OFF_ERROR_MSG_BEFORE_BETTER_IDENTIFY
	//if(xf.ExecuteLabTalk(NULL, NULL, NULL, 0, &nErr, dwCntrl) != 0)
		//XF_THROW(XFERR_FAIL_EVAL);
	xf.ExecuteLabTalk(NULL, NULL, NULL, 0, &nErr, dwCntrl);
	/// end TURN_OFF_ERROR_MSG_BEFORE_BETTER_IDENTIFY
}

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

}

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