unit frmCardList;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.ListBox, untCard;

type
  TFormCardList = class(TForm)
    btnOK: TCornerButton;
    btnCancel: TCornerButton;
    btnAddCard: TCornerButton;
    lstCards: TListBox;
    procedure btnAddCardClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FCardList: TArray<Int64>;
    procedure addOneCardItem(ACard: ICard);
    procedure deleteCardItem(ACardId: Int64; AIndex: Integer);
  public
    procedure FillCardIds(list: TArray<Int64>);
  published
    property CardList: TArray<Int64> read FCardList;
  end;

var
  FormCardList: TFormCardList;

implementation

uses
  untCardOperationItem, frmSearchCard, untArrayHelper, moduleData, frmStyleModule;

{$R *.fmx}

procedure TFormCardList.addOneCardItem(ACard: ICard);
var
  item: TCardOperItem;
  c: ICard;
begin
  c := DMData.GetCardInfo(ACard.GetCardId);
  FCardList.Add(ACard.GetCardId);
  lstCards.BeginUpdate;
  item:= TCardOperItem.Create(lstCards);
  item.CardName:= c.GetCardName;
  item.CardId:= c.GetCardId;
  item.Card := c;
  item.ShowFace:= False;
  item.Index:= lstCards.Count - 1;
  item.OnDelete:= deleteCardItem;
  item.OnCardAddOneClicked:= addOneCardItem;
  lstCards.EndUpdate;
end;

procedure TFormCardList.btnAddCardClick(Sender: TObject);
var
  cid: Int64;
  c: ICard;
  item: TCardOperItem;
begin
  cid := 0;
  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 := DMData.GetCardInfo(cid);
    lstCards.BeginUpdate;
    item := TCardOperItem.Create(lstCards);
    item.CardName:= c.GetCardName;
    item.CardId:= cid;
    item.Card := c;
    item.ShowFace := False;
    item.Index:= lstCards.Count - 1;
    item.OnDelete:= deleteCardItem;
    item.OnCardAddOneClicked:= addOneCardItem;
    lstCards.EndUpdate;
  end;
end;

procedure TFormCardList.deleteCardItem(ACardId: Int64; AIndex: Integer);
var
  cid: Int64;
  i, j: Integer;
  item: TCardOperItem;
begin
  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 := TCardOperItem(lstCards.ListItems[j]);
        if (item.CardId = 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 := TCardOperItem(lstCards.ListItems[i]);
    item.Index:= i;
  end;

end;

procedure TFormCardList.FillCardIds(list: TArray<Int64>);
var
  i: Integer;
  item: TCardOperItem;
  c: ICard;
begin
  SetLength(FCardList, 0);
  SetLength(FCardList, list.Count);
  for i := 0 to list.Count - 1 do begin
    FCardList[i] := list[i];
  end;

  lstCards.Clear;

  if (list.Count > 0) then begin
    lstCards.BeginUpdate;
    for i:= 0 to FCardList.Count - 1 do begin
      c := DMData.GetCardInfo(FCardList[i]);
      item := TCardOperItem.Create(lstCards);
      item.CardName:= c.GetCardName;
      item.CardId:= FCardList[i];
      item.ShowFace := False;
      item.Card := c;
      item.Index:= i;
      item.OnDelete:= deleteCardItem;
      item.OnCardAddOneClicked:= addOneCardItem;
    end;
    lstCards.EndUpdate;
  end;
end;

procedure TFormCardList.FormCreate(Sender: TObject);
begin
  SetLength(FCardList, 0);
end;

procedure TFormCardList.FormDestroy(Sender: TObject);
begin
  SetLength(FCardList, 0);
end;

end.
