#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
//put additional include files here

//put your own support static functions here
///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
//#define STR_C_W_D "current working directory"
///end UPDATE_XF_ERR_THROW
#define CDSET_INI_SECTION "cdset"

///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
static bool _is_path_unassigned(LPCSTR lpcszPath)
{
	string strPath(lpcszPath);
	if(strPath.IsEmpty() || okutil_cvt_str_to_predefined_type(strPath) == PDS_UNASSIGNED)
		return true;
	
	return false;
}
///end UPDATE_XF_ERR_THROW



void mkdir(const string& dir)
{
	
	///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
	string strMsg;  
	string strCWD;
	ocu_load_msg_str(XF_SYSTEM_CURRENT_WORKING_FOLDER, &strCWD);
	///end UPDATE_XF_ERR_THROW
	
	///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
	//string workingfolder;
	//int nn = okutil_cvt_str_to_predefined_type(dir);
	//if(PDS_USER == nn)
	//{
		////-- user entered path
		//string strNewpathResolved;
		//okutil_resolve_str_expression(dir , &strNewpathResolved);
		//
		//// If user enters relative path, create full path relative to custom path.
		//// otherwise IsPath() will examine it relative to another choice of cwd 
		//if(!strNewpathResolved.IsEmpty() && ( 1!=strNewpathResolved.Find(":\\") || 0!=strNewpathResolved.Find("\\\\") ) )
		//{
			//if(0<=strNewpathResolved.Find("\\"))
			//{
				/////Cheney 2007-7-2 UPDATE_XF_ERR_THROW
				////printf("md can create only one directory level at a time.\n", strNewpathResolved);//Better to call COKUPath strPath.CreateDirectory()	to allow multiple levels to be created.
				//warning_msg_box(XF_MKDIR_CREATE_ONE_DIR_AT_ONE_TIME, false, 'E');
				/////end UPDATE_XF_ERR_THROW
				//return;
			//}
			//else
			//{
				//workingfolder = okutil_get_custom_path();
				//workingfolder=workingfolder+strNewpathResolved;
				//strNewpathResolved = workingfolder;
			//}
			//
		//}
	//
		//if(strNewpathResolved.IsPath())
		//{	
			/////Cheney 2007-7-2 UPDATE_XF_ERR_THROW
			////printf("%s already exists.\n", strNewpathResolved);
			//warning_msg_box(XF_MKDIR_ALREADY_EXISTS, strNewpathResolved, false, 'E');
			/////end UPDATE_XF_ERR_THROW
		//}
		//else
		//{
			//if(!strNewpathResolved.IsEmpty())
			//{
				//if( 0<CreateDirectory( strNewpathResolved , NULL) )//Better to call COKUPath strPath.CreateDirectory()	to allow multiple levels to be created.
					/////Cheney 2007-7-2 UPDATE_XF_ERR_THROW
					////printf("%s has been created.\n", strNewpathResolved);
					//warning_msg_box(XF_MKDIR_HAS_BEEN_CREATE, strNewpathResolved, false, 'R');
					/////end UPDATE_XF_ERR_THROW
				//else
					/////Cheney 2007-7-2 UPDATE_XF_ERR_THROW
					////printf("%s has not been created.\n", strNewpathResolved);
					//warning_msg_box(XF_MKDIR_HAS_NOT_CREATE, strNewpathResolved, false, 'R');
					/////end UPDATE_XF_ERR_THROW
			//}
			//
		//}
	//}
	//else // user called with default <unassigned> to get current settings
	//{
		/////Cheney 2007-7-2 UPDATE_XF_ERR_THROW
		////printf("%s", STR_C_W_D, workingfolder);
		//strMsg.Format("%s\r\n%s", strCWD, workingfolder);
		//warning_msg_box(strMsg, false, 'R');
		/////end UPDATE_XF_ERR_THROW
	//}
	
	string workingfolder;
	if( _is_path_unassigned(dir) ) // user called with default <unassigned> to get current settings
	{
		workingfolder = okutil_get_custom_path(true);// true means update map 
		strMsg.Format("%s\r\n%s", strCWD, workingfolder);
		warning_msg_box(strMsg, false, 'R');
		return;
	}
	
	string strNewpathResolved;
	okutil_resolve_str_expression(dir , &strNewpathResolved);
	
	// If user enters relative path, create full path relative to custom path.
	// otherwise IsPath() will examine it relative to another choice of cwd 
	if(!strNewpathResolved.IsEmpty() && ( 1!=strNewpathResolved.Find(":\\") || 0!=strNewpathResolved.Find("\\\\") ) )
	{
		if(0<=strNewpathResolved.Find("\\"))
		{
			///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
			//warning_msg_box(XF_MKDIR_CREATE_ONE_DIR_AT_ONE_TIME, false, 'E');
			XF_THROW(XF_MKDIR_CREATE_ONE_DIR_AT_ONE_TIME);
			///-----End CLEANUP_XFS_ERROR_CODE
			return;
		}
		else
		{
			workingfolder = okutil_get_custom_path();
			workingfolder=workingfolder+strNewpathResolved;
			strNewpathResolved = workingfolder;
		}
		
	}
	
	if(strNewpathResolved.IsPath())
	{	
		///-----Kit 10/22/2010 CLEANUP_XFS_ERROR_CODE
		//warning_msg_box(XF_MKDIR_ALREADY_EXISTS, strNewpathResolved, false, 'E');
		XF_THROW_EX(XF_MKDIR_ALREADY_EXISTS, strNewpathResolved);
		///-----End CLEANUP_XFS_ERROR_CODE
	}
	else
	{
		if(!strNewpathResolved.IsEmpty())
		{
			if( 0<CreateDirectory( strNewpathResolved , NULL) )//Better to call COKUPath strPath.CreateDirectory()	to allow multiple levels to be created.
				warning_msg_box(XF_MKDIR_HAS_BEEN_CREATE, strNewpathResolved, false, 'R');
			else
				warning_msg_box(XF_MKDIR_HAS_NOT_CREATE, strNewpathResolved, false, 'R');
		}
		
	}
	
}

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

}

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