unit frmContinousEffected;

{$mode Delphi}{$H+}

interface

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

type

  { TFormContinousEffected }

  TFormContinousEffected = class(TForm)
    CornerButton1: TD2CornerButton;
    CornerButton2: TD2CornerButton;
    D2Resources1: TD2Resources;
    Layout1: TD2Layout;
    lstCards: TD2ListBox;
    Root: TD2Background;
    Scene: TD2Scene;
    procedure CornerButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FCESelectedCards: TFPGList<TCard>;
    FCurrentCard: TCard;
    FPuzzle: TPuzzleField;

  public
    procedure RefreshList();

  published
    // managed by FormHome
    property CEPuzzle: TPuzzleField read FPuzzle write FPuzzle;
    property CECurrentCard: TCard read FCurrentCard write FCurrentCard;
    property CESelectedCards: TFPGList<TCard> read FCESelectedCards;
  end;

var
  FormContinousEffected: TFormContinousEffected;

implementation

uses
  untUtils;

{$R *.lfm}

{ TFormContinousEffected }

procedure TFormContinousEffected.FormCreate(Sender: TObject);
begin
  FCESelectedCards := TFPGList<TCard>.Create;
end;

procedure TFormContinousEffected.CornerButton1Click(Sender: TObject);
var
  i: Integer;
  item: TCardListItemCheck;
begin
  FCESelectedCards.Clear;
  for i := 0 to lstCards.Count - 1 do begin
    item := TCardListItemCheck(lstCards.Items[i]);
    if (item.Checked) then begin
      FCESelectedCards.Add(item.Card);
    end;
  end;
end;

procedure TFormContinousEffected.FormDestroy(Sender: TObject);
begin
  FCESelectedCards.Free;
  // FreeCards(FCESelectedCards);
end;

procedure TFormContinousEffected.RefreshList();
var
  i: Integer;
  c: TCard;
  item: TCardListItemCheck;
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 := TCardListItemCheck.Create(lstCards);
      item.CardName:= c.CardName;
      item.Id:= c.CardId;
      item.Player:= 0;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.ContinousEffectTo, c.CardId, c.Owner, c.Player, c.Location, c.Seq) <> nil;
    end;
  end;
  for i:= 0 to 5 do begin
    c := FPuzzle.SCard[0, i];
    if (c <> nil) then begin
      item := TCardListItemCheck.Create(lstCards);
      item.CardName:= c.CardName;
      item.Id:= c.CardId;
      item.Player:= 0;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.ContinousEffectTo, c.CardId, c.Owner, c.Player, c.Location, c.Seq) <> nil;
    end;
  end;

  for i := 5 to 6 do begin
    c := FPuzzle.ExtraMonster[i];
    if (c <> nil) then begin
      item := TCardListItemCheck.Create(lstCards);
      item.CardName:= c.CardName;
      item.Id:= c.CardId;
      item.Player:= IfThen<Integer>(c.Owner = coSelf, 0, 1);
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.ContinousEffectTo, c.CardId, c.Owner, c.Player, c.Location, c.Seq) <> nil;
    end;
  end;

  for i := 1 to 4 do begin
    c := FPuzzle.MCard[1, i];
    if (c <> nil) then begin
      item := TCardListItemCheck.Create(lstCards);
      item.CardName:= c.CardName;
      item.Id:= c.CardId;
      item.Player:= 1;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.ContinousEffectTo, c.CardId, c.Owner, c.Player, c.Location, c.Seq) <> nil;
    end;
  end;
  for i := 1 to 5 do begin
    c := FPuzzle.SCard[1, i];
    if (c <> nil) then begin
      item := TCardListItemCheck.Create(lstCards);
      item.CardName:= c.CardName;
      item.Id:= c.CardId;
      item.Player:= 1;
      item.Card := c;
      item.Checked:= PickOneCardFromList(FCurrentCard.ContinousEffectTo, c.CardId, c.Owner, c.Player, c.Location, c.Seq) <> nil;
    end;
  end;

  lstCards.EndUpdate;

end;

end.

