#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	location_data,
	location_system,
	location_user,
	location_page,
};
//put additional include files here
#include <..\OriginLab\Import_Utils.h>
#include <..\OriginLab\Fu_Utils.h>
//#include <..\OriginLab\iwbase.h>
///Kyle 10/29/08 MOVE_CODE_FOR_DRAG_AND_DROP_TO_FILE_DRAG_N_DROP
//#include <..\OriginLab\iw_filter.h>
#include <..\OriginLab\IWBase.h>
#include <..\OriginLab\AscImpOptions.h>
#include <..\OriginLab\dragNdrop.h>
///End MOVE_CODE_FOR_DRAG_AND_DROP_TO_FILE_DRAG_N_DROP
//#include <..\OriginLab\BinImp.h>
//#include <..\OriginLab\FileImport.h>

//put your own support static functions here

//////////////////////////////////////////////////////////////////////////////////////////
// Modification Log :
// Folger 08/12/08 IMPROVE_ERROR_HANDLING_FOR_FAILING_TO_LOAD_FILTER
// Kyle 10/29/08 MOVE_CODE_FOR_DRAG_AND_DROP_TO_FILE_DRAG_N_DROP
// Sim 05-12-2009 QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
//////////////////////////////////////////////////////////////////////////////////////////


void impFile(const string& fname, const string& filtername, const int& location, Range& orng)
{
	/*
	//	int nErrorCode = import_file(dst, fname, filter);
	/// Hong 9/26/06
	if( fname == NULL || !fname.IsFile())
		XF_THROW(CER_INVALID_FILEPATH);
	
	string strClassName;
	Tree trFilter(filter);
	GetFilterClassName(trFilter, strClassName);
	IWBase& iwFilter = (IWBase&) Project.FindClass(strClassName);
	Tree trInfo;
	/// Hong 8/08/06 NEED_GET_TARGET_LAYER_BY_FILTER
	Page pgTarget;
	dst.GetParent(pgTarget);
	dst = GetTargetLayer(pgTarget, 0, 0, trFilter, true);
	/// end NEED_GET_TARGET_LAYER_BY_FILTER
	/// Hong 9/12/06 SUPPORT_MUTIIL_FILES
	iwFilter.InitFileArray();
	iwFilter.AddFile(fname, true );
	/// end SUPPORT_MUTIIL_FILES
	int iErr = iwFilter.Execute(dst, 0, fname, trInfo, trFilter);
	//	if( IMPERR_NONE != nErrorCode )
	//		XF_THROW_EX("import_file failed with error code %d", nErrorCode);
	*/
	Datasheet dsTarget;
	int c1,c2;
	/// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	//orng.GetRange(dsTarget, c1, c2);
	Page pgTarget;
	if ( !GetRangeInfo(orng, dsTarget, pgTarget, c1, c2) )
		XF_THROW(XFERR_INVALID_RANGE);
	/// end CENTRALIZE_CHECK_CALID_RANGE
	if(!dsTarget)
		XF_THROW(XFERR_INVALID_MATOBJ);// a better global err for not matching book would be better
	//Page pgTarget  = dsTarget.GetPage(); /// Hong 03/28/07 v8.0591 CENTRALIZE_CHECK_CALID_RANGE
	int nIndexLayer = dsTarget.GetIndex(); 
	StringArray stDataFileName;
	separate_multiline_files(fname, stDataFileName);
	/// Hong 9/28/06 ADD_ERROR_MESSAGE_IF_FILE_INVALID
	if(stDataFileName.GetSize() <= 0)
		XF_THROW(CER_INVALID_IMPORT_DATA_FILE);
	/// Hong 9/28/06 ADD_ERROR_MESSAGE_IF_FILE_INVALID
	
	string strPage;
	pgTarget.GetName(strPage);
	
	string strFilterName(filtername);
	if(!strFilterName.IsEmpty() && strFilterName.Find(".oif") < 0)
		strFilterName = strFilterName + ".oif";	
	bool bLoadFilter = false;
	string strFileName = GetFileName(stDataFileName[0]);
	
	Tree trFilter;
	///---Sim 05-12-2009 QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
	// I improved LoadFilter() function, let it be able to handle the case of filter in page
	// So I think we can remove follow separated codes. Why need handle this case on here? I can't see reason
	///---END QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
	//In target Page, seem no need filternane to be valid, maybe can disable it in this mode
	/// Hong 06/14/07 v8.0641 USER_KEYWORD_FOR_COMBO_STRING
	//if (location == 3)
	if ( location_page == location )
	/// end USER_KEYWORD_FOR_COMBO_STRING
	{			
		/*
		fuLoadFilterFromPage(trFilter, pgTarget, FILTER_TYPE_ASCII);
		fuLoadFilterFromPage(trFilter, pgTarget, FILTER_TYPE_BINARY);
		fuLoadFilterFromPage(trFilter, pgTarget, FILTER_TYPE_USERDEFINED);
		fuLoadFilterFromPage(trFilter, pgTarget, FILTER_TYPE_XFUNC);
		*/
		///---Sim 05-12-2009 QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
		//for(int ii=0; ii < 3; ii++)
			//if( fuLoadFilterFromPage(trFilter, pgTarget, ii) && IsApplicable(trFilter, strFileName) )
				//break;
		for(int ii=FILTER_TYPE_ASCII; ii < FILTER_TYPE_XFUNC; ii++)
			if( fuLoadFilterFromPage(trFilter, pgTarget, ii) && is_applicable_ex(trFilter, strFileName) )
				break;
		///---END QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
		bLoadFilter = true;
	}
	
	/*
	if ( !filtername.IsEmpty() )
	{
		
		string strFilePath, strFiler;
		switch (location)
		{
		//.....	get trFilter
		case 0:		// Data file folder
			for(int ii=0; ii < stDataFileName.GetSize(); ii++)
			{
				strFilePath = GetFilePath(stDataFileName[ii]);
				strFilterName = strFilePath + strFilterName;
				if(filtername.IsFile())
				{
					bLoadFilter = true;
					break;
				}
			}
			break;
		case 1:		// Origin working filter folder
			strFilePath.Format("%s%s", GetAppPath(TRUE), FILTERS_FOLDER_NAME);
			strFilterName = strFilePath + strFilterName;
			if(strFilterName.IsFile())
				bLoadFilter = true;
			break;
		case 2:		// User filter folder
			strFilePath.Format("%s%s", GetAppPath(), FILTERS_FOLDER_NAME);
			strFilterName = strFilePath + strFilterName;
			if(strFilterName.IsFile())
				bLoadFilter = true;
			break;
		case 3:		// In target Page
			break;
		default:
			break;
		}
		if(!bLoadFilter)
		{
			XF_THROW(XFERR_WT_FAILED_TO_CREATE_FILTER);
		}
		else
			fuLoad(trFilter, filtername);	
	}
	*/
	//if ( !strFilterName.IsEmpty() )
	if ( !strFilterName.IsEmpty() && !bLoadFilter)//no need call LoadFilter in case in Target Page
	{
		///---Sim 05-12-2009 QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
		//if (!LoadFilter(strFilterName, stDataFileName[0], strPage, location, trFilter) )
		if (!load_import_filter_ex(strFilterName, stDataFileName[0], strPage, location, trFilter) )
		///---END QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
		{
			//------ Folger 08/12/08 IMPROVE_ERROR_HANDLING_FOR_FAILING_TO_LOAD_FILTER
			//XF_THROW(XFERR_WT_FAILED_TO_CREATE_FILTER);
			XF_THROW(XFERR_FAIL_TO_LOAD_FILTER);
			//------
		}
	}
		
	///---Sim 05-12-2009 QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
	//if ( !ImportFiles(stDataFileName, strPage, nIndexLayer, trFilter) )
	if ( !import_files_ex(stDataFileName, strPage, nIndexLayer, trFilter) )
	///---END QA80-13594 MAKE_USER_IMPORT_OC_CODE_COMPATIBLE
	{
		XF_THROW(IMPERR_IMPORT_FUNC_ERR);
	}	
}

//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 impFile_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return impFile_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if ( 0 == lstrcmp(lpcszNodeName, "fname") )
	{
		/// Hong 11/1/06 ERROR_MESSAGE
		int nErrCode = CER_NO_ERROR;
		int nInvalidFileIdx = 0;
		string strFileName = trGetN.fname.strVal;
		if( strFileName.IsEmpty ())
		{
			nErrCode = CER_EMPTY_FILENAME;
		}
		else if( !is_valid_filename(strFileName, nInvalidFileIdx) )
		{
			nErrCode = CER_INVALID_FILENAME_INDEX;
		}
		if(CER_NO_ERROR != nErrCode)
		{
			strErrMsg = nErrCode;
			
			if ( CER_INVALID_FILENAME_INDEX == nErrCode )
			{
				strErrMsg += ":";
				strErrMsg += nInvalidFileIdx;
			}
	
			bOKEnable = false;
			return true;
		}
		/// end ERROR_MESSAGE
	}
	/// Hong 03/19/07 SUPPORT_BROWSE_FOR_FILTER_NAME
	if ( 0 == lstrcmp(lpcszNodeName, "filtername") )
	{
		string strFilterName = trGetN.filtername.strVal;
		if(!strFilterName.IsEmpty() && strFilterName.Find(".oif") < 0)
			strFilterName += ".oif";	
		/// Hong 03/21/07 MORE_CHECK_FOR_FULL_PATH	
		//if(strFilterName.IsFile())
		string strPath = GetFilePath(strFilterName);
		if( strPath.IsPath() && strFilterName.IsFile() )
		/// end MORE_CHECK_FOR_FULL_PATH
			trGetN.location.Enable = false;
		else
			trGetN.location.Enable = true;
	}
	/// end SUPPORT_BROWSE_FOR_FILTER_NAME
	return false;

}

int impFile_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 impFile_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if(nGetNDialog > 0)
	{
		trGetN.finfo.Show = 0;
		trGetN.trFilter.Show = 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 impFile_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
