// The top of every source code file must include this line
#include "sierrachart.h"
#include "../BruSoftwareScTrading/BruTrade.h"

// For reference, refer to this page:
// https://www.sierrachart.com/index.php?page=doc/AdvancedCustomStudyInterfaceAndLanguage.php

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies. 
//SCDLLName("Custom Study DLL")

//This is the basic framework of a study function. Change the name 'TemplateFunction' to what you require.
SCSFExport scsf_ToolPerformance(SCStudyInterfaceRef sc)
{
	SCSubgraphRef TextLabels = sc.Subgraph[0];
	SCInputRef DrawMode = sc.Input[0];
	int& toolLineNumber = sc.GetPersistentInt(1);

	// Section 1 - Set the configuration variables and defaults
	if (sc.SetDefaults)
	{
		sc.GraphName = "ToolPerformance";
		sc.GraphRegion = 0;
		
		// During development set this flag to 1, so the DLL can be rebuilt without restarting Sierra Chart. When development is completed, set it to 0 to improve performance.
		sc.FreeDLL = 0;

		sc.AutoLoop = 1;  //Automatic looping is enabled. 
		
		sc.Subgraph[0].Name = "Name";
		sc.Subgraph[0].DrawStyle = DRAWSTYLE_LINE;
		sc.Subgraph[0].PrimaryColor = RGB (0, 255, 0);
		
		DrawMode.Name = "DrawMode";
		DrawMode.SetCustomInputStrings("UTAM_ADD_ALWAYS;UTAM_ADD_ONLY_IF_NEW;UTAM_ADD_OR_ADJUST;ADD.ALWAYS.THEN.DELETE");
		DrawMode.SetCustomInputIndex(0);

		TextLabels.Name = "Text Labels";
		TextLabels.PrimaryColor = COLOR_CYAN;
		TextLabels.LineWidth = 8;
		TextLabels.DrawStyle = DRAWSTYLE_CUSTOM_TEXT;

		return;
	}
	if (sc.HideStudy)
	{
		return;
	}
	if (sc.Index == 0)
	{
		switch (DrawMode.GetIndex())
		{
		case 0:
			sc.GraphName = "ToolPerformance UTAM_ADD_ALWAYS";
			break;
		case 1:
			sc.GraphName = "ToolPerformance UTAM_ADD_ONLY_IF_NEW";
			break;
		case 2:
			sc.GraphName = "ToolPerformance UTAM_ADD_OR_ADJUST";
			break;
		case 3:
			sc.GraphName = "ToolPerformance ADD.ALWAYS.THEN.DELETE";
			break;
		default:;
		}
		SCString message;
		message.Format("ArraySize=%d", sc.ArraySize);
		sc.AddMessageToLog(message, 1);
	}
	
	s_UseTool Tool;
	Tool.ChartNumber = sc.ChartNumber;
	Tool.Region = sc.GraphRegion;
	Tool.DrawingType = DRAWING_TEXT;
	Tool.ReverseTextColor = 0;
	Tool.BeginIndex = sc.Index;
	Tool.BeginValue = sc.High[sc.Index];
	Tool.Color = TextLabels.PrimaryColor;
	Tool.TextAlignment = DT_CENTER | DT_BOTTOM;
	Tool.FontSize = TextLabels.LineWidth;
	Tool.FontBold = true;
	Tool.Text = "X";
	switch (DrawMode.GetIndex())
	{
	case 0:
		Tool.AddMethod = UTAM_ADD_ALWAYS;
		break;
	case 1:
		Tool.AddMethod = UTAM_ADD_ONLY_IF_NEW;
		break;
	case 2:
		Tool.AddMethod = UTAM_ADD_OR_ADJUST;
		break;
	case 3:
		Tool.AddMethod = UTAM_ADD_ALWAYS;
		break;
	default: ;
	}
	sc.UseTool(Tool);
	if (DrawMode.GetIndex() >= 2)
	{
		if (toolLineNumber >= 0)
		{
			sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, toolLineNumber);
		}
		toolLineNumber = Tool.LineNumber;
	}
}
