#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	l_j,
	l_g,
	l_c,
	l_e,
};
///Kyle 09/22/08 ADD_A_PARAMETER_TO_SET_PATH_TYPE
enum
{
	path_type_user,
	path_type_system,
};
///End ADD_A_PARAMETER_TO_SET_PATH_TYPE

//--------- modification log --------
// CPY 8/28/2007 NEED_ERR_HANDLING_FOR_FOLDER_NOT_EXISTED
// CPY 8/28/2007 UFF_ONLY_IF_PROCESSING_ALL
//	CPY 11/20/07 FORCE_LOAD_XF_WIHTOUT_VALDATA_LOCALIZATION
// CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
// CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
// CPY 12/18/07 ADD_FNAME_TO_SUPPORT_CREATE_XML_FOR_ALL
// CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
//	Folger 08/18/08 FIX_XF_MAPPING_NAME_COMPARE
// Kyle 08/18/08 QA80-12037 MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
// Kyle 09/22/08 ADD_A_PARAMETER_TO_SET_PATH_TYPE
// Folger 09/25/09 SUPPORT_PROCESS_SINGLE_XF_GUI_XML
// Folger 10/20/09 XF2GUI_SHOULD_NOT_SKIP_ANY_VARIABLES
//-----------------------------------

#include <xfguixml.h> //CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
#include <..\originlab\xfunctionex.h>


#define STR_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION "=="			///Kyle 08/18/08 QA80-12037 MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
#define STR_VAR_HINT							 "hint"

static int _get_language_from_gui(int language)
{
	int nLang;
	switch (language)
	{
	case l_j:
		nLang = IDRS_HELP_J;
		break;
	case l_g:
		nLang = IDRS_HELP_G;
		break;
	case l_c:
		nLang = IDRS_HELP_C;
		break;
	case l_e:	/// Max 12/4/07 ADD_ENGLISH_GUI_FILE
	default:
		nLang = IDRS_HELP_E;
		break;
	}
	
	return nLang;
}

static string _get_gui_filename(int nLang)
{
	switch (nLang)
	{
	case IDRS_HELP_J:
		return STR_XF_GUI_JAPANESE_FILE;
	case IDRS_HELP_G:
		return STR_XF_GUI_GERMAN_FILE;
	case IDRS_HELP_C:
		return STR_XF_GUI_CHINESE_FILE;
	/// Max 12/4/07 ADD_ENGLISH_GUI_FILE	
	case IDRS_HELP_E:
		return STR_XF_GUI_ENGLISH_FILE;
	/// END ADD_ENGLISH_GUI_FILE
	default:
		return "";
	}
	
	return "";
}

//---- CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
static void _set_node(TreeNode& trGUIXF, LPCSTR lpcszTag, string& strVal, string& strAttrVal, int nLang, BOOL bSetAsCDATA = false)
{
	int nCPA = _cvt_lang_to_code_page(nLang);
	
	TreeNode trNode;
	trNode = trGUIXF.AddTextNode(strVal, lpcszTag, -1, bSetAsCDATA, nCPA);
	if(strAttrVal.IsEmpty())
		strAttrVal = " ";
	else if(strVal.IsEmpty())
		strVal = " "; // if E is not empty, localized cannot be empty
		
	trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
}

//----- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
static void _set_node(XFunction& xf, TreeNode& trGUIXF, int nDataID, LPCSTR lpcszTag, int nLang)
{
	string	strVal = xf.GetHelpText(nDataID, nLang, true);		
	string strAttrVal = xf.GetHelpText(nDataID);
	//---- CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
	/*
	TreeNode trNode;
	trNode = trGUIXF.AddTextNode(strVal, lpcszTag);
	if(strAttrVal.IsEmpty())
		strAttrVal = " ";
		
	trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
	*/
	///Kyle 08/18/08 QA80-12037 MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
	if(IDV_HELP_DESCRIPTION==nDataID)
		strVal = STR_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION + strVal;
	///End MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
	_set_node(trGUIXF, lpcszTag, strVal, strAttrVal, nLang);
}
//----- end NEED_CUSTOM_BUTTONS_TEXT
/*	
strVal = xf.GetHelpText(IDV_HELP_BRIEF_INFO, nLang, true);
strAttrVal = xf.GetHelpText(IDV_HELP_BRIEF_INFO);
trNode = trGUIXF.AddTextNode(strVal, STR_XF_GUI_HINT);
trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
*/



void xf2gui(const string& xfname, const string& cat, const int& l, const string& path, const string& fname, const int& pathtype)
{
	vector<string> vsXFFileNames, vsXFNames;	
	vector<string> vsTempFileName, vsPath;
	
	//---- CPY 8/28/2007 UFF_ONLY_IF_PROCESSING_ALL
	//okutil_find_files_from_map(&vsTempFileName, &vsPath, SUPPORTFILE_XF, FF_USE_C_NAMES);
	///Kyle 09/22/08 ADD_A_PARAMETER_TO_SET_PATH_TYPE
	//okutil_find_files_from_map(&vsTempFileName, &vsPath, SUPPORTFILE_XF, FF_USE_C_NAMES, NULL, 0, NULL, NULL, ORIGIN_PATH_USER);
	int nPathType;
	if( pathtype==path_type_user )
		nPathType = ORIGIN_PATH_USER;
	else
		nPathType = ORIGIN_PATH_SYSTEM;
	okutil_find_files_from_map(&vsTempFileName, &vsPath, SUPPORTFILE_XF, FF_USE_C_NAMES, NULL, 0, NULL, NULL, nPathType);
	///End ADD_A_PARAMETER_TO_SET_PATH_TYPE
	//----
	if ( cat.IsEmpty() )
	{
		//vsXFFileNames = vsTempFileName;
		vsXFFileNames = vsPath; /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		vsXFNames = vsTempFileName;//--------------- CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
	}
	else
	{
		string strPath, strCat(cat);
		strCat.MakeLower();
		for (int ii = vsTempFileName.GetSize() - 1;  ii >= 0; ii--)
		{
			strPath = GetFilePath(vsPath[ii]);
			strPath.MakeLower();
			if ( strPath.Find(strCat) > 0 )
			{
				//vsXFFileNames.Add(vsTempFileName[ii]);
				vsXFFileNames.Add(vsPath[ii]); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
				vsXFNames.Add(vsTempFileName[ii]);//--------------- CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
			}
		}			
	}					
	//--------------- CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
	string strTemp = xfname;
	strTemp.MakeValidCName();
	if(!strTemp.IsEmpty())
	{
		int nn = -1;
		for(int ii = 0; ii < vsXFNames.GetSize(); ii++)
		{
			//------ Folger 08/18/08 FIX_XF_MAPPING_NAME_COMPARE
			//if(vsXFNames[ii].Compare(strTemp) == 0)
			if(vsXFNames[ii].CompareNoCase(strTemp) == 0)
			//------ End FIX_XF_MAPPING_NAME_COMPARE
			{
				nn = ii;
				break;
			}
		}
		if(nn >= 0)
		{
			strTemp = vsXFFileNames[ii];
			vsXFFileNames.SetSize(1);
			vsXFFileNames[0] = strTemp;
		}
		else
			vsXFFileNames.SetSize(0);	
	}
	//---------------
	//---- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT		
	if(vsXFFileNames.GetSize() == 0)
	{
		warning_msg_box("No XFunction found!", false);
		return;
	}
	//----
	
	int nLang = _get_language_from_gui(l);
	string strXML = _get_gui_filename(nLang);
	//---- CPY 12/18/07 ADD_FNAME_TO_SUPPORT_CREATE_XML_FOR_ALL
	if(!fname.IsEmpty())
		strXML = fname;
	//----
	///------ Folger 09/25/09 SUPPORT_PROCESS_SINGLE_XF_GUI_XML
	else if ( !strTemp.IsEmpty() )
	{
		strXML.Format("%s_%s.xml", GetFileName(_get_gui_filename(nLang), TRUE), GetFileName(strTemp, TRUE));
	}
	///------ End SUPPORT_PROCESS_SINGLE_XF_GUI_XML
	if ( strXML.IsEmpty() )
		XF_THROW(XFERR_INVALID_ARG);
	
	string strXMLPath = path;
	strXMLPath.TrimRight("\\");
	//------ CPY 8/28/2007 NEED_ERR_HANDLING_FOR_FOLDER_NOT_EXISTED
	if( !CheckMakePath(strXMLPath) )
		XF_THROW(XFERR_INVALID_ARG);	
	//------
	strXMLPath += "\\" + strXML;
	//--------------- CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
	// following code make no sense to me
	//bool bXMLExist = false;
	//Tree trGUI;
	//if ( !strTempXFTemp.IsEmpty() && strXMLPath.IsFile() )
	//	bXMLExist = trGUI.Load(strXMLPath);
	Tree trGUI;
	bool bXMLExist = false;
	if(strXMLPath.IsFile() )
		bXMLExist = trGUI.Load(strXMLPath);
	//---------------
	//----- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
	//XFunction xf;	
	//-----
	string strMsg, strVal, strAttrVal;
	vector<string> vsVarNames;
	warning_msg_box("Processing (ESC to skip to the end)...", false);
	//--------------- CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
	///// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
	//DWORD dwCtrl = 0;
	//if ( xfname.IsEmpty() )
	//	dwCtrl = LTXF_BY_FILENAME;
	/// end FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
	DWORD dwCtrl = LTXF_BY_FILENAME;
	//---------------
	
	waitCursor cur;//----- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
	for( int ii = 0; ii < vsXFFileNames.GetSize() && !cur.CheckEsc(); ii++ )
	{
		XFunction xf;//----- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT moved to inside scope	
	
		//if( !xf.Load(NULL, vsXFFileNames[ii]) )
		//---------- CPY 11/20/07 FORCE_LOAD_XF_WIHTOUT_VALDATA_LOCALIZATION
		//if( !xf.Load(NULL, vsXFFileNames[ii], dwCtrl) ) /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		if( !xf.LoadNoLocalize(NULL, vsXFFileNames[ii], dwCtrl) )
		//----------
		{
			//strMsg.Format(_L("%s failure : XF fails loading"), vsXFFileNames[ii]);
			strMsg.Format(_L("%s failure : XF fails loading"), GetFileName(vsXFFileNames[ii], true)); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
			warning_msg_box(strMsg, false);
			continue;
		}						
		
		string strXFName = xf.GetFunctionName(true);
		TreeNode trGUIXF, trGUIVar, trNode;
		if ( bXMLExist )
			trGUIXF = trGUI.GetNode(strXFName);
		if ( !trGUIXF )
		 	trGUIXF = trGUI.AddNode(strXFName);	
		 else
		 	trGUIXF.Reset();
		 				
		//----- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
		/*
		strVal = xf.GetHelpText(IDV_HELP_DESCRIPTION, nLang, true);		
		strAttrVal = xf.GetHelpText(IDV_HELP_DESCRIPTION);
		trNode = trGUIXF.AddTextNode(strVal, STR_XF_GUI_DESCRIPTION);		
		trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
		
		strVal = xf.GetHelpText(IDV_HELP_BRIEF_INFO, nLang, true);
		strAttrVal = xf.GetHelpText(IDV_HELP_BRIEF_INFO);
		trNode = trGUIXF.AddTextNode(strVal, STR_XF_GUI_HINT);
		trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
		*/
		_set_node(xf, trGUIXF, IDV_HELP_DESCRIPTION, STR_XF_GUI_DESCRIPTION, nLang);
		_set_node(xf, trGUIXF, IDV_HELP_BRIEF_INFO, STR_XF_GUI_HINT, nLang);
		_set_node(xf, trGUIXF, IDV_GETN_CUSTOM_BUTTONS, STR_XF_CUSTOM_BUTTON, nLang);
		//----- end NEED_CUSTOM_BUTTONS_TEXT
	
		///------ Folger 10/20/09 XF2GUI_SHOULD_NOT_SKIP_ANY_VARIABLES
		//xf.GetVariables(vsVarNames);
		xf.GetVariables(vsVarNames, NULL, NULL, FALSE, NULL, NULL, XF_GET_ALL_VARIABLES);
		///------ End XF2GUI_SHOULD_NOT_SKIP_ANY_VARIABLES
		for (int jj = 0; jj < vsVarNames.GetSize(); jj++)
		{
			trGUIVar = trGUIXF.AddNode(vsVarNames[jj]);
			
			if ( xf.GetVarLabel(vsVarNames[jj], strAttrVal, IDRS_HELP_E) )
				strAttrVal.Empty();
				
			if ( xf.GetVarLabel(vsVarNames[jj], strVal, nLang) )
				strVal.Empty();
			
			if ( strVal.IsEmpty() )
				strVal = strAttrVal;
			//---- CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
			/*
			trNode = trGUIVar.AddTextNode(strVal, STR_XF_GUI_VAR_DISPLAY_NAME);
			trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
			
			strAttrVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_HELP_REMARK);
			strVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_HELP_REMARK, nLang, true);
			trNode = trGUIVar.AddTextNode(strVal, STR_XF_GUI_VAR_DESCRIPTION);
			trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
			
			strAttrVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_AUTOCOMPLETE_HINT);
			strVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_AUTOCOMPLETE_HINT, nLang, true);
			trNode = trGUIVar.AddTextNode(strVal, STR_XF_GUI_VAR_HINT);
			trNode.SetAttribute(STR_ATTRIB_ENGLISH, strAttrVal);
			*/
			///Kyle 08/18/08 QA80-12037 MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
			if(vsVarNames[jj].Find(STR_VAR_HINT) == 0)
				strVal = STR_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION + strVal;
			///End MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
			_set_node(trGUIVar, STR_XF_GUI_VAR_DISPLAY_NAME, strVal, strAttrVal, nLang);
			
			strAttrVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_HELP_REMARK);
			strVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_HELP_REMARK, nLang, true);
			///Kyle 08/18/08 QA80-12037 MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
			strVal = STR_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION + strVal;
			///End MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
			_set_node(trGUIVar, STR_XF_GUI_VAR_DESCRIPTION, strVal, strAttrVal, nLang);
			
			strAttrVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_AUTOCOMPLETE_HINT);
			strVal = xf.GetVarHelpText(vsVarNames[jj], IDV_VAR_AUTOCOMPLETE_HINT, nLang, true);
			_set_node(trGUIVar, STR_XF_GUI_VAR_HINT, strVal, strAttrVal, nLang);
			//----
		}
	
		//strMsg.Format(_L("%s success"), vsXFFileNames[ii]);
		//----- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
		//strMsg.Format(_L("%s success"), GetFileName(vsXFFileNames[ii], true)); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		strMsg.Format("%s: Vars = %d", GetFileName(vsXFFileNames[ii], true),  vsVarNames.GetSize());
		//-----
		warning_msg_box(strMsg, false);
	}
	
	if(trGUI.Save(strXMLPath))
		strMsg.Format(_L("Finished, output file(%s) created/updated."), strXMLPath);
	else
		strMsg.Format("failed to save to output file\r\n%s", strXMLPath);
	
	warning_msg_box(strMsg, false);
		
}

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

}

int xf2gui_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	
	return -1;
	
}
//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 xf2gui_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 xf2gui_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
