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

//put your own support static functions here

///------ Folger 02/26/10 QA81-14568-P2 CHANGE_UFF_BETTER_ERROR_CHECKING
static	BOOL	IS_FILE_PATH_INVALID(LPCSTR lpcszPath, string* pstrErrMsg)
{
	return !CheckUFF(lpcszPath, TRUE, FALSE, pstrErrMsg);
}
///------ End CHANGE_UFF_BETTER_ERROR_CHECKING

BOOL	is_reset_uf(int nEventID)
{
	return GETNE_ON_CUSTOM_BUTTON1 == nEventID;
}

void	init_uff_path(TreeNode& trGetN)
{
	trGetN.path.strVal = okutil_get_origin_path(ORIGIN_PATH_USER);
}


void changeUFF(const string& path, const int& copy, const string& hint)
{
	string		strErrMsg;
	if ( IS_FILE_PATH_INVALID(path, &strErrMsg) )
		XF_THROW(strErrMsg);
		
	string		strPath = path;
	check_add_ending_char(strPath, '\\');
	string		strPathOriginal = okutil_get_origin_path(ORIGIN_PATH_USER);
	if ( 0 == strPath.CompareNoCase(strPathOriginal) )
		return;
	
	if ( !strPath.IsPath() && !CheckMakePath(strPath) )
		XF_THROW(CER_INVALID_FILEPATH);
	
	Registry	reg(HKEY_CURRENT_USER);
	reg.SetValue(GetRegKey(), "path", strPath);
	
	if ( copy )
	{
		progressBox		pb(_L("Files copying, please wait ..."), PBOX_NO_BUTTON | PBOX_NO_BAR);
		pb.SetText(_L("Copy Files"));
		copy_directory(strPathOriginal, strPath, TRUE);
	}
	else
	{
		DWORD	dwVal = 1;
		okutil_uff_changed_registry_access(UFF_CHANGE_REGISTRY_SET, &dwVal);
		
		//vector<string>	vsFilesToCopy = {"origin.ini"
			//};
		//for ( int ii=0; ii<vsFilesToCopy.GetSize(); ++ii )
		//{
			//copyfile(strPathOriginal + vsFilesToCopy[ii], strPath + vsFilesToCopy[ii]);
		//}
	}
	
	warning_msg_box(XFWARN_RESTART_ORIGIN_TO_USE_NEW_UFF, true);
}

//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 changeUFF_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return changeUFF_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if ( GETNE_ON_INIT == nEventID )
	{
		init_uff_path(trGetN);
		return false;
	}
	
	if ( is_reset_uf(nEventID) )
	{
		init_uff_path(trGetN);
		strAux = DLG_NEED_CHANGE_GRID;
		return true;
	}
	
	if ( IS_FILE_PATH_INVALID(trGetN.path.strVal, &strErrMsg) )
	{
		bOKEnable = false;
	}
	///Kyle 10/09/2009 PREVENT_COPYING_DIRECTORY_TO_ITS_SUB_FOLDER
	else if(trGetN.copy.nVal)
	{
		string		strPathOriginal = okutil_get_origin_path(ORIGIN_PATH_USER);
		string strPath = trGetN.path.strVal;
		check_add_ending_char(strPath, '\\');
		if(strPathOriginal.CompareNoCase(strPath))
		{
			strPath = strPath.Left(strPathOriginal.GetLength());
			if(strPathOriginal.CompareNoCase(strPath) == 0)
				strErrMsg = "Files copy can not proceed when destination folder is a subfolder of the source folder.";
		}
	}
	///End PREVENT_COPYING_DIRECTORY_TO_ITS_SUB_FOLDER
	
	return true;

}

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