#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>

#include <o8dlg.h>
//----- Modification log ---------
//CPY 9/22/07 SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY
//CPY 9/24/07 HIDE_LOCAL_PATH_ALWAYS_USE_INTERNAL_RESOLUTION
//CPY 9/28/2007 REMOVE_FROM_GROUP_SHOULD_BE_POSSIBLE
//Fisher 11/15/07 ADD_MAP_ID_TO_CHM
//Folger 08/11/08 QA80-11988 IMPROVE_ERROR_CHECKING_FOR_SETTING_GROUP_SERVER
//Folger 08/12/08 QA80-11988-P3 ERROR_HANDLING_FOR_SET_GROUP_CLIENT_BEFORE_SETTING_UP_GROUUP_LEADER
//Folger 01/21/09 REMOVE_LOCAL_VARIABLE_FROM_GUI_SINCE_INTERNAL_RESOLUTION_IS_ALWAYS_USED
//Folger 08/27/09 QA80-14215 CHANGE_USER_FILES_FOLDER_IN_ORIGIN

//------ Folger 08/11/08 QA80-11988 IMPROVE_ERROR_CHECKING_FOR_SETTING_GROUP_SERVER
#define UNC_PATH_PREFIX   "\\\\"

///------ Folger 08/27/09 QA80-14215 CHANGE_USER_FILES_FOLDER_IN_ORIGIN
//#define ADD_BACKSLASH_IF_NOT(strPath) {string strLast = strPath.Right(1); if(strLast != "\\") strPath += "\\";}
///------ End CHANGE_USER_FILES_FOLDER_IN_ORIGIN

static	bool	_is_path_writable(LPCSTR lpcszFilePath)
{
	string	strLocal;
	okutil_resolve_UNC_to_local_path(&strLocal, lpcszFilePath);
	if ( !strLocal.IsEmpty() )
		return true;
	///------ Folger 08/27/09 QA80-14215 CHANGE_USER_FILES_FOLDER_IN_ORIGIN
	//string	strTmpDir = lpcszFilePath;
	//ADD_BACKSLASH_IF_NOT(strTmpDir);
	//strTmpDir += "Tmp";
	//
	//if ( !CreateDirectory(strTmpDir, NULL) )
		//return false;
	//
	//RemoveDirectory(strTmpDir);
	//return true;
	return is_path_writable(strLocal);
	///------ End CHANGE_USER_FILES_FOLDER_IN_ORIGIN
}
//------ End IMPROVE_ERROR_CHECKING_FOR_SETTING_GROUP_SERVER


void group_server(const string& path, const string& hint, const int& server)
{
	
	//----- CPY 9/28/2007 REMOVE_FROM_GROUP_SHOULD_BE_POSSIBLE
	//make new folder if needed
	//if (!local.IsEmpty())
	//	CheckMakePath(local);
	//-----
	//---- CPY 9/28/2007 REMOVE_FROM_GROUP_SHOULD_BE_POSSIBLE
	string strPath = path;
	strPath.TrimLeft(); strPath.TrimRight();
	if(strPath.IsEmpty())
	{
		okutil_set_group_server(NULL, -1);
		warning_msg_box(IDXFINFO_GROUP_FEATURE_RESET, false, 'W');
		return;
	}
	//----
		
	string strOldPath;
	BOOL bWasServer= okutil_is_group_server(&strOldPath);
	int nSetServer = server;
	if(bWasServer && 0==server)
		nSetServer = -1;
	//---- CPY 9/24/07 HIDE_LOCAL_PATH_ALWAYS_USE_INTERNAL_RESOLUTION
	//int nn = okutil_set_group_server(path, nSetServer, local);
	int nn = okutil_set_group_server(strPath, nSetServer);
	//----
	//----- end SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY
	
	if(nn != 0)
	{
		//------ Folger 08/12/08 QA80-11988-P3 ERROR_HANDLING_FOR_SET_GROUP_CLIENT_BEFORE_SETTING_UP_GROUUP_LEADER
		if ( nn == GSERR_SERVER_LOCATION_INVALID )
		{
			///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
			//warning_msg_box(XFERR_INVALID_GROUP_SERVER_FOLDER, false, 'E');
			XF_THROW(XFERR_INVALID_GROUP_SERVER_FOLDER);
			///-----End CLEANUP_XFS_ERROR_CODE
			return;
		}
		//------ End ERROR_HANDLING_FOR_SET_GROUP_CLIENT_BEFORE_SETTING_UP_GROUUP_LEADER
		
		///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
		//warning_msg_box(XFWARN_GROUP_SERVER_UPDATE_FAILED, nn, false, 'E');
		XF_THROW_EX(XFWARN_GROUP_SERVER_UPDATE_FAILED, nn);
		///-----End CLEANUP_XFS_ERROR_CODE
		return;
	}
	
	if(nSetServer > 0)//---- CPY 9/22/07 SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY
	{
		if((nn = okutil_copy_essential_files_upon_change_group_folder(true)) == 0)
			warning_msg_box(IDXFINFO_NEW_GROUP_SERVER, strPath, false, 'R');
		else
		{
			///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE			
			//warning_msg_box(XFWARN_GROUP_SERVER_UPDATE_FAILED, nn, false, 'E');
			XF_THROW_EX(XFWARN_GROUP_SERVER_UPDATE_FAILED, nn);
			///-----End CLEANUP_XFS_ERROR_CODE
		}
	}
	else // for client, need to trigger new update
	{
		LT_execute("doc -k"); // to reset first time run idle processing
		warning_msg_box(IDXFINFO_GROUP_SERVER_UPDATED, strPath, false, 'R');
	}
	//success
	
}

//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 group_server_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return group_server_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	//---- CPY 9/22/07 SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY
	/*
	if (nEventID > 0 && lstrcmpi(lpcszNodeName, "type") == 0)
	{
		trGetN.local.Show = trGetN.type.nVal == type_groupserverlocal;
	}
	*/
	///------ Folger 01/21/09 REMOVE_LOCAL_VARIABLE_FROM_GUI_SINCE_INTERNAL_RESOLUTION_IS_ALWAYS_USED
	//if (nEventID < 0 || (nEventID > 0 && lstrcmpi(lpcszNodeName, "server") == 0))
	//{
		//trGetN.local.Show = false;//trGetN.server.nVal;CPY 9/24/07 HIDE_LOCAL_PATH_ALWAYS_USE_INTERNAL_RESOLUTION
	//}
	///------ End REMOVE_LOCAL_VARIABLE_FROM_GUI_SINCE_INTERNAL_RESOLUTION_IS_ALWAYS_USED
	//------ Folger 08/11/08 QA80-11988 IMPROVE_ERROR_CHECKING_FOR_SETTING_GROUP_SERVER
	trGetN.server.Enable = 1;
	if ( !trGetN.path.strVal.IsEmpty() )
	{
		/// need to check if path not empty, empty means disconnect from server/client
		if ( trGetN.path.strVal.Find(UNC_PATH_PREFIX) != 0 || !trGetN.path.strVal.IsPath() )
		{
			strErrMsg = XFERR_GROUP_INVALID_UNC_PATH;
			bOKEnable = false;
		}
		else if ( !_is_path_writable(trGetN.path.strVal) )
		{
			strErrMsg = XFWARN_GROUP_SERVER_FOLDER_NOT_WRITABLE;
			trGetN.server.nVal = 0;
			trGetN.server.Enable = 0;
		}
		return true;
	}
	//------ End IMPROVE_ERROR_CHECKING_FOR_SETTING_GROUP_SERVER	
	
	//---- end SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY	
	return false;
	

}

int group_server_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 group_server_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	
	//------ fisher 11/16/07 ADD_MAP_ID_TO_CHM
	if(is_use_origin_chm())
		trGetN.SetAttribute(STR_HELPID_ATTRIB, IDD_SGFL_DLG);
	
	if(nGetNDialog > 0)
	{
		
		//if (!okutil_is_group_files_update_feature_permitted())
		//{
			//warning_msg_box(XFWARN_GROUP_CANNOT_USE, true);
			//nRet = XFEVT_ABORT;
			//return;
		//}
		
		/// Hong 09/05/07 v8.0696 GROUP_SERVER_PATH_NO_NEED_CHECK_IF_ALREADY_SERVER
		//if(okutil_set_group_server(NULL) < 0)
		int nRetTemp = okutil_set_group_server(NULL);
		if ( nRetTemp < 0 && GSERR_LOCAL_INI_SERVER_NOT_SET != nRetTemp && GSERR_LOCAL_LOCATION_INVALID != nRetTemp )		
		/// end GROUP_SERVER_PATH_NO_NEED_CHECK_IF_ALREADY_SERVER
		{
			//warning_msg_box(XFWARN_GROUP_FOLDER_NOT_WRITABLE, true);
			int			nMsg = 0;
			if (GSERR_ILLEGAL == nRetTemp)
				nMsg = XFWARN_GROUP_CANNOT_USE;
			else
				nMsg = XFWARN_GROUP_FOLDER_NOT_WRITABLE;
			warning_msg_box(nMsg, true);
			nRet = XFEVT_ABORT;
			return;
		}
		string strPath, strLocalPath;
		BOOL bWasServer= okutil_is_group_server(&strPath, NULL, true, &strLocalPath);
		//---- CPY 9/22/07 SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY
		/*
		if(bWasServer)
		{
			int nn = warning_msg_box(XFWARN_CHANGE_GROUP_SERVER, true, 'W', MB_YESNO);
			if(nn == IDNO)
			{
				nRet = XFEVT_ABORT;
				return;
			}
		}
		
		//try {
			//string strMsg;
			//ocu_load_err_msg_str(IDXF_ASK_GROUP_FOLDER, &strMsg);
			//strPath = InputBox(strMsg, strPath);
		//}
		//catch(int nErr)
		//{
			//nRet = XFEVT_ABORT;
			//return;
		//}
		//
		trGetN.path.strVal = strPath;
		trGetN.local.strVal = okutil_get_origin_path(ORIGIN_PATH_GROUP);
		char szPath[MAX_PATH];
		if (okutil_group_server_local_group_path_control(true, szPath))
		{
			if (strPath.CompareNoCase(szPath) == 0)
				trGetN.type.nVal = type_groupserverremote;
			else
				trGetN.type.nVal = type_groupserverlocal;
		}
		else
			trGetN.type.nVal = type_groupclient;
		
		trGetN.local.Show = trGetN.type.nVal == type_groupserverlocal;
		//nRet = XFEVT_PROCEED_NO_DLG;
		*/
		trGetN.path.strVal = strPath;
		trGetN.server.nVal = bWasServer;
		//trGetN.local.strVal = strLocalPath;		///------ Folger 01/21/09 REMOVE_LOCAL_VARIABLE_FROM_GUI_SINCE_INTERNAL_RESOLUTION_IS_ALWAYS_USED
		//---- end SIMPLIFY_GUI_TO_USE_CHECKBOX_ONLY
	}
}
//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 group_server_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
