unit FMXTee.Editor.Tools.SeriesStats;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.Tools.Series,
  FMX.ListBox, FMX.Edit, FMXTee.Tools.SeriesStats, FMXTee.Engine, FMX.TabControl,
  FMXTee.Procs, FMX.Layouts, FMX.Memo;

type
  TSeriesStatsEditor = class(TSeriesToolEditor)
    TabControl1: TTabControl;
    TabStats: TTabItem;
    TabInfo: TTabItem;
    CheckBox1: TCheckBox;
    Button1: TButton;
    CheckBox2: TCheckBox;
    Button2: TButton;
    CheckBox3: TCheckBox;
    Button3: TButton;
    CheckBox4: TCheckBox;
    Button4: TButton;
    CheckBox5: TCheckBox;
    Button5: TButton;
    CheckBox6: TCheckBox;
    Button6: TButton;
    Memo1: TMemo;
    procedure FormActivate(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure CBSeriesChange(Sender: TObject);
    procedure CheckBox1Change(Sender: TObject);
    procedure ButtonClick(Sender: TObject);
    procedure CheckBox2Change(Sender: TObject);
    procedure CheckBox3Change(Sender: TObject);
    procedure CheckBox4Change(Sender: TObject);
    procedure CheckBox5Change(Sender: TObject);
    procedure CheckBox6Change(Sender: TObject);
  private
    { Private declarations }
    SeriesStats : TSeriesStatsTool;

    procedure EnableControls;
    procedure RefreshInfo;
    procedure CheckFunction(ABox:TCheckBox; AFunctionClass:TTeeFunctionClass);
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Chart.Functions, FMXTee.Functions.Stats,
  FMXTee.Functions.CurveFitting, FMXTee.Editor.Series;//, TeePenDlg, TeeConst, TeeProCo;

type
  TSeriesStatsToolAccess=class(TSeriesStatsTool);

procedure TSeriesStatsEditor.EnableControls;

  procedure EnableCheck(AButton:TButton; AClass:TTeeFunctionClass);
  var tmp : TCheckBox;
  begin
    if Assigned(AButton.TagObject) then
    begin
      tmp:=TCheckBox(AButton.TagObject);

      tmp.Enabled:=Assigned(SeriesStats) and Assigned(SeriesStats.Series);

      if Assigned(SeriesStats) then
         tmp.TagObject:=TSeriesStatsToolAccess(SeriesStats).GetStatFunction(AClass);

      tmp.IsChecked:=Assigned(tmp.TagObject);
      AButton.Enabled:=tmp.IsChecked;
    end;
  end;

begin
  EnableCheck(Button1,TAverageTeeFunction);
  EnableCheck(Button2,TTrendFunction);
  EnableCheck(Button3,TLowTeeFunction);
  EnableCheck(Button4,THighTeeFunction);
  EnableCheck(Button5,TMedianTeeFunction);
  EnableCheck(Button6,TCurveFittingFunction);
end;

procedure TSeriesStatsEditor.RefreshInfo;
begin
  Memo1.Lines.Clear;

  if Assigned(SeriesStats) and Assigned(SeriesStats.Series) then
     SeriesStats.Statistics(Memo1.Lines);
end;

procedure TSeriesStatsEditor.FormActivate(Sender: TObject);
begin
  inherited;

  if not Assigned(SeriesStats) then
  begin
    SeriesStats:=TSeriesStatsTool(TagObject);
    TabControl1.ActiveTab:=TabStats;

    Button1.TagObject:=CheckBox1;
    Button2.TagObject:=CheckBox2;
    Button3.TagObject:=CheckBox3;
    Button4.TagObject:=CheckBox4;
    Button5.TagObject:=CheckBox5;
    Button6.TagObject:=CheckBox6;

    EnableControls;
  end;
end;

procedure TSeriesStatsEditor.CheckBox1Change(Sender: TObject);
begin
  Button1.Enabled:=CheckBox1.IsChecked;
  CheckFunction(CheckBox1,TAverageTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox2Change(Sender: TObject);
begin
  Button2.Enabled:=CheckBox2.IsChecked;
  CheckFunction(CheckBox2,TTrendFunction);
end;

procedure TSeriesStatsEditor.CheckBox3Change(Sender: TObject);
begin
  Button3.Enabled:=CheckBox3.IsChecked;
  CheckFunction(CheckBox3,TLowTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox4Change(Sender: TObject);
begin
  Button4.Enabled:=CheckBox4.IsChecked;
  CheckFunction(CheckBox4,THighTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox5Change(Sender: TObject);
begin
  Button5.Enabled:=CheckBox5.IsChecked;
  CheckFunction(CheckBox5,TMedianTeeFunction);
end;

procedure TSeriesStatsEditor.CheckBox6Change(Sender: TObject);
begin
  Button6.Enabled:=CheckBox6.IsChecked;
  CheckFunction(CheckBox6,TCurveFittingFunction);
end;

procedure TSeriesStatsEditor.CheckFunction(ABox:TCheckBox; AFunctionClass:TTeeFunctionClass);
var tmp : TTeeFunction;
begin
  if Assigned(SeriesStats) and Assigned(SeriesStats.Series) then
  begin
    tmp:=TSeriesStatsToolAccess(SeriesStats).GetStatFunction(AFunctionClass);

    if Assigned(tmp) then
       tmp.ParentSeries.Visible:=ABox.IsChecked
    else
    if ABox.IsChecked then
    begin
      tmp:=AFunctionClass.Create(SeriesStats.Owner);
      tmp.ParentSeries:=TChartSeriesClass(SeriesStats.Series.ClassType).Create(SeriesStats.Owner);
      tmp.ParentSeries.ShowInEditor:=False;
      tmp.ParentSeries.ParentChart:=SeriesStats.ParentChart;
      tmp.ParentSeries.DataSource:=SeriesStats.Series;
      tmp.ParentSeries.FunctionType:=tmp;
      ABox.TagObject:=tmp;
    end;
  end;
end;

procedure TSeriesStatsEditor.TabControl1Change(Sender: TObject);
begin
  if TabControl1.ActiveTab=TabInfo then
     RefreshInfo;
end;

procedure TSeriesStatsEditor.ButtonClick(Sender: TObject);
var tmp : TCheckBox;
begin
  tmp:=TCheckBox((Sender as TButton).TagObject);
  with TSeriesEditor.CreateEditor(Self,TTeeFunction(tmp.TagObject).ParentSeries) do
  try
    Panel1.Visible:=False;
    Caption:=tmp.Text;
    ShowModal;
  finally
    Free;
  end;
end;

procedure TSeriesStatsEditor.CBSeriesChange(Sender: TObject);
begin
  inherited;

  EnableControls;
  RefreshInfo;
end;

initialization
  RegisterSeriesEditor(TSeriesStatsEditor);
end.
