#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
// modofication log
// Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
/// Iris 07/13/2007 v8.0659 SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
/// Sim 07-13-2007 FIX_CHECK_PATH
/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
/// Hong 10/08/07 QA80-9301 v8.0718 FIX_FAIL_EXPORT_AS_TDMS
/// Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG

//put additional include files here
#include <GetNBox.h>
#include <..\originlab\exportTDM.h>
#include <okocUtils.h>		///Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG

#define STR_TDM_FILE_GROUP "TDMFiles" ///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS

///---Jake 06/27/07 STORE_USER_FILE_NAME
//#define STR_ONLY_FILE_NAME_ATTRIB  "OnlyFileName" 
#define STR_NODE_FILE 	"fname" 
///---end STORE_USER_FILE_NAME

//put your own support static functions here
static bool _check_add_file_ext(string& strFile)
{
	string strFilepath = strFile;
	string strFname, strFext, strExt;	
	/// Hong 09/24/07 QA80-10343 v8.0707 UPDATE_TO_NEW_DLL_SUPPORT_TDMS
	//if( !separate_file_name_ext(strFilepath, strFname, strFext) || 0 != strFext.CompareNoCase("tdm") )
	/// Hong 10/08/07 QA80-9301 v8.0718 FIX_FAIL_EXPORT_AS_TDMS
	//if( !separate_file_name_ext(strFilepath, strFname, strFext) || 0 != strFext.CompareNoCase("tdm") || 0 != strFext.CompareNoCase("tdms") )
	if( !separate_file_name_ext(strFilepath, strFname, strFext) || (0 != strFext.CompareNoCase("tdm") && 0 != strFext.CompareNoCase("tdms")) )
	/// end FIX_FAIL_EXPORT_AS_TDMS
	/// end UPDATE_TO_NEW_DLL_SUPPORT_TDMS
	{
		///---Sim 05-07-2009 QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
#ifndef CHECK_EXCEED_FILE_PATH_LENGTH_LIMITATION
		strFile = strFname + ".tdm";
#else
		strFile = strFname;;
		add_file_extension(strFile, "tdm");
		check_trim_file_path_if_too_long(strFile);
#endif
		///---END QA80-13571 FIX_CRASH_WHEN_FILE_PATH_LENGTH_IS_TOO_LONG
		return true;
	}
	return false; // no change
}

/// Hong 05/25/07 v8.0627 BETTER_INITIAL_PATH
static string _get_default_path()
{
	string strPath = get_file_group_default_path(STR_TDM_FILE_GROUP); ///---Sim 05-25-2007 GET_DEFAULT_FROM_ORIGIN_OPTIONS
	if ( strPath.IsEmpty() )
		strPath = GetAppPath(FALSE);
	
	return strPath;
}
/// end BETTER_INITIAL_PATH

/// Hong 03/05/07 ADD_ERROR_CHECK
static bool _gui_check_update_path(TreeNode& trGetN)
{	
	TreeNode 	trPath = trGetN.fname;
	
	//get path, if path is empty, get current file path
	string		strPath = trPath.strVal;
	if(strPath.IsEmpty() || strPath == STR_UNASSIGNED)
		/// Hong 05/25/07 v8.0627 BETTER_INITIAL_PATH
		//strPath = GetAppPath();
		strPath = _get_default_path();
		/// end BETTER_INITIAL_PATH	
		
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	bool 	bIsSpecialPath = str_is_predefined_path(strPath);
	if(bIsSpecialPath)
		strPath = cvt_predefined_path_to_real_path(strPath);
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	//get ext and filname from path
	string strFile, strExtFromPath;
	separate_file_name_ext(strPath, strFile, strExtFromPath);
	string strFilename = GetFileName(strFile); //strFile have path
	
	//if fil name is empty, use actived wks'name as file name
	string strWkbName;
	WorksheetPage	wp;
	okxf_resolve_string_get_origin_object(trGetN.iwp.strVal, &wp);
	if(!wp)
		return false;
	strWkbName = wp.GetName();
	
	if(strFilename.IsEmpty())
	{
		strFile = GetFilePath(strFile) + strWkbName;
	}

	//update path in GUI
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	if(bIsSpecialPath)
	{
		strPath = trPath.strVal;
	}
	else
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL	
	/// Hong 09/24/07 QA80-10343 v8.0707 UPDATE_TO_NEW_DLL_SUPPORT_TDMS
	{
		//strPath =  strFile + ".tdm";
		strPath = strFile;
		/// Hong 10/08/07 QA80-9301 v8.0718 FIX_FAIL_EXPORT_AS_TDMS		
		if ( !strExtFromPath.IsEmpty() )
			strPath += "." + strExtFromPath;
		/// end FIX_FAIL_EXPORT_AS_TDMS
		_check_add_file_ext(strPath);
	}
	/// end UPDATE_TO_NEW_DLL_SUPPORT_TDMS
	
	trPath.strVal = strPath;
	
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	tree_set_browse_combo_attrib(trPath);	
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	
	return true; 
}

static bool _check_err_condition(TreeNode& trGetN, LPCSTR lpcszNodeName, string& strErrMsg)
{
	/// Iris 07/13/2007 v8.0659 SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
	/*
	///Cheney 2006-11-3 CHECK_IF_FILE_PATH_IS_VALID
	if(lpcszNodeName != NULL)  
	{
		/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		//string strPath = GetFilePath(trGetN.fname.strVal); //when init, path is empty, no need to check 
		string	strPath = GetFilePath(strAbsolutePath);
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		if(!strPath.IsPath())
		{
			/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
			//strErrMsg = CER_INVALID_FILEPATH;
			//return false;
			strErrMsg = XFWARN_WILL_CREATE_NONEXISTING_PATH;
			return true;
			///end CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
		}
	}
	///end CHECK_IF_FILE_PATH_IS_VALID
	*/
	///---Sim 07-13-2007 FIX_CHECK_PATH
	//check_relative_path(trGetN.fname.strVal, false, strErrMsg);
	///---END FIX_CHECK_PATH
	///end SHARE_CHECK_PATH_CODES_FOR_ALL_EXPORT_XFS
	
	if(NULL == lpcszNodeName || lstrcmpi(lpcszNodeName, "fname") == 0)
	{
		if(!_gui_check_update_path(trGetN))
		{
			strErrMsg = "Failed to update filename";
			return false;
		}		
	}
	
	///---Sim 07-13-2007 FIX_CHECK_PATH
	bool bOK = true;
	check_relative_path(trGetN.fname.strVal, false, &strErrMsg, &bOK);
	if( !bOK )
		return false;
	///---END FIX_CHECK_PATH
	
	///---Sim 06-05-2007 WARNING_EXPORT_TO_EXISTING_FILE
	/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	//if ( trGetN.fname.strVal.IsFile() )
	string strAbsolutePath = cvt_predefined_path_to_real_path(trGetN.fname.strVal); /// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	if(strAbsolutePath.IsFile())
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	{
		strErrMsg = EXPWARN_FILE_EXIST;
		strErrMsg += ":";
		/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		//strErrMsg += GetFileName(trGetN.fname.strVal);
		strErrMsg += GetFileName(strAbsolutePath);
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	}
	///---END WARNING_EXPORT_TO_EXISTING_FILE
	
	return true;
}
/// end ADD_ERROR_CHECK

///Jake 06/28/07 CODES_HAVE_BEEN_MOVED_TO_GLOBAL_FUN
/*
//---Jake 06/27/07 STORE_USER_FILE_NAME
static void _set_only_file_name_attrib(TreeNode& trGetN, LPCSTR lpcszFileName)
{
	string strOnlyFileName = GetFileName(lpcszFileName);
	trGetN.fname.SetAttribute(STR_ONLY_FILE_NAME_ATTRIB,strOnlyFileName);
}
//---end STORE_USER_FILE_NAME
*/
///end CODES_HAVE_BEEN_MOVED_TO_GLOBAL_FUN

void expNITDM(const WorksheetPage& iwp, const string& fname, const int& percent)
{
	if(!iwp)
		XF_THROW(CER_NO_WORKSHEET);
	
	//---- Iris 07/13/2007 fname is readonly, the code below is incorrect.
	//_check_add_file_ext(fname);
	//----
	
	/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
	//string strPath = GetFilePath(fname);
	//if(!strPath.IsPath())
	string strPath = cvt_predefined_path_to_real_path(fname);
	_check_add_file_ext(strPath);
	if( !GetFilePath(strPath).IsPath() )
	///end CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
	{
		/// Iris 07/04/2007 v8.0655 CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
		//XF_THROW(CER_INVALID_FILEPATH);
		if( !CreateDirectory(strPath, NULL) )
		{
			XF_THROW(CER_FAIL_TO_CREATE_PATH);
		}
		///end CREATE_PATH_IF_SPECIFIED_PATH_IS_NOT_EXISTED
	}
	
	/// Hong 03/06/07 FIX_FAIL_TO_CREATE_FILE_IF_ALREADY_EXIST
	// DDC_CreateFile fail to works if file already exist, so we delete it before import
	DeleteFile(strPath);
	/// end FIX_FAIL_TO_CREATE_FILE_IF_ALREADY_EXIST
	exportTDM tdmFile(strPath);
	int nErr = tdmFile.WriteFile(iwp, percent);	
	
	/// Hong 04/13/07 PRINT_XF_MSG_TO_SCRIPT_WINDOW
	string strMessage;
	///Folger 05/24/07 MODIFY_OUTPUT_INFO_MSG
	//strMessage.Format(_L("%s to export page %s to %s."), (fname.IsFile() && nErr == 0)?"Succeed":"Failed", iwp.GetName(), fname);
	///Folger 06/05/07 ADD_FAILURE_MSG
	if (strPath.IsFile() && nErr == 0)
		ocu_load_msg_str(XF_EXPORT_SUCCESS, &strMessage, iwp.GetName(), NULL, strPath);
	else
		ocu_load_msg_str(XF_EXPORT_FAILED, &strMessage, iwp.GetName(), NULL, strPath);
	///End ADD_FAILURE_MSG
	///End MODIFY_OUTPUT_INFO_MSG
	
	///Jake 06/28/07 SUPPORT_DEFALUT_KEY_NAME
	/*
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
	char szLastXFName[MAXFULLPATH];
	LT_get_str(LTVAR_LAST_XF, szLastXFName, MAXFULLPATH);
	update_recent_path_list(szLastXFName,fname);//update path to registry
	//----end GET_COMBO_LIST_FROM_REGISTRY
	*/
	update_recent_path_list(strPath);
	///end SUPPORT_DEFALUT_KEY_NAME
	
	///Folger 04/16/07 REPLACE_BY_OKOC_OUT_MSG
	//xf_print_msg(strMessage);
	okoc_out_msg(strMessage, 'I');
	///End REPLACE_BY_OKOC_OUT_MSG
	/// end PRINT_XF_MSG_TO_SCRIPT_WINDOW
}

//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 expNITDM_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return expNITDM_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
	if(lstrcmpi(lpcszNodeName, STR_NODE_FILE) == 0 )
	{
		/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		/*
		//string		strPath = trGetN.fname.strVal;
		//int nID = okutil_cvt_str_to_predefined_type(strPath);
		//if( nID != PDS_INVALID && nID != PDS_USER)
		//{
			//strPath = cvt_predefined_path_to_real_path(strPath);
			//string strFileName;
			//trGetN.fname.GetAttribute(STR_ONLY_FILE_NAME_ATTRIB,strFileName);
			//strPath = GetFilePath(strPath) + strFileName;
			//trGetN.fname.strVal = strPath;
		//}
		//_set_only_file_name_attrib(trGetN,strPath);	
		TreeNode trPath = trGetN.fname;
		tree_check_set_real_path(trPath);
		tree_set_only_file_name_attrib(trPath,trPath.strVal);
		*/
		///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL		
	}
	//----end GET_COMBO_LIST_FROM_REGISTRY
	
	/// Hong 03/05/07 ADD_ERROR_CHECK
	bOKEnable = _check_err_condition(trGetN, nEventID==GETNE_ON_INIT || nEventID==GETNE_ON_THEME? NULL:lpcszNodeName, strErrMsg);
	/// end ADD_ERROR_CHECK
	return false;

}

int expNITDM_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 expNITDM_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	/// Iris 07/04/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	/*
	//----Jake 06/27/07 GET_COMBO_LIST_FROM_REGISTRY
	if(nGetNDialog > 0) 
	{	
		//char szLastXFName[MAXFULLPATH];
		//TreeNode trPath = trGetN.fname;
		//LT_get_str(LTVAR_LAST_XF, szLastXFName, MAXFULLPATH); //Jake 06/27/07 GET_THE_NAME_OF_LAST_XF_CALLED
		//tree_set_browse_combo_attrib(trPath, szLastXFName, false);		
		TreeNode trPath = trGetN.fname;
		tree_set_browse_combo_attrib(trPath);
	}
	//----end GET_COMBO_LIST_FROM_REGISTRY
	*/
	///end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
	if(nGetNDialog > -1)
	{
		WorksheetPage	wp;
		okxf_resolve_string_get_origin_object(trGetN.iwp.strVal, &wp);
		if(!wp)
		{
			nRet = XFEVT_ABORT;
			warning_msg_box(CER_NO_WORKSHEET, true);
			return;
		}
		
		///---Sim 06-18-2007 ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_DLG
		/*
		if(!(dwCntrl & LTXF_FROM_GUI_PROMPT) || (dwCntrl & LTXF_THEME_USED))
		{
			//trim file name from path then set it to wks name
			if((dwCntrl & LTXF_THEME_USED) && !trGetN.fname.strVal.IsEmpty())
			{
				trGetN.fname.strVal = GetFilePath(trGetN.fname.strVal) + wp.GetName();
			}
		}
		*/
		
		string strFileName = trGetN.fname.strVal;
		
		if ( LTXF_FROM_GUI_MENU & dwCntrl ) // if from menu, ignore default value fname$
			strFileName = "";
		
		//if(nGetNDialog == 0)
		//{
			//if apply theme, should ask user input a file name but not pop-up a dlg
			//if(is_empty_or_unassigned(trGetN.fname.strVal) || (dwCntrl & LTXF_THEME_USED))
		if( is_empty_or_unassigned(strFileName) )
		{
			/*
			string strExt = ".tdm";
	
			string strPageName = wp.GetName();
			/// Hong 05/25/07 v8.0627 BETTER_INITIAL_PATH
			//string strPath = is_empty_or_unassigned(trGetN.fname.strVal)? GetAppPath() : GetFilePath(trGetN.fname.strVal);				
			string strPath = is_empty_or_unassigned(trGetN.fname.strVal)? _get_default_path() : GetFilePath(trGetN.fname.strVal);
			/// end BETTER_INITIAL_PATH
			/// Hong 03/14/07 FIX_FAIL_OPEN_SAVE_DIALOG
			//strPath = GetSaveAsBox("*tdm National Instrucment", strPath, strPageName);
			/// Hong 05/25/07 v8.0627 BETTER_INITIAL_PATH
			//strPath = GetSaveAsBox("[National Instrucment File(*.tdm)] *.tdm", strPath, strPageName);
			strPath = GetSaveAsBox("[National Instrucment File(*.tdm)] *.tdm", strPath, strPageName + strExt);
			/// end BETTER_INITIAL_PATH
			/// end FIX_FAIL_OPEN_SAVE_DIALOG
			*/
			
			string strWksName = wp.GetName();
			
			int nFilesRet;
			if ( 0 != nGetNDialog )
			{
				int nShowOptionsDlg = 1;
				/// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_TDM_FILE_GROUP, _L("National Instrucment File"),
				//	0, NULL, strWksName, _L("Show Options Dialog"), &nShowOptionsDlg);
				nFilesRet = get_saveas_box_by_file_group(strFileName, STR_TDM_FILE_GROUP, NULL,
					0, NULL, strWksName, STR_SHOW_OPT_DLG, &nShowOptionsDlg);
				/// end CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
				nRet = (nShowOptionsDlg ? XFEVT_PROCEED : XFEVT_PROCEED_NO_DLG);
			}
			else
			{
				//nFilesRet = get_saveas_box_by_file_group(strFileName, STR_TDM_FILE_GROUP, _L("National Instrucment File"), 0, NULL, strWksName);
				nFilesRet = get_saveas_box_by_file_group(strFileName, STR_TDM_FILE_GROUP, NULL, 0, NULL, strWksName); /// Hong 09/26/07 8.0709 CLEAN_OPEN_SAVE_DIALOG_TITLE_TO_DEFAULT
			}
			
			//if(strPath.IsEmpty())
			if ( 0 == nFilesRet )
			{
				nRet = XFEVT_ABORT;
				//warning_msg_box(XFERR_UI_USER_CANCEL, false); // Hong 03/14/07 NO_NEED_CANCEL_WARNING
				return;
			}
	
			//trGetN.fname.strVal = strPath;
			trGetN.fname.strVal = strFileName;
			/// Iris 07/03/2007 v8.0654 USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
			//tree_set_only_file_name_attrib(trGetN.fname,strFileName);///Jake 06/27/07 SET_ONLY_FILENAME
			////end USAGE_OF_BROWSER_COMBO_NOT_CONSISTENT_IN_FILE_AND_PATH_CONTROL
		}
		//}
		///---END ADD_SHOW_DLG_CHECK_BOX_TO_SAVE_AS_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 expNITDM_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
