unit UFormCharts;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, TAGraph, TASeries, Forms, Controls, Graphics,
  Dialogs, StdCtrls;

type

  { TFormCharts }

  TFormCharts = class(TForm)
    Chart1: TChart;
    ComboBox1: TComboBox;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    procedure Redraw;
    procedure Translate;
  end;

var
  FormCharts: TFormCharts;

implementation

{$R *.lfm}

uses
  UCore, UGame, UPlayer;

resourcestring
  SOccupiedCells = 'Occupied cells';
  SMilitaryPower = 'Military power';
  SDiscoveredCells = 'Discovered cells';
  SCitiesCount = 'Cities count';
  SWinObjectiveCells = 'Win objective cells';


{ TFormCharts }

procedure TFormCharts.ComboBox1Change(Sender: TObject);
begin
  Redraw;
end;

procedure TFormCharts.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  Core.PersistentForm.Save(Self);
end;

procedure TFormCharts.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
  Core.ThemeManager1.UseTheme(Self);
  Redraw;
  Translate;
  if (ComboBox1.ItemIndex = -1) and (ComboBox1.Items.Count > 0) then
    ComboBox1.ItemIndex := 0;
end;

procedure TFormCharts.Redraw;
var
  I: Integer;
  X: Integer;
  NewSeries: TLineSeries;
begin
  Chart1.Series.Clear;
  for I := 0 to Core.Game.Players.Count - 1 do
  with TPlayer(Core.Game.Players[I]) do begin
    NewSeries := TLineSeries.Create(nil);
    NewSeries.LinePen.Color := Color;
    for X := 0 to TurnStats.Count - 1 do begin
      if ComboBox1.ItemIndex = 0 then NewSeries.AddXY(X, TGameTurnStat(TurnStats[X]).OccupiedCells)
      else if ComboBox1.ItemIndex = 1 then NewSeries.AddXY(X, TGameTurnStat(TurnStats[X]).Units)
      else if ComboBox1.ItemIndex = 2 then NewSeries.AddXY(X, TGameTurnStat(TurnStats[X]).DiscoveredCells)
      else if ComboBox1.ItemIndex = 3 then NewSeries.AddXY(X, TGameTurnStat(TurnStats[X]).Cities)
      else if ComboBox1.ItemIndex = 4 then NewSeries.AddXY(X, TGameTurnStat(TurnStats[X]).WinObjectiveCells);
    end;
    Chart1.AddSeries(NewSeries);
  end;
end;

procedure TFormCharts.Translate;
var
  LastIndex: Integer;
begin
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
  with ComboBox1 do begin
    LastIndex := ItemIndex;
    Clear;
    Items.Add(SOccupiedCells);
    Items.Add(SMilitaryPower);
    Items.Add(SDiscoveredCells);
    Items.Add(SCitiesCount);
    Items.Add(SWinObjectiveCells);
    ItemIndex := LastIndex;
  end;
end;

end.

