#include "sierrachart.h"
SCDLLName("ChartDrawingTest")

/*==================================================================================*/
void DrawACSRectangle(SCStudyInterfaceRef sc, const int32_t barIndex)
{
	auto rectangle = s_UseTool();

	rectangle.ChartNumber = sc.ChartNumber;
	rectangle.Region = sc.GraphRegion;
	rectangle.AddMethod = UTAM_ADD_ALWAYS;

	rectangle.BeginValue = sc.Low[barIndex];
	rectangle.EndValue = sc.High[barIndex];
	
	rectangle.BeginIndex = barIndex;
	rectangle.EndIndex = barIndex + 10;

	rectangle.DrawUnderneathMainGraph = sc.DrawStudyUnderneathMainPriceGraph;

	rectangle.DrawingType = DRAWING_RECTANGLEHIGHLIGHT;
	rectangle.Color = COLOR_BLUE;
	rectangle.SecondaryColor = COLOR_RED;
	
	rectangle.LineStyle = LINESTYLE_SOLID;
	rectangle.LineWidth = 1;
	rectangle.TransparencyLevel = sc.TransparencyLevel;
	
	sc.UseTool(rectangle);
}

/*==================================================================================*/
void DrawACSLine(SCStudyInterfaceRef sc, const int32_t barIndex)
{
	auto line = s_UseTool();

	line.ChartNumber = sc.ChartNumber;
	line.Region = sc.GraphRegion;
	line.AddMethod = UTAM_ADD_ALWAYS;

	line.BeginValue = sc.High[barIndex];
	line.EndValue = sc.High[barIndex];

	line.BeginIndex = barIndex;
	line.EndIndex = barIndex + 10;

	line.DrawUnderneathMainGraph = sc.DrawStudyUnderneathMainPriceGraph;

	line.DrawingType = DRAWING_LINE;
	line.Color = COLOR_BLUE;

	line.LineStyle = LINESTYLE_SOLID;
	line.LineWidth = 1;

	sc.UseTool(line);
}

/*==================================================================================*/
void DrawACSText(SCStudyInterfaceRef sc, const int32_t barIndex)
{
	auto text = s_UseTool();

	text.ChartNumber = sc.ChartNumber;
	text.Region = sc.GraphRegion;
	text.AddMethod = UTAM_ADD_ALWAYS;

	text.BeginValue = sc.High[barIndex] + 1.0f;

	text.BeginIndex = barIndex;

	text.DrawUnderneathMainGraph = sc.DrawStudyUnderneathMainPriceGraph;

	text.DrawingType = DRAWING_TEXT;
	text.ReverseTextColor = 0;
	text.TextAlignment = DT_VCENTER | DT_CENTER;
	text.Color = COLOR_BLUE;
	text.FontSize = 8;
	text.FontBold = 1;
	text.Text = "TESTING";

	sc.UseTool(text);
}

/*==================================================================================*/
SCSFExport scsf_ChartDrawingTest(SCStudyInterfaceRef sc)
{
	auto& in_MaxDrawings = sc.Input[0];
	auto& in_DrawingFrequency = sc.Input[1];
	auto& in_DrawingType = sc.Input[2];

	if (sc.SetDefaults)
	{
		sc.GraphName = "ACS Chart Drawing Test";
		sc.GraphRegion = 0;
		sc.AutoLoop = 0;
		sc.ValueFormat = VALUEFORMAT_INHERITED;
		sc.GlobalDisplayStudySubgraphsNameAndValue = 0;
		sc.DisplayStudyInputValues = 0;
		sc.HideDLLAndFunctionNames = 1;

		in_MaxDrawings.Name = "Max Drawing Count";
		in_MaxDrawings.SetInt(1000);
		in_MaxDrawings.SetIntLimits(1, 50000);

		in_DrawingFrequency.Name = "Bars Between Drawings";
		in_DrawingFrequency.SetInt(10);
		in_DrawingFrequency.SetIntLimits(5, 50);

		in_DrawingType.Name = "Drawing Type";
		in_DrawingType.SetCustomInputStrings("Rectangle;Line;Text");
		in_DrawingType.SetCustomInputIndex(0);

		return;
	}

	for (auto barIndex = sc.UpdateStartIndex; barIndex < sc.ArraySize; barIndex++)
	{
		if (barIndex % in_DrawingFrequency.GetInt() != 0 || sc.HideStudy)
			continue;

		if (sc.GetACSDrawingsCount(sc.ChartNumber, 1) >= in_MaxDrawings.GetInt())
			break;

		switch (in_DrawingType.GetIndex())
		{
		case 0:
			DrawACSRectangle(sc, barIndex);
			break;
		case 1:
			DrawACSLine(sc, barIndex);
			break;
		case 2:
			DrawACSText(sc, barIndex);
			break;
		}
	}

	const auto hidden = sc.HideStudy ? "Hidden" : "Visible";

	auto msg = SCString();
	msg.AppendFormat("Study ID: %d is %s | ", sc.StudyGraphInstanceID, hidden);
	msg.AppendFormat("Drawings Added by Study: %d | ", sc.GetACSDrawingsCount(sc.ChartNumber, 1));
	msg.AppendFormat("Total Drawings for All Studies: %d | ", sc.GetACSDrawingsCount(sc.ChartNumber, 0));
	msg.AppendFormat("Last Calculation Time: %d", sc.LastFullCalculationTimeInMicroseconds);

	sc.AddMessageToLog(msg, 1);
}

/*==================================================================================*/

