unit UFormGameSystems;

{$mode delphi}

interface

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

type

  { TFormGameSystems }

  TFormGameSystems = class(TForm)
    AAdd: TAction;
    ASelectAll: TAction;
    AModify: TAction;
    ARemove: TAction;
    AClone: TAction;
    ActionList1: TActionList;
    ListView1: TListView;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    PopupMenu1: TPopupMenu;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    procedure AAddExecute(Sender: TObject);
    procedure AModifyExecute(Sender: TObject);
    procedure ARemoveExecute(Sender: TObject);
    procedure ASelectAllExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    procedure ListView1KeyPress(Sender: TObject; var Key: char);
    procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
  private
    FGameSystems: TGameSystems;
    procedure SetGameSystems(AValue: TGameSystems);

  public
    procedure UpdateInterface;
    procedure UpdateList;
    property GameSystems: TGameSystems read FGameSystems write SetGameSystems;
  end;

var
  FormGameSystems: TFormGameSystems;


implementation

{$R *.lfm}

uses
  UCore, UFormGameSystem;

resourcestring
  SRemoveItems = 'Remove items';
  SRemoveItemsQuery = 'Do you want to remove selected items?';
  SNewGameSystem = 'New game system';

{ TFormGameSystems }

procedure TFormGameSystems.ARemoveExecute(Sender: TObject);
var
  I: Integer;
begin
  if Assigned(ListView1.Selected) then begin
    if MessageDlg(SRemoveItems, SRemoveItemsQuery,
    TMsgDlgType.mtConfirmation, [mbCancel, mbOk], 0) = mrOk then begin
      for I := ListView1.Items.Count - 1 downto 0 do
        if ListView1.Items[I].Selected then begin
          ListView1.Items[I].Selected := False;
          GameSystems.Remove(TGameSystem(ListView1.Items[I].Data));
        end;
      UpdateList;
      UpdateInterface;
    end;
  end;
end;

procedure TFormGameSystems.AModifyExecute(Sender: TObject);
var
  TempEntry: TGameSystem;
begin
  if Assigned(ListView1.Selected) then
  with TGameSystem(ListView1.Selected.Data) do begin
    TempEntry := TGameSystem.Create;
    TempEntry.Assign(TGameSystem(ListView1.Selected.Data));
    FormGameSystem := TFormGameSystem.Create(Self);
    try
      FormGameSystem.LoadData(TempEntry);
      if FormGameSystem.ShowModal = mrOk then begin
        FormGameSystem.SaveData(TempEntry);
        TGameSystem(ListView1.Selected.Data).Assign(TempEntry);
        UpdateList;
        UpdateInterface;
      end;
    finally
      FreeAndNil(FormGameSystem);
      FreeAndNil(TempEntry);
    end;
  end;
end;

procedure TFormGameSystems.AAddExecute(Sender: TObject);
var
  TempEntry: TGameSystem;
begin
  TempEntry := TGameSystem.Create;
  FormGameSystem := TFormGameSystem.Create(Self);
  try
    TempEntry.FileName := SNewGameSystem + GameSystemExt;
    FormGameSystem.LoadData(TempEntry);
    if FormGameSystem.ShowModal = mrOk then begin
      FormGameSystem.SaveData(TempEntry);
      GameSystems.Add(TempEntry);
      TempEntry := nil;
      UpdateList;
      UpdateInterface;
    end;
  finally
    FreeAndNil(FormGameSystem);
  end;
  TempEntry.Free;
end;

procedure TFormGameSystems.ASelectAllExecute(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to ListView1.Items.Count - 1 do
    ListView1.Items[I].Selected := True;
end;

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

procedure TFormGameSystems.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  Core.CoolTranslator1.TranslateComponentRecursive(Self);
  Core.ThemeManager1.UseTheme(Self);
  for I := 0 to ToolBar1.ButtonCount - 1 do
    ToolBar1.Buttons[I].Hint := ToolBar1.Buttons[I].Caption;
end;

procedure TFormGameSystems.FormDestroy(Sender: TObject);
begin
  GameSystems := nil;
end;

procedure TFormGameSystems.FormShow(Sender: TObject);
begin
  Core.PersistentForm.Load(Self);
  UpdateList;
  UpdateInterface;
end;

procedure TFormGameSystems.ListView1Data(Sender: TObject; Item: TListItem);
begin
  if Item.Index < ListView1.Items.Count then
  with TGameSystem(GameSystems[Item.Index]) do begin
    Item.Caption := GetName;
    Item.Data := GameSystems[Item.Index];
  end;
end;

procedure TFormGameSystems.ListView1KeyPress(Sender: TObject; var Key: char);
begin
  if Key = #13 then AModify.Execute;
  if Key = #27 then Close;
end;

procedure TFormGameSystems.ListView1SelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
begin
  UpdateInterface;
end;

procedure TFormGameSystems.SetGameSystems(AValue: TGameSystems);
begin
  if FGameSystems = AValue then Exit;
  FGameSystems := AValue;
end;

procedure TFormGameSystems.UpdateInterface;
begin
  ARemove.Enabled := Assigned(FGameSystems) and Assigned(ListView1.Selected);
  AModify.Enabled := Assigned(FGameSystems) and Assigned(ListView1.Selected);
  AAdd.Enabled := Assigned(FGameSystems);
  ASelectAll.Enabled := ListView1.Items.Count > 0;
end;

procedure TFormGameSystems.UpdateList;
begin
  if Assigned(GameSystems) then ListView1.Items.Count := GameSystems.Count
    else ListView1.Items.Count := 0;
  ListView1.Refresh;
end;

end.

