#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	l_j,
	l_g,
	l_c,
};

//--------- modification log --------
// CPY 8/28/2007 UFF_ONLY_IF_PROCESSING_ALL
// CPY 8/28/2007 XML_TO_XF_UPDATE_PROBLEMATIC
// CPY 11/20/07 FORCE_LOAD_XF_WIHTOUT_VALDATA_LOCALIZATION
// CPY 11/30/07 USE_SAME_CODE_WHEN_XF_NAME_IS_GIVEN
// CPY 12/3/07 ALLOW_CAT_IN_XML_FILE_NAME
// CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
// Kyle 02/13/2009 QA80-12037 REMOVE_HEADER_THAT_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
// 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>
//put your own support static functions here

///Kyle 02/13/2009 QA80-12037 REMOVE_HEADER_THAT_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
#define STR_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION "=="
///End REMOVE_HEADER_THAT_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION

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;
	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;
	default:
		return "";
	}
	
	return "";
}

//----- CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
static void _get_node_to_xf(XFunction& xf, TreeNode& trGUIXF, LPCSTR lpcszTag, int nNodeID, int nLang, LPCSTR lpcszVarName = NULL)
{
	TreeNode trNode;
	trNode = trGUIXF.GetNode(lpcszTag);
	if ( trNode )
	{
		int nCodePage = _cvt_lang_to_code_page(nLang);
		string strVal = trNode.GetNodeText(nCodePage);

		///Kyle 02/13/2009 QA80-12037 REMOVE_HEADER_THAT_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION
		if(0 == strVal.Find(STR_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION))
			strVal = strVal.Mid(2);
		///End REMOVE_HEADER_THAT_MARK_WHOLE_SENTENCE_FOR_LOCALIZATION

		if(lpcszVarName)
			xf.SetVarHelpText(lpcszVarName, strVal, nNodeID, nLang);
		else
			xf.SetHelpText(strVal, nNodeID, nLang);
	}
}
//-----



void gui2xf(const string& xfname, const string& cat, const int& l, const string& path)
{
	int nLang = _get_language_from_gui(l);
	string strXML = _get_gui_filename(nLang);
	if ( strXML.IsEmpty() )
		XF_THROW(XFERR_INVALID_ARG);
	//----- CPY 12/3/07 ALLOW_CAT_IN_XML_FILE_NAME
	string strFilename = GetFileName(strXML, true);
	if(!cat.IsEmpty())
	{
		strXML.Format("%s_%s.xml", strFilename, cat);
	}
	//----
	
	string strXMLPath = path;
	strXMLPath.TrimRight("\\");
	strXMLPath += "\\" + strXML;
	//--- CPY 12/18/07 PROCESS_SINGLE_XF_XML
	if(!strXMLPath.IsFile() && !xfname.IsEmpty())
		strXMLPath.Format("%s%s_%s.xml", path, strFilename, xfname);
	//---
	Tree trGUI;
	if ( !trGUI.Load(strXMLPath) )
		XF_THROW(CER_INVALID_FILEPATH);	
		
	//string strTempXFTemp(xfname);
	//strTempXFTemp.MakeValidCName();
	
	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);
	okutil_find_files_from_map(&vsTempFileName, &vsPath, SUPPORTFILE_XF, FF_USE_C_NAMES, NULL, 0, NULL, NULL, ORIGIN_PATH_USER);
	//------
	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(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++)
		{
			if(vsXFNames[ii].CompareNoCase(strTemp) == 0)
			{
				nn = ii;
				break;
			}
		}
		if(nn >= 0)
		{
			strTemp = vsXFFileNames[ii];
			vsXFFileNames.SetSize(1);
			vsXFFileNames[0] = strTemp;
		}
		else
		{
			string strMsg;
			strMsg.Format("%s not found", strTemp);
			warning_msg_box(strMsg, false);
			return;
		}
	}
	//---------------
	
	XFunction xf;	
	string strMsg, strVal, strAttrVal;
	vector<string> vsVarNames;
	//---- CPY 8/28/2007 XML_TO_XF_UPDATE_PROBLEMATIC
	//warning_msg_box(_L("Processing..."), false);
	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++ )
	{
		string strXFFileName = GetFileName(vsXFFileNames[ii], true); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		TreeNode trXF;//---- CPY 8/28/2007 XML_TO_XF_UPDATE_PROBLEMATIC
		//if( !xf.Load(&trXF, vsXFFileNames[ii]) )
		//----- CPY 11/20/07 FORCE_LOAD_XF_WIHTOUT_VALDATA_LOCALIZATION
		//if( !xf.Load(&trXF, vsXFFileNames[ii], dwCtrl) ) /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		if( !xf.LoadNoLocalize(&trXF, vsXFFileNames[ii], dwCtrl) )
		//-----
		{
			//strMsg.Format(_L("%s failure : XF fails loading"), vsXFFileNames[ii]);
			strMsg.Format(_L("%s failure : XF fails loading"), strXFFileName); /// 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 = trGUI.GetNode(strXFName);
		if ( !trGUIXF )
		{
			//strMsg.Format(_L("%s has no translation info"), vsXFFileNames[ii]);
			strMsg.Format(_L("%s has no translation info"), strXFFileName); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
			warning_msg_box(strMsg, false);
			continue;
		}	
		//----- CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
		/*
		TreeNode trNode;
		trNode = trGUIXF.GetNode(STR_XF_GUI_DESCRIPTION);
		if ( trNode )
			xf.SetHelpText(trNode.strVal, IDV_HELP_DESCRIPTION, nLang);
		
		trNode = trGUIXF.GetNode(STR_XF_GUI_HINT);
		if ( trNode )
			xf.SetHelpText(trNode.strVal, IDV_HELP_BRIEF_INFO, nLang);
		//---- CPY 12/16/2007 NEED_CUSTOM_BUTTONS_TEXT
		trNode = trGUIXF.GetNode(STR_XF_CUSTOM_BUTTON);
		if ( trNode )
			xf.SetHelpText(trNode.strVal, IDV_GETN_CUSTOM_BUTTONS, nLang);
		//---
		*/
		_get_node_to_xf(xf, trGUIXF, STR_XF_GUI_DESCRIPTION, 	IDV_HELP_DESCRIPTION, 	nLang);
		_get_node_to_xf(xf, trGUIXF, STR_XF_GUI_HINT, 			IDV_HELP_BRIEF_INFO, 	nLang);
		_get_node_to_xf(xf, trGUIXF, STR_XF_CUSTOM_BUTTON, 		IDV_GETN_CUSTOM_BUTTONS, nLang);
		//-----
		///------ 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
		TreeNode trGUIVar;
		for (int jj = 0; jj < vsVarNames.GetSize(); jj++)
		{
			trGUIVar = trGUIXF.GetNode(vsVarNames[jj]);
			if ( !trGUIVar )
				continue;
			//----- CPY 12/19/07 J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
			/*
			trNode = trGUIVar.GetNode(STR_XF_GUI_VAR_DISPLAY_NAME);
			if ( trNode )
				xf.SetVarHelpText(vsVarNames[jj], trNode.strVal, IDV_VAR_HELP_DESCRIPTION, nLang);
			
			trNode = trGUIVar.GetNode(STR_XF_GUI_VAR_DESCRIPTION);
			if ( trNode )
				xf.SetVarHelpText(vsVarNames[jj], trNode.strVal, IDV_VAR_HELP_REMARK, nLang);
			
			trNode = trGUIVar.GetNode(STR_XF_GUI_VAR_HINT);
			if ( trNode )
				xf.SetVarHelpText(vsVarNames[jj], trNode.strVal, IDV_VAR_AUTOCOMPLETE_HINT, nLang);
			*/
			_get_node_to_xf(xf, trGUIVar, STR_XF_GUI_VAR_DISPLAY_NAME, 	IDV_VAR_HELP_DESCRIPTION, nLang, vsVarNames[jj]);
			_get_node_to_xf(xf, trGUIVar, STR_XF_GUI_VAR_DESCRIPTION, 	IDV_VAR_HELP_REMARK, nLang, vsVarNames[jj]);
			_get_node_to_xf(xf, trGUIVar, STR_XF_GUI_VAR_HINT, 	IDV_VAR_AUTOCOMPLETE_HINT, nLang, vsVarNames[jj]);
			//---- end J_XML_GIBBERISH_WHEN_PROCESSED_IN_E_OS
		}
		//---- CPY 8/28/2007 XML_TO_XF_UPDATE_PROBLEMATIC
		/*
		trNode = xf.GetTree();
		xf_save(trNode);
		
		strMsg.Format(_L("%s success"), vsXFFileNames[ii]);
		*/
		if(xf_save(trXF))
			//strMsg.Format(_L("%d: %s"), ii+1, vsXFFileNames[ii]);
			strMsg.Format("%d: %s", ii+1, strXFFileName); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		else
			//strMsg.Format(_L("%d: %s ***** failed."), ii+1, vsXFFileNames[ii]);
			strMsg.Format(_L("%d: %s ***** save failed."), ii+1, strXFFileName); /// Hong 08/31/07 QA80-10310 v8.0693 FIX_FAIL_LOADING_XF_WITH_NOT_C_NAME
		//---- end XML_TO_XF_UPDATE_PROBLEMATIC	
		warning_msg_box(strMsg, false);
	}
		
	strMsg.Format("%s\r\nFinished.", 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 gui2xf_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return gui2xf_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	return false;

}

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