unit UFormPlayersStats;

{$mode delphi}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  Menus, ActnList, UGame, UListViewSort;

type

  { TFormPlayersStats }

  TFormPlayersStats = class(TForm)
    ASpectate: TAction;
    ActionList1: TActionList;
    ListView1: TListView;
    ListViewSort1: TListViewSort;
    MenuItem1: TMenuItem;
    PopupMenu1: TPopupMenu;
    procedure ASpectateExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
    procedure ListViewSort1Filter(ListViewSort: TListViewSort);
  private

  public
    procedure ReloadList;
  end;

var
  FormPlayersStats: TFormPlayersStats;


implementation

uses
  UCore, UPlayer;

{$R *.lfm}

{ TFormPlayersStats }

procedure TFormPlayersStats.ListView1Data(Sender: TObject; Item: TListItem);
begin
  with Core.Game do
  if Item.Index < ListViewSort1.List.Count then
  with TPlayer(ListViewSort1.List[Item.Index]) do begin
    Item.Caption := Name;
    Item.Data := ListViewSort1.List[Item.Index];
    Item.SubItems.Add(PlayerModeText[Mode]);
    Item.SubItems.Add(IntToStr(TotalCells));
    Item.SubItems.Add(IntToStr(TotalDiscovered));
    Item.SubItems.Add(IntToStr(TotalCities));
    Item.SubItems.Add(IntToStr(TotalUnits));
    Item.SubItems.Add(IntToStr(TotalWinObjectiveCells));
  end;
end;

function TFormPlayersStats.ListViewSort1CompareItem(Item1, Item2: TObject
  ): Integer;
begin
  Result := 0;
  if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
    with ListViewSort1 do
    case Column of
      0: Result := CompareString(TPlayer(Item1).Name, TPlayer(Item2).Name);
      1: Result := CompareString(PlayerModeText[TPlayer(Item1).Mode], PlayerModeText[TPlayer(Item2).Mode]);
      2: Result := CompareInteger(TPlayer(Item1).TotalCells, TPlayer(Item2).TotalCells);
      3: Result := CompareInteger(TPlayer(Item1).TotalDiscovered, TPlayer(Item2).TotalDiscovered);
      4: Result := CompareInteger(TPlayer(Item1).TotalCities, TPlayer(Item2).TotalCities);
      5: Result := CompareInteger(TPlayer(Item1).TotalUnits, TPlayer(Item2).TotalUnits);
      6: Result := CompareInteger(TPlayer(Item1).TotalWinObjectiveCells, TPlayer(Item2).TotalWinObjectiveCells);
    end;
    if ListViewSort1.Order = soDown then Result := -Result;
  end else Result := 0;
end;

procedure TFormPlayersStats.ListViewSort1Filter(ListViewSort: TListViewSort);
var
  I: Integer;
begin
  ListViewSort1.List.Clear;
  for I := 0 to Core.Game.Players.Count - 1 do begin
    ListViewSort1.List.Add(Core.Game.Players[I]);
  end;
end;

procedure TFormPlayersStats.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
  Core.ThemeManager1.UseTheme(Self);
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
  ReloadList;
end;

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

procedure TFormPlayersStats.ASpectateExecute(Sender: TObject);
begin
  if Assigned(ListView1.Selected) then
    Core.Spectate(TPlayer(ListView1.Selected.Data));
end;

procedure TFormPlayersStats.ReloadList;
begin
  ListViewSort1.Refresh;
end;

end.

