unit untCardCheckItem;

interface

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

type
  
  TCardCheckItem = class(TListBoxItem)
  private
    FCard: ICard;
    FCardName: string;
    FId: Int64;
    FImg: TImage;
    FLbl: TText;
    FChk: TCheckBox;
    FPlayer: Integer;
    FCardId: Int64;
    function GetChecked: Boolean;
    procedure SetCard(AValue: ICard);
    procedure SetCardName(AValue: string);
    procedure SetChecked(AValue: Boolean);
    procedure SetCardId(AValue: Int64);
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure SetPlayer(AValue: Integer);
    procedure termImpl(ATID: TThreadID; AData: Int64);
  public
    constructor Create(AOwner: TComponent); Override;
    destructor Destroy; Override;
  published
    property Card: ICard read FCard write SetCard;
    property CardId: Int64 read FCardId write SetCardId;
    property CardName: string read FCardName write SetCardName;
    property Player: Integer read FPlayer write SetPlayer;
    property Checked: Boolean read GetChecked write SetChecked;
  end;

implementation

uses
  untDownloadImage, untUtils, untThread;

{ TCardCheckItem }

constructor TCardCheckItem.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Parent := TFMXObject(AOwner);
  Align:= TAlignLayout.Top;

  Width:= 584;
  Height:= 76;

  FImg := TImage.Create(Self);
  FImg.Parent := Self;
  FImg.Align:= TAlignLayout.MostLeft;
  FImg.Width:= 50;
  FImg.Height:= 68;
  FImg.Margins.Left:= 8;
  FImg.Margins.Right:= 8;
  FImg.Margins.Top:= 4;
  FImg.Margins.Bottom:= 4;
  FImg.WrapMode:= TImageWrapMode.Fit;
  FImg.HitTest:= False;

  FChk := TCheckBox.Create(Self);
  FChk.Parent := Self;
  FChk.Align := TAlignLayout.Right;
  FChk.Margins.Right := 8;
  FChk.Margins.Top := 16;
  FChk.Margins.Bottom := 16;
  FChk.Width := 19;
  FChk.Scale.X:= 1.5;
  FChk.Scale.Y:= 1.5;

  FLbl := TText.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align:= TAlignLayout.Client;
  FLbl.TextSettings.Font.Family:= 'Microsoft Yahei';
  FLbl.TextSettings.Font.Size:= 16;
  FLbl.WordWrap:= False;
  FLbl.HitTest:= False;
  FLbl.TextSettings.HorzAlign := TTextAlign.Leading;
  FLbl.TextSettings.VertAlign:= TTextAlign.Center;
  FLbl.TextSettings.FontColor := TAlphaColors.White;

end;

destructor TCardCheckItem.Destroy;
begin
  FChk.Free;
  FLbl.Free;
  FImg.Free;
  inherited;
end;

function TCardCheckItem.execImpl(ATID: TThreadID; AData: Int64): Int64;
var
  ASucc: Boolean;
begin
  ASucc := TDownloader.DownloadCardImage(AData);
  Exit(EXT.IfThen<Int64>(ASucc, AData, 0));
end;

function TCardCheckItem.GetChecked: Boolean;
begin
  Exit(FChk.IsChecked);
end;

procedure TCardCheckItem.SetCard(AValue: ICard);
begin
  FCard:=AValue;
end;

procedure TCardCheckItem.SetCardName(AValue: string);
begin
  FCardName:= AValue;
  FLbl.Text:= FCardName;
end;

procedure TCardCheckItem.SetChecked(AValue: Boolean);
begin
  FChk.IsChecked:= AValue;
end;

procedure TCardCheckItem.SetCardId(AValue: Int64);
var
  AImgPath: string;
begin
  FId:= AValue;
  AImgPath:= ExtractFilePath(ParamStr(0)) + PathDelim + 'cardimg' + PathDelim;
  if (not DirectoryExists(AImgPath)) then begin
    ForceDirectories(AImgPath);
  end;
  AImgPath := AImgPath + Format('%d.jpg', [FId]);

  if FileExists(AImgPath) then begin
    FImg.Bitmap.LoadFromFile(AImgPath);
  end else begin
    ThreadEXT.ThreadExecute<Int64, Int64>(FId, execImpl, termImpl);
  end;
end;

procedure TCardCheckItem.SetPlayer(AValue: Integer);
begin
  FPlayer:=AValue;
  case FPlayer of
  0:
    begin
      FLbl.TextSettings.FontColor := COLOR_SELF;
    end;
  1:
    begin
      FLbl.TextSettings.FontColor := COLOR_OPPONENT;
    end;
  end;
end;

procedure TCardCheckItem.termImpl(ATID: TThreadID; AData: Int64);
begin
  TDownloader.LoadCardImage(AData, FImg);
end;

end.
