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

enum {
	IDM_SHOW_TEXT_MEAN = 1,// must start with a >0 value, 
	IDM_SHOW_TEXT_N,
	IDM_SHOW_TEXT_SD,
};

class DescStatsXFMenu : public XFMenu
{
public:
	DescStatsXFMenu(ButtonInfo& biMenu) : XFMenu(biMenu)
	{
	}
protected:
	//virtual
	void Construct()
	{
		XFMenu::ConstructQuickOutputCommands();
		AddSeparator();
		BeginPopup("Show with tool");
			int nFlags;		
			nFlags = m_tr.Settings.ShowLabelN.nVal? MF_CHECKED : MF_UNCHECKED;
			Add("Sample Size (N)", IDM_SHOW_TEXT_N, nFlags);
			nFlags = m_tr.Settings.ShowLabelMean.nVal? MF_CHECKED : MF_UNCHECKED;
			Add("Average (Mean)", IDM_SHOW_TEXT_MEAN, nFlags);
			nFlags = m_tr.Settings.ShowLabelSD.nVal? MF_CHECKED : MF_UNCHECKED;
			Add("Standard Deviation (SD)", IDM_SHOW_TEXT_SD, nFlags);
		EndPopup();
		AddSeparator();
		XFMenu::ConstructDataSelRectOptions();
	}
	//virtual 
	void InitDefaults(TreeNode& tr)
	{
  		XFMenu::InitDefaults(tr);
		tr.Settings.ShowLabelN.nVal = 1; // data
		tr.Settings.ShowLabelMean.nVal = 1;
  		tr.Settings.ShowLabelSD.nVal = 1;
	}
	//virtual 
	bool DoCommand(int nCmdID)
	{
		int nn;
		switch(nCmdID)
		{
		case IDM_SHOW_TEXT_N:
			m_tr.Settings.ShowLabelN.nVal = m_tr.Settings.ShowLabelN.nVal? 0:1;
			return false;// indicate need for further processing, to update the display
		case IDM_SHOW_TEXT_MEAN:
			m_tr.Settings.ShowLabelMean.nVal = m_tr.Settings.ShowLabelMean.nVal? 0:1;
			return false;// indicate need for further processing, to update the display			
		case IDM_SHOW_TEXT_SD:
			m_tr.Settings.ShowLabelSD.nVal = m_tr.Settings.ShowLabelSD.nVal? 0:1;
			return false;// indicate need for further processing, to update the display
		}
		return XFMenu::DoCommand(nCmdID);
	}
};

static bool _make_display_label(string& str, const TreeNode& trResults, const TreeNode& tr)
{
	str.Empty();
	if(tr.Settings.ShowLabelN.nVal)
		str += "N = " + trResults.N.nVal + "\r\n";
	if(tr.Settings.ShowLabelMean.nVal)
		str += "Mean = " + ftoa(trResults.mean.dVal) + "\r\n";
	if(tr.Settings.ShowLabelSD.nVal)
		str += "SD = " + ftoa(trResults.sd.dVal);
	return str.GetLength() > 0? true:false;
}
			

void DescStats(EventInfo& ei, LineInfo& bln, const ButtonInfo& biMenu, TextInfo& tx, const int& option)
{
	DescStatsXFMenu menu(biMenu);
	int nMenuCmd = 0;
	DataRange dr;
	bool bFoundData = menu.ProcessEvents(ei, biMenu, dr, nMenuCmd, 1, bln, tx);
	if(nMenuCmd < 0)
		return;
	Tree trResults;
	if(bFoundData && curve_stats(dr, trResults))
	{
		double dLeft, dTop, dRight, dBottom;
		bln.GetParentCorner(0, dLeft, dTop);
		bln.GetParentCorner(2, dRight, dBottom);
		bln.MoveTo(dLeft, trResults.mean.dVal);
		bln.LineTo(dRight, trResults.mean.dVal);
		TreeNode tr = biMenu.GetUserData();
		string strLabel;
		if(_make_display_label(strLabel, trResults, tr))
		{
			tx.SetText(strLabel);
		}
	}
	else
		bFoundData = false;
	
	bln.SetShow(bFoundData);
	tx.SetShow(bFoundData);
	
	if(bFoundData)
		menu.QuickOutput(nMenuCmd, trResults, "DescStats");
	
}

//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 DescStats_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
