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


#include <XFMenu.h>

/// Max 12/25/07 COMMENT_UNUSED_CODES
/*
enum {
	IDM_SHOW_TEXT_SLOPE = 1,// must start with a >0 value, 
	IDM_SHOW_TEXT_N,
	IDM_SHOW_TEXT_INTERCEPT,
};


class FitLinearXFMenu : public XFMenu
{
public:
	FitLinearXFMenu(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("Show with tool: Sample Size (N)", IDM_SHOW_TEXT_N, nFlags);
			nFlags = m_tr.Settings.ShowLabelSlope.nVal? MF_CHECKED : MF_UNCHECKED;
			Add("Show with tool: Slope (B)", IDM_SHOW_TEXT_SLOPE, nFlags);
			nFlags = m_tr.Settings.ShowLabelIntercept.nVal? MF_CHECKED : MF_UNCHECKED;
			Add("Show with tool: Intercept (A)", IDM_SHOW_TEXT_INTERCEPT, nFlags);
		EndPopup();
		AddSeparator();
		XFMenu::ConstructOnMoveOptions();
		AddSeparator();
		XFMenu::ConstructDataSelRectOptions();
		AddSeparator();
		XFMenu::ConstructOperationCommands();
	}
	//virtual 
	void InitDefaults(TreeNode& tr)
	{
  		XFMenu::InitDefaults(tr);
		tr.Settings.ShowLabelN.nVal = 1; // data
		tr.Settings.ShowLabelSlope.nVal = 1;
  		tr.Settings.ShowLabelIntercept.nVal = 1;
	}
	//virtual
	string GetOperationClassName() {return "FitLinear";}
	//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_SLOPE:
			m_tr.Settings.ShowLabelSlope.nVal = m_tr.Settings.ShowLabelSlope.nVal? 0:1;
			return false;// indicate need for further processing, to update the display			
		case IDM_SHOW_TEXT_INTERCEPT:
			m_tr.Settings.ShowLabelIntercept.nVal = m_tr.Settings.ShowLabelIntercept.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.ShowLabelSlope.nVal)
		str += "Slope = " + ftoa(trResults.b.dVal) + "\r\n";
	if(tr.Settings.ShowLabelIntercept.nVal)
		str += "Intercept = " + ftoa(trResults.a.dVal);
	return str.GetLength() > 0? true:false;
}

static bool _fit_linear(const DataRange& dr, TreeNode& tr)
{
	DWORD dwPlotID;
	vector vy, vx;
	if(dr.GetData(DRR_GET_MISSING |  DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vy, &vx) < 0)
		return false;
	vector vcoeff;
	if(fitlinear(vx, vy, vcoeff))
	{
		double x1, x2;
		vx.GetMinMax(x1, x2);
		tr.SetAttribute(STR_LABEL_ATTRIB, "y = A + B x");
		tr.Data.strVal = dr.GetDescription();
		tr.Data.TypeID = TNVAL_TYPE_CSTRING; tree_set_add_col_info(tr.Data, OKDATAOBJ_DESIGNATION_X, 24);
		tr.N.nVal = vy.GetSize();
		tr.x1.dVal = x1;
		tr.x2.dVal = x2;
		tr.a.dVal = vcoeff[0];
		tr.b.dVal = vcoeff[1];
		tr.aErr.dVal = vcoeff[2];
		tr.bErr.dVal = vcoeff[3];
		return true;
	}
	return false;
}
*/
/// END COMMENT_UNUSED_CODES	

void FitLinear(EventInfo& ei, LineInfo& ln, ButtonInfo& biMenu, TextInfo& tx, const int& option)
{
	/// Max 12/25/07 COMMENT_UNUSED_CODES
	/*
	FitLinearXFMenu menu(biMenu);
	DataRange dr;
	int nMenuCmd = 0;
	bool bFoundData = menu.ProcessEvents(ei, biMenu, dr, nMenuCmd, 2, ln, tx);
	if(nMenuCmd < 0)
		return;
	Tree trResults;
	if(bFoundData && _fit_linear(dr, trResults))
	{
		double x1 = trResults.x1.dVal;
		double x2 = trResults.x2.dVal;
		ln.MoveTo(x1, trResults.a.dVal + trResults.b.dVal * x1);
		ln.LineTo(x2, trResults.a.dVal + trResults.b.dVal * x2);
		TreeNode tr = biMenu.GetUserData();
		string strLabel;
		if(_make_display_label(strLabel, trResults, tr))
		{
			tx.SetText(strLabel);
		}
		else
		{
			tx.SetText("");
		}
	}
	else
		bFoundData = false;
	
	ln.SetShow(bFoundData);
	tx.SetShow(bFoundData);
		
	if(bFoundData)
		menu.QuickOutput(nMenuCmd, trResults, "FitLinear");
	*/
	/// END COMMENT_UNUSED_CODES
}

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

}
