#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>
enum{
	option_active,
	option_book,
	option_folder,
	option_recursive,
	option_project,
	option_specified,
};
//put additional include files here
#include <../OriginLab/theme_utils.h>
#define PAGE_OPTION_OFFSET			0
//put your own support static functions here

///Folger 06/25/07 INIT_STATIC_VARIABLE
//static int s_nOptionMap[option_specified + 1];
static int s_nOptionMap[] = {
	-1,
	OPTION_ACTIVE_PAGE,
	OPTION_ALL_IN_ACTIVE_FOLDER,
	OPTION_ALL_RECURSIVE_IN_ACTIVE_FOLDER,
	OPTION_ALL_IN_OPJ,
	-1
};
///End INIT_STATIC_VARIABLE

static void _init_theme_list(TreeNode& trTheme)	
{
	init_theme_list(trTheme, THTYPE_WORKSHEET);
	//string strCombo;
	//vector<string> vsThemeNames;
	//trTheme.GetAttribute(STR_COMBO_ATTRIB, strCombo);
	//strCombo.GetTokens(vsThemeNames, '|');
	//strCombo += '|';
	//trTheme.SetAttribute(STR_COMBO_ATTRIB, strCombo);
	//if (vsThemeNames.GetSize())
		//trTheme.strVal = vsThemeNames[0];
	if (trTheme.strVal.IsEmpty())
	{
		string strCombo;
		trTheme.GetAttribute(STR_COMBO_ATTRIB, strCombo);
		if (strCombo.GetNumTokens())
			trTheme.strVal = strCombo.GetToken(0, '|');
	}
}

static bool _gui_apply_theme(LPCSTR lpcszTheme, Worksheet& wks)
{
	string strTheme(lpcszTheme);
	string strSubFolder = "Themes\\Worksheet\\" + strTheme + ".OTH";
	string strFile = GetAppPath(true) + strSubFolder;
	if(!strFile.IsFile())
		strFile = GetAppPath() + strSubFolder;
	//------ Folger 08/07/08 MAKE_APPLY_FORMAT_UNDOABLE
	//return wks.ApplyFormat(strFile);
	Grid grid;
	grid.Attach(wks);
	return grid.ApplyFormat(strFile, TRUE, TRUE, TRUE);
	//------ End MAKE_APPLY_FORMAT_UNDOABLE
}

static string _make_sheet_string_from_pages(const vector<string> &vsPages)
{
	string strTmp;
	int nSize = vsPages.GetSize();
	vector<string> vsSheets;
	for (int ii=0; ii<nSize; ii++)
	{
		Page pg(vsPages[ii]);
		foreach(Layer lay in pg.Layers)
		{
			Worksheet wks(lay);
			wks.GetRangeString(strTmp);
			vsSheets.Add(strTmp);
		}
	}
	strTmp.SetTokens(vsSheets, STR_PAGE_LIST_SEPARATOR);
	return strTmp;
}

static void _refresh_sheets(TreeNode &trGetN)
{
	vector<string> vsPages;
	if (trGetN.option.nVal == option_active)
	{
		Worksheet wks(Project.ActiveLayer());
		if (wks)
		{
			string strTmp;
			wks.GetRangeString(strTmp);
			trGetN.sheets.strVal = strTmp;
		}
	}
	///------ Folger 03/30/10 APPLY_THEME_TO_WKS_SUPPORT_MUTIPLE_SHEETS_FROM_LT_SEPARATED_BY_VERTIVAL_BAR
	//else if (trGetN.option.nVal != option_specified)
	else if (trGetN.option.nVal == option_specified)
	{
		vector<string>		vsSheets;
		trGetN.sheets.strVal.GetTokens(vsPages, trGetN.sheets.strVal.Find(STR_PAGE_LIST_SEPARATOR) > 0 ? STR_PAGE_LIST_SEPARATOR : '|');
		trGetN.sheets.strVal.SetTokens(vsPages, STR_PAGE_LIST_SEPARATOR);
	}
	else
	///------ End APPLY_THEME_TO_WKS_SUPPORT_MUTIPLE_SHEETS_FROM_LT_SEPARATED_BY_VERTIVAL_BAR
	{
		int nWkss = get_folder_pages_name(vsPages, s_nOptionMap[trGetN.option.nVal], PAGE_OPTION_OFFSET, NULL, NULL, EXIST_WKS);
		trGetN.sheets.strVal = _make_sheet_string_from_pages(vsPages);
	}
	trGetN.sheets.strVal.Remove('!');
	trGetN.sheets.Enable = trGetN.option.nVal == option_specified;
}


void themeApply2w(const string& theme, const int& option, const string& sheets)
{
	vector<string> vsSheets;
	sheets.GetTokens(vsSheets, STR_PAGE_LIST_SEPARATOR);
	int nSize = vsSheets.GetSize();
	for (int ii=0; ii<nSize; ii++)
	{
		Worksheet wks(vsSheets[ii]);
		if (wks)
		{
			_gui_apply_theme(theme, wks);
		}
	}
}

//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 themeApply2w_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return themeApply2w_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	///Folger 06/21/07 REFRESH_SHEETS_WHEN_THEME_CHANGE
	if (nEventID == GETNE_ON_THEME)
	{
		_init_theme_list(trGetN.theme);
		_refresh_sheets(trGetN);
	}
	///End REFRESH_GRAPHS_WHEN_THEME_CHANGE
	
	if (nEventID > 0)
	{
		if (!lstrcmpi(lpcszNodeName, "option"))
		{
			_refresh_sheets(trGetN);			
		}
		else if (!lstrcmpi(lpcszNodeName, "sheets"))
		{
			vector<string> vsNames;
			trGetN.sheets.strVal.GetTokens(vsNames, STR_PAGE_LIST_SEPARATOR);
			for (int ii=0; ii<vsNames.GetSize(); ii++)
			{
				Worksheet wks(vsNames[ii]);
				if (!wks)
				{
					vsNames.RemoveAt(ii--);
				}
			}
			trGetN.sheets.strVal.SetTokens(vsNames, STR_PAGE_LIST_SEPARATOR);
		}
	}
	
	if (trGetN.sheets.strVal.IsEmpty())
	{
		///Cheney 2007-7-2 UPDATE_XF_ERR_THROW
		//strErrMsg = CER_EMPTY_VARIABLE;
		//strErrMsg += ":" + _L("Worksheets");
		strErrMsg = CER_NO_WORKSHEET;
		///end UPDATE_XF_ERR_THROW
		bOKEnable = false;
		return false;
	}
	return false;

}

int themeApply2w_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 themeApply2w_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{
	if (nGetNDialog >= 0)
	{
		///Folger 06/25/07 INIT_STATIC_VARIABLE
		//Initial Map
		//s_nOptionMap[option_active] = -1;
		//s_nOptionMap[option_SheetsInBook] = OPTION_ACTIVE_PAGE;
		//s_nOptionMap[option_SheetsInFld] = OPTION_ALL_IN_ACTIVE_FOLDER;
		//s_nOptionMap[option_SheetInFldRe] = OPTION_ALL_RECURSIVE_IN_ACTIVE_FOLDER;
		//s_nOptionMap[option_SheetInOpj] = OPTION_ALL_IN_OPJ;
		//s_nOptionMap[option_specified] = -1;
		///End INIT_STATIC_VARIABLE
		//Initial theme,sheets
		if (nGetNDialog == 1)
			_init_theme_list(trGetN.theme);
		_refresh_sheets(trGetN);
	}
}
//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 themeApply2w_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
