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

//put your own support static functions here

static		int		_get_template_file_type(LPCSTR lpcsz)
{
	string                strExt;
	separate_file_name_ext(lpcsz, NULL, &strExt);
	
	if ( strExt.CompareNoCase("OGW") == 0 || strExt.CompareNoCase("OTW") == 0 )
		return EXIST_WKS;
	if ( strExt.CompareNoCase("OGM") == 0 || strExt.CompareNoCase("OTM") == 0)
		return EXIST_MATRIX;
	
	return -1;
}

static		int		_get_template_file_type_and_window_applicable_checking(const Page& pg, LPCSTR lpcszFile, string& strErr)
{
	if ( !pg )
	{
		strErr = CER_INVALID_PAGE;
		return -1;
	}
	
	int		nType = -1;
	if ( (nType = _get_template_file_type(lpcszFile)) < 0 )
	{
		strErr = XFERR_INVALID_TEMPLATE_FILE_TYPE;
		return -1;
	}
	
	if ( pg.GetType() != nType )
	{
		strErr = XFERR_TEMPLATE_NOT_APPLICABLE;
		return -1;
	}
	
	return nType;
}

class TempPageDestroyHelper
{
public:
	TempPageDestroyHelper(Page& pg)
	{
		m_pg = pg;
	}
	~TempPageDestroyHelper()
	{
		if ( m_pg )
			m_pg.Destroy();
	}
	
private:
	
	Page		m_pg;
};


void addsheet(const Page& book, const string& fname, const int& index, const int& active, string& oname)
{
	
	int		nType = -1;
	string	strErr;
	if ( (nType = _get_template_file_type_and_window_applicable_checking(book, fname, strErr)) < 0 )
		XF_THROW(strErr);	
	
	DWORD	dwCreate = CREATE_KEEP_LAYER_NAMES | CREATE_HIDDEN;
	Page	pgTemp;
	
	if ( EXIST_WKS == nType )
	{
		WorksheetPage	pgTempWks;
		if ( !pgTempWks.Create(fname, dwCreate) )
			XF_THROW(XFERR_FAILS_TO_LOAD_TEMPLATE);
		pgTemp = pgTempWks;
	}
	else
	{
		MatrixPage		pgTempMat;
		if ( !pgTempMat.Create(fname, dwCreate) )
			XF_THROW(XFERR_FAILS_TO_LOAD_TEMPLATE);
		pgTemp = pgTempMat;
	}
	
	TempPageDestroyHelper		clHelper(pgTemp);		/// for safely temp page destroy
	
	ASSERT(book);
	
	int		nLayerIndex = -1;
	
	if ( 0 == index )
	{
		nLayerIndex = book.Layers.Count();
		book.AddLayers(pgTemp);
	}
	else
	{
		--index;
		if ( index < 0 || index >= pgTemp.Layers.Count()  )
			XF_THROW(XFERR_LAYER_INDEX_OUT_OF_RANGE);
		
		Layer lay = pgTemp.Layers(index);
		nLayerIndex = book.AddLayer(lay);
	}
	
	if ( nLayerIndex < 0 )
		XF_THROW(XFERR_NEWSHT_CREATE_FAILED);
	
	if ( oname )
	{
		oname = book.Layers(nLayerIndex).GetName();
	}
	
	if ( active )
	{
		page_set_active_layer(book, nLayerIndex, 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 addsheet_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return addsheet_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	if ( GETNE_ON_INIT == nEventID
		|| lstrcmpi(lpcszNodeName, "book") == 0
		|| lstrcmpi(lpcszNodeName, "fname") == 0
		)
	{
		int		nType = -1;
		string	strBook, strSheet;
		okutil_get_book_sheet_info(&strBook, &strSheet, trGetN.book.strVal);
		Page	pg(strBook);
		if ( (nType = _get_template_file_type_and_window_applicable_checking(pg, trGetN.fname.strVal, strErrMsg)) < 0 )
		{
			bOKEnable = false;
			return true;
		}
	}
	
	return false;

}

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