#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here
#include <..\Originlab\oEDF.h> 
#include <..\originlab\file_utils.h>
#include <..\originlab\fu_utils.h>
//put your own support static functions here

static bool _file_name_event(TreeNode& trGetN, string& strErrMsg)
{
	if ( !trGetN )
		return false;
	
	string strFileNames = trGetN.fname.strVal;
	TreeNode trFiles = trGetN.trfiles;
	trFiles.RemoveChildrenWithPrefix(); //reset the branch
	
	if ( !check_imported_file_name(strFileNames, strErrMsg) )
		return false;
	
	StringArray saFileNames;
	separate_multiline_files(strFileNames, saFileNames);
	if ( !_construct_files_info_tree(trFiles, saFileNames, strErrMsg) )
	{
		trFiles.RemoveChildrenWithPrefix();
		strErrMsg = CER_INVALID_IMPORT_DATA_FILE;
		return false;
	}
	return true;
}

static bool _construct_files_info_tree(TreeNode& trFiles, const vector<string>& vsFileNames, string& strErrMsg)
{
	if ( !trFiles )
		return false;
	int nFiles = vsFileNames.GetSize();
	for ( int iFile = 0; iFile < nFiles; iFile++ )
	{
		string strFile = GetFileName(vsFileNames[iFile], true);
		string strTagName;
		strTagName.Format("file%d", iFile + 1);
		TreeNode trFile = trFiles.AddNode(strTagName);
		trFile.SetAttribute(STR_LABEL_ATTRIB, strFile);
		

		TreeNode trFileInfo = trFile;
		EDF edf;
		if ( (edf.Open(vsFileNames[iFile]) != EDF_SUCCESS) || (edf.GetFileInfo(trFileInfo, TRUE) != EDF_SUCCESS) )
		{
			strErrMsg = IMPERR_FAILED_OPEN;
			return false;
		}
	}
	return true;
}



void impEDF(const string& fname, const TreeNode& trfiles, const TreeNode& options, Range& orng)
{
	///Sophy 6/21/2010 ORG-152-S1 MORE_IMPORT_OPTIONS_FOR_IMPEDF
	//Worksheet wks;
	//WorksheetPage	wp;
	//int nC1, nC2;
	//if ( !GetRangeInfo(orng, wks, wp, nC1, nC2) )
		//XF_THROW(XFERR_INVALID_RANGE);
	//if ( !wks )
		//XF_THROW(CER_INVALID_WKS);
	///end MORE_IMPORT_OPTIONS_FOR_IMPEDF
	StringArray saFileNames;
	separate_multiline_files(fname, saFileNames);
	int nRet, iFile, nFiles = saFileNames.GetSize();
	Tree trBaseInfo;
	///Sophy 6/21/2010 ORG-152-S1 MORE_IMPORT_OPTIONS_FOR_IMPEDF
	int nC1, nC2;
	Datasheet ds;
	orng.GetRange(ds, nC1, nC2);
	Layer lyTarget = ds;
	if ( !lyTarget.IsValid() )
		XF_THROW(IMPERR_INVALID_DATASHEET);
	TreeNode trUsingFilter;
	double dDefault = 0;
	if ( fuGetDouble(options, IDE_DISP_IMP_ORIGINVER, dDefault) )
		trUsingFilter = options;
	int nImportMode = fuGetImportMode(trUsingFilter);
	
	Page pgTarget;
	lyTarget.GetParent(pgTarget);
	int nType = pgTarget.GetType();
	if ( nType != EXIST_WKS )
	{
		fuSetTargetPage(trUsingFilter, EXIST_WKS);
	}
	
	///Sophy 6/28/2010 ORG-152-S9 SHOW_SPARKLINE_ON_IMPEDF_SIGNALS
	int	nSparkLines = fuGetSparkLine(trUsingFilter);
	///end SHOW_SPARKLINE_ON_IMPEDF_SIGNALS
	
	Worksheet wks(lyTarget);
	int nIndexLayer = lyTarget.GetIndex();
	///end MORE_IMPORT_OPTIONS_FOR_IMPEDF
	for ( iFile = 0; iFile < nFiles; iFile++ )
	{
		EDF edf;
		if ( (nRet = edf.Open(saFileNames[iFile])) != EDF_SUCCESS )
		{
			XF_THROW_EX(nRet, GetFileName(saFileNames[iFile], false));
		}
		
		wks = GetTargetLayer(pgTarget, nIndexLayer, iFile, trUsingFilter, true, orng, nImportMode);
		
		string strTagName;
		strTagName.Format("file%d", iFile + 1);
		TreeNode trFile = trfiles.GetNode(strTagName);
		//TreeNode trFileInfo = trFile.GetNode("FileInfo");
		TreeNode trFileInfo = trFile;
		nRet = edf.Import(wks, trFileInfo);
		if ( EDF_USERABORT == nRet )
		{
			XF_THROW("User Abort");
		}
		if ( nRet != EDF_SUCCESS )
		{
			XF_THROW_EX(nRet, GetFileName(saFileNames[iFile], false));
		}
		
		///Sophy 6/28/2010 ORG-152-S9 SHOW_SPARKLINE_ON_IMPEDF_SIGNALS
		bool bShowSparkLine = update_sparkline(nSparkLines, wks, 0, -1);
		///end SHOW_SPARKLINE_ON_IMPEDF_SIGNALS
		DataRange drImported;
		drImported.Add(wks, 0, NULL, -1);
		Tree trDataRange;
		getRangeInfo(trDataRange, drImported);
		setPageAndLayerName(drImported, saFileNames[iFile], trUsingFilter, COVER_LAST_ADDED_COMMENT);
		
		TreeNode trInfo = trBaseInfo.AddNode("File" + (iFile + 1));
		impinfo_AddFile(wks, saFileNames[iFile], trInfo, ASCIMP_MODE_NEW_BOOKS, FILTER_TYPE_XFUNC, trUsingFilter, iFile + 1, trDataRange);
	}
}

//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 impEDF_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impEDF_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	//return false;
	if ( GETNE_ON_INIT == nEventID || GETNE_ON_THEME == nEventID || 0 == lstrcmp(lpcszNodeName, "fname") )
	{
		bOKEnable = _file_name_event(trGetN, strErrMsg);
		return true;
	}

}

int impEDF_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	///Sophy 6/21/2010 ORG-152-S1 MORE_IMPORT_OPTIONS_FOR_IMPEDF
	string strTagName(lpcszVarName);
	if ( strTagName == "options" )
	{
		///Sophy 6/28/2010 ORG-152-S9 SHOW_SPARKLINE_ON_IMPEDF_SIGNALS
		//constructOptionsForXF(tr, true, true, false, false, false, false);
		constructOptionsForXF(tr, true, true, true, false, false, false);
		///end SHOW_SPARKLINE_ON_IMPEDF_SIGNALS
		
		TreeNode tr1stImportMode = tree_get_node_by_id(tr, IDE_DISPIMP_1STMODE, true);
		if ( tr1stImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_REPLACE_BOOK_SHEET);
			tr1stImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			tr1stImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			tr1stImportMode.nVal = ASCIMP_MODE_REPLACE_DATA;
		}
		
		TreeNode trImportMode = tree_get_node_by_id(tr, IDE_IMP_IMPORTMODE, true);
		if ( trImportMode )
		{
			string strCombo, strMapping;
			strCombo = getImportModeListString(strMapping, LIST_IMPORT_MODE_BOOK_SHEET);
			trImportMode.SetAttribute(STR_COMBO_ATTRIB, strCombo);
			trImportMode.SetAttribute(STR_INTMAP_ATTRIB, strMapping);
			trImportMode.nVal = ASCIMP_MODE_NEW_BOOKS;	
		}
		fuSetTargetPage(tr, EXIST_WKS);
		fuSetXFunctionName(tr, "impEDF");
	}
	///end MORE_IMPORT_OPTIONS_FOR_IMPEDF
	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 impEDF_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	if ( nGetNDialog > -1 )
	{
		string strFileName = trGetN.fname.strVal;
		if ( LTXF_FROM_GUI_MENU & dwCntrl )
			strFileName = "";
		if ( is_empty_or_unassigned(strFileName) )
		{
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				nFilesRet = get_open_box_by_file_group(strFileName, "EDF", NULL, OPENBOX_FLAG_MULTIFILES, NULL, NULL, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				nFilesRet = get_open_box_by_file_group(strFileName, "EDF", NULL, true);
			}
			if ( nFilesRet <= 0 ) //not selected
			{
				nRet = XFEVT_ABORT;
				return;
			}
		}
		trGetN.fname.strVal = strFileName;
		string strErrMsg;
		if ( !_file_name_event(trGetN, strErrMsg) )
		{
			int nErr = atoi(strErrMsg);
			nRet = XFEVT_ABORT;
			warning_msg_box(nErr, false, 'E');
			return;
		}
	}
}
//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 impEDF_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
