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

//modification log
// CPY 6/13/07 NEED_TO_USE_BETTER_NAME_FOR_UNIQUE_1ST_LETTER
// CPY 8/9/2007 MATRIX_SHEET, col->cols, result->outname, added rows, mat
// Folger 01/16/09 QA80-12977 PE_MKDIR_FAILS_TO_ACTIVE_FOLDER_DUE_TO_NEWSHEET_ACTIVE_LAYER


void newsheet(const Page& book, const string& name, const string& labels, const int& cols, const int& rows, const string& xy, const string& template, const int& use, const string& sheet, string& outname, const int& mat, const int& active)
{
	
	// if c < 0, we are going to keep the number of cols from default of loading the sheet
	//------ CPY 8/9/2007 MATRIX_SHEET
	//	WorksheetPage wkbk = book;
	//	if(!wkbk)
	//		XF_THROW(XFERR_NEWSHT_BOOK_INVALID);
	//Worksheet wks;
	if(mat)
	{
		MatrixPage mbk = book;
		
		if(!mbk)
			//XF_THROW(XFERR_NEWSHT_BOOK_INVALID);
			XF_THROW(XFERR_NEWSHT_MBOOK_INVALID); /// Hong 12/14/07 v8.0768 MORE_PRECISE_ERROR_MSG
	}
	else
	{
		WorksheetPage wkbk = book;
		
		if(!wkbk)
			XF_THROW(XFERR_NEWSHT_BOOK_INVALID);
	}
	Datasheet ds;
	//-------
	if(use)
	{
		ds = book.Layers(name);
	}
	int	nLayerIndex = -1;
	bool bReuseExisting = false;
	if(ds) // already existed and we want to use it
	{
		nLayerIndex = ds.GetIndex();
		bReuseExisting = true;
	}
	else
	{
		//------ CPY 8/9/2007 MATRIX_SHEET
		//	WorksheetPage wkbk = book;
		//	nLayerIndex = wkbk.AddLayer("", CREATE_UNDOABLE, template, 0, sheet);
		//	wks = wkbk.Layers(nLayerIndex);
		if(mat)
		{
			MatrixPage mbk = book;
			nLayerIndex = mbk.AddLayer("", CREATE_UNDOABLE, template);
			ds = mbk.Layers(nLayerIndex);
		}
		else
		{
			WorksheetPage wkbk = book;
			nLayerIndex = wkbk.AddLayer("", CREATE_UNDOABLE, template, 0, sheet);
			ds = wkbk.Layers(nLayerIndex);
		}
		//--------
	}
	if(!ds)
		XF_THROW(XFERR_NEWSHT_CREATE_FAILED);
	
	if(!bReuseExisting && name.GetLength() > 0)
		ds.SetName(name, OCD_ENUM_NEXT);
	
	int nColsFromLabels = 0;
	int nCols = cols;
	int nRows = rows;//---- CPY 8/9/2007 MATRIX_SHEET
	vector<string> vsColLabels;
	if(labels && !labels.IsEmpty())
	{
		nColsFromLabels = labels.GetTokens(vsColLabels, '|');
		if(nColsFromLabels > nCols)
			nCols = nColsFromLabels;
	}
	//------ CPY 8/9/2007 MATRIX_SHEET
	if(mat)
	{
		MatrixLayer ml = ds;
		if(nCols >= 0)
		{
			ml.SetSize(1, nRows, nCols);
		}
	}
	else
	{
		Worksheet wks = ds;
		if(nCols >= 0)
		{
			wks.SetSize(nRows, nCols);
			if(nColsFromLabels > 0)
			{
				for(int ii = 0; ii < nColsFromLabels; ii++)
				{
					Column cc(wks, ii);
					cc.SetLongName(vsColLabels[ii]);
				}
			}
		}
		if(xy && !xy.IsEmpty())
			wks.SetColDesignations(xy, true);
	}
	//------ Folger 02/25/08 QA80-11148 ADD_ACTIVE_NEW_SHEET_OPTION
	if ( active )
	//------
		///------ Folger 01/16/09 QA80-12977 PE_MKDIR_FAILS_TO_ACTIVE_FOLDER_DUE_TO_NEWSHEET_ACTIVE_LAYER
		//page_set_active_layer(book, nLayerIndex, false);
		page_set_active_layer(book, nLayerIndex, true);
		///------ End PE_MKDIR_FAILS_TO_ACTIVE_FOLDER_DUE_TO_NEWSHEET_ACTIVE_LAYER
	//------ CPY 8/9/2007 MATRIX_SHEET
	//result = make_book_sheet_name(wkbk.GetName(), wks.GetName());
	//result+="!";// so result is a full range string
	ds.GetRangeString(outname);
	//------
	
}

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

}

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