unit untCardEquipItem;

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
  TCardEquipToItem = class(TListBoxItem)
  private
    FCard: ICard;
    FCardId: Int64;
    FImg: TImage;
    FLbl: TText;
    function GetCardName: string;
    procedure SetCard(const Value: ICard);
    procedure SetCardId(const Value: Int64);
    procedure SetCardName(const Value: string);
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure termImpl(ATID: TThreadID; AData: Int64);
  public
    constructor Create(AOwner: TComponent; AC: ICard);
  published
    property Card: ICard read FCard write SetCard;
    property CardId: Int64 read FCardId write SetCardId;
    property CardName: string read GetCardName write SetCardName;
  end;

implementation

uses
  untThread, untDownloadImage, untUtils;

{ TCardEquipToItem }

constructor TCardEquipToItem.Create(AOwner: TComponent; AC: ICard);
begin
  inherited Create(AOwner);
  Parent := TFMXObject(AOwner);
  Align:= TAlignLayout.Top;
  Width := 250;
  Height := 76;

  FImg := TImage.Create(Self);
  FImg.Parent := Self;
  FImg.Align := TAlignLayout.Left;
  FImg.Width := 46;
  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;

  FLbl := TText.Create(Self);
  FLbl.Parent := Self;
  FLbl.Align := TAlignLayout.Client;
  FLbl.TextSettings.Font.Family := 'Microsoft Yahei';
  FLbl.TextSettings.Font.Size := 12;
  FLbl.WordWrap := False;
  FLbl.HitTest := False;
  FLbl.HorzTextAlign := TTextAlign.Leading;
  FLbl.VertTextAlign := TTextAlign.Center;

  Card := AC;
  if FCard = nil then begin
    FLbl.TextSettings.FontColor := COLOR_NONE;
    Exit();
  end;
  if FCard.GetOwner = coSelf then begin
    FLbl.TextSettings.FontColor := COLOR_SELF;
  end else if FCard.GetOwner = coOpponent then begin
    FLbl.TextSettings.FontColor := COLOR_OPPONENT;
  end else begin
    FLbl.TextSettings.FontColor := COLOR_NONE;
  end;
end;

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

function TCardEquipToItem.GetCardName: string;
begin
  Exit(FLbl.Text);
end;

procedure TCardEquipToItem.SetCard(const Value: ICard);
begin
  FCard := Value;
  if (FCard <> nil) then begin
    CardName:= FCard.GetCardName;
    CardId:= FCard.GetCardId;
  end else begin
    CardName:= '无';
    CardId:= 0;
  end;
end;

procedure TCardEquipToItem.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);
  end else begin
    ThreadEXT.ThreadExecute<Int64, Int64>(FCardId, execImpl, termImpl);
  end;
end;

procedure TCardEquipToItem.SetCardName(const Value: string);
begin
  FLbl.Text:= Value;
end;

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

end.
