unit untSearchCardItem;

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
  TSearchCardListItem = class(TListBoxItem)
  private
    FCardId: Int64;
    FImg: TImage;
    FLbl: TText;
    FReady: Boolean;
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure termImpl(ATID: TThreadID; AData: Int64);
    function GetCardName: String;
    procedure SetCardId(const Value: Int64);
    procedure SetCardName(const Value: String);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property CardId: Int64 read FCardId write SetCardId;
    property CardName: String read GetCardName write SetCardName;
    property Ready: Boolean read FReady;
  end;

implementation

uses
  untThread, untDownloadImage, untUtils;

{ TSearchCardListItem }

constructor TSearchCardListItem.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Parent := TFmxObject(AOwner);

  FReady := False;

  Width := 195;
  Height := 220;
  Padding.Top := 4;
  Padding.Bottom := 4;
  Padding.Left := 4;
  Padding.Right := 4;

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

  FImg := TImage.Create(Self);
  FImg.parent := Self;
  FImg.Align := TAlignLayout.Client;
  FImg.WrapMode := TImageWrapMode.Fit;
  FImg.HitTest := False;

end;

destructor TSearchCardListItem.Destroy;
begin
  FLbl.Free;
  FImg.Free;
  inherited;
end;

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

function TSearchCardListItem.GetCardName: String;
begin
  Exit(FLbl.Text);
end;

procedure TSearchCardListItem.SetCardId(const Value: Int64);
var
  AImgPath: string;
begin
  FCardId := Value;
  AImgPath:= ExtractFilePath(ParamStr(0)) + PathDelim + 'cardimg' + PathDelim;
  if (not DirectoryExists(AImgPath)) then begin
    ForceDirectories(AImgPath);
  end;
  AImgPath := AImgPath + Format('%d.jpg', [FCardId]);
  if FileExists(AImgPath) then begin
    FImg.Bitmap.LoadFromFile(AImgPath);
    FReady := True;
  end else begin
    ThreadEXT.ThreadExecute<Int64, Int64>(FCardId, execImpl, termImpl);
  end;
end;

procedure TSearchCardListItem.SetCardName(const Value: String);
begin
  FLbl.Text := Value;
end;

procedure TSearchCardListItem.termImpl(ATID: TThreadID; AData: Int64);
begin
  TDownloader.LoadCardImage(AData, FImg);
  FReady := True;
end;

end.
