unit frmCardList;

{$mode Delphi}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, orca_scene2d, fgl, untData, untCard;

type

  { TFormCardList }

  TFormCardList = class(TForm)
    btnOK: TD2CornerButton;
    btnCancel: TD2CornerButton;
    btnAddCard: TD2CornerButton;
    D2Resources1: TD2Resources;
    Layout1: TD2Layout;
    lstCards: TD2ListBox;
    Root: TD2Background;
    Scene: TD2Scene;
    procedure btnAddCardClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FCardList: TFPGList<Int64>;
    procedure addOneCardItem(ACard: TCard);
    procedure deleteCardItem(ACardId: Int64; AIndex: Integer);
  public
    procedure FillCardIds(list: TFPGList<Int64>);
  published
    property CardList: TFPGList<Int64> read FCardList;
  end;

var
  FormCardList: TFormCardList;

implementation

uses untListItem, frmSearchCard, untConfig;

{$R *.lfm}

{ TFormCardList }

procedure TFormCardList.btnAddCardClick(Sender: TObject);
var
  cid: Int64 = 0;
  c: TCard;
  item: TCardListItemOp;
begin
  with TFormSearchCard.Create(Self) do begin
    if ShowModal = mrOK then begin
      cid := CardId;
    end;
    Free;
  end;
  if cid > 0 then begin
    FCardList.Add(cid);
    c := Cards.GetCardInfo(cid);
    lstCards.BeginUpdate;
    item := TCardListItemOp.Create(lstCards);
    item.CardName:= c.CardName;
    item.Id:= cid;
    item.Card := c;
    item.ShowFace := False;
    item.Index:= lstCards.Count - 1;
    item.OnDelete:= deleteCardItem;
    item.OnCardAddOneClicked:= addOneCardItem;
    lstCards.EndUpdate;
  end;
end;

procedure TFormCardList.FormCreate(Sender: TObject);
var
  AScale: Double;
begin
  FCardList := TFPGList<Int64>.Create;
  AScale:= TConfigReader.GetScale();
  self.Width:= Trunc(600 * AScale);
  self.Height:= Trunc(800 * AScale);
  Root.Scale.X:= AScale;
  Root.Scale.Y:= AScale;
end;

procedure TFormCardList.FormDestroy(Sender: TObject);
begin
  FCardList.Free;
end;

procedure TFormCardList.deleteCardItem(ACardId: Int64; AIndex: Integer);
var
  cid: Int64;
  i, j: Integer;
  item: TCardListItemOp;
begin
  // index matches
  for i:= 0 to FCardList.Count - 1 do begin
    cid := FCardList[i];
    if cid = ACardId then begin
      for j := 0 to lstCards.Count - 1 do begin
        item := TCardListItemOp(lstCards.Items[j]);
        if (item.Id = ACardId) and (item.Index = AIndex) then begin
          FCardList.Delete(i);
          item.Free;
          Break;
        end;
      end;
      Break;
    end;
  end;

  for i := 0 to lstCards.Count - 1 do begin
    item := TCardListItemOp(lstCards.Items[i]);
    item.Index:= i;
  end;
end;

procedure TFormCardList.addOneCardItem(ACard: TCard);
var
  item: TCardListItemOp;
  c: TCard;
begin
  c := Cards.GetCardInfo(ACard.CardId);
  FCardList.Add(ACard.CardId);
  lstCards.BeginUpdate;
  item:= TCardListItemOp.Create(lstCards);
  item.CardName:= c.CardName;
  item.Id:= c.CardId;
  item.Card := c;
  item.ShowFace:= False;
  item.Index:= lstCards.Count - 1;
  item.OnDelete:= deleteCardItem;
  item.OnCardAddOneClicked:= addOneCardItem;
  lstCards.EndUpdate;
end;

procedure TFormCardList.FillCardIds(list: TFPGList<Int64>);
var
  i: Integer;
  item: TCardListItemOp;
  cn: string;
  c: TCard;
begin
  FCardList.Clear;
  FCardList.AddList(list);
  lstCards.Clear;
  if (list.Count > 0) then begin
    lstCards.BeginUpdate;
    for i:= 0 to FCardList.Count - 1 do begin
      c := Cards.GetCardInfo(FCardList[i]);
      item := TCardListItemOp.Create(lstCards);
      item.CardName:= c.CardName;
      item.Id:= FCardList[i];
      item.ShowFace := False;
      item.Card := c;
      item.Index:= i;
      item.OnDelete:= deleteCardItem;
      item.OnCardAddOneClicked:= addOneCardItem;
    end;
    lstCards.EndUpdate;
  end;
end;

end.

