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;

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


{ 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);
  if (ComboBox1.ItemIndex = -1) and (ComboBox1.Items.Count > 0) then
    ComboBox1.ItemIndex := 0;
  Redraw;
  Translate;
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);
    end;
    Chart1.AddSeries(NewSeries);
  end;
end;

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

end.

