unit frmContinousEffected;

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
  TFormContinousEffected = class(TForm)
    btnOK: TCornerButton;
    btnCancel: TCornerButton;
    lstCards: TListBox;
    procedure btnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FCESelectedCards: TArray<ICard>;
    FCurrentCard: ICard;
    FPuzzle: TPuzzleField;
  public
    procedure RefreshList();
  published
    // managed by FormHome
    property CEPuzzle: TPuzzleField read FPuzzle write FPuzzle;
    property CECurrentCard: ICard read FCurrentCard write FCurrentCard;
    property CESelectedCards: TArray<ICard> read FCESelectedCards;
  end;

var
  FormContinousEffected: TFormContinousEffected;

implementation

uses
  untCardCheckItem, untArrayHelper, untUtils, frmStyleModule;

{$R *.fmx}

procedure TFormContinousEffected.btnOKClick(Sender: TObject);
var
  i: Integer;
  item: TCardCheckItem;
begin
  SetLength(FCESelectedCards, 0);

  for i := 0 to lstCards.Count - 1 do begin
    item := TCardCheckItem(lstCards.ListItems[i]);
    if (item.Checked) then begin
      FCESelectedCards.Add(item.Card);
    end;
  end;
end;

procedure TFormContinousEffected.FormCreate(Sender: TObject);
begin
  SetLength(FCESelectedCards, 0);
end;

procedure TFormContinousEffected.FormDestroy(Sender: TObject);
begin
  SetLength(FCESelectedCards, 0);
end;

procedure TFormContinousEffected.RefreshList;
var
  i: Integer;
  c: ICard;
  item: TCardCheckItem;
begin
  if FPuzzle = nil then Exit;
  if FCurrentCard = nil then Exit;

  lstCards.Clear;
  lstCards.BeginUpdate;

  for i:= 0 to 4 do begin
    c := FPuzzle.MCard[0, i];
    if (c <> nil) then begin
      item := TCardCheckItem.Create(lstCards);
      item.CardName:= c.GetCardName;
      item.CardId:= c.GetCardId;
      item.Player:= 0;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.GetContinousEffectTo, c.GetCardId, c.GetOwner, c.GetPlayer, c.GetLocation, c.GetSeq) <> nil;
    end;
  end;

  for i:= 0 to 5 do begin
    c := FPuzzle.SCard[0, i];
    if (c <> nil) then begin
      item := TCardCheckItem.Create(lstCards);
      item.CardName:= c.GetCardName;
      item.CardId:= c.GetCardId;
      item.Player:= 0;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.GetContinousEffectTo, c.GetCardId, c.GetOwner, c.GetPlayer, c.GetLocation, c.GetSeq) <> nil;
    end;
  end;

  for i := 5 to 6 do begin
    c := FPuzzle.ExtraMonster[i];
    if (c <> nil) then begin
      item := TCardCheckItem.Create(lstCards);
      item.CardName:= c.GetCardName;
      item.CArdId:= c.GetCardId;
      item.Player:= EXT.IfThen<Integer>(c.GetOwner = coSelf, 0, 1);
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.GetContinousEffectTo, c.GetCardId, c.GetOwner, c.GetPlayer, c.GetLocation, c.GetSeq) <> nil;
    end;
  end;

  for i := 1 to 4 do begin
    c := FPuzzle.MCard[1, i];
    if (c <> nil) then begin
      item := TCardCheckItem.Create(lstCards);
      item.CardName:= c.GetCardName;
      item.CardId:= c.GetCardId;
      item.Player:= 1;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.GetContinousEffectTo, c.GetCardId, c.GetOwner, c.GetPlayer, c.GetLocation, c.GetSeq) <> nil;
    end;
  end;
  for i := 1 to 5 do begin
    c := FPuzzle.SCard[1, i];
    if (c <> nil) then begin
      item := TCardCheckItem.Create(lstCards);
      item.CardName:= c.GetCardName;
      item.CardId:= c.GetCardId;
      item.Player:= 1;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.GetContinousEffectTo, c.GetCardId, c.GetOwner, c.GetPlayer, c.GetLocation, c.GetSeq) <> nil;
    end;
  end;

  lstCards.EndUpdate;

  (*



  *)
end;

end.
