unit untCardOperationItem;

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
  TOnCardListItemOpDeleted = procedure (ACardId: Int64; AIndex: Integer) of object;
  TOnCardAddOneClicked = procedure (ACard: ICard) of object;

  TCardOperItem = class(TListBoxItem)
  private
    FCard: ICard;
    FCardName: string;
    FFace: Integer;
    FOnCardAddOneClicked: TOnCardAddOneClicked;
    FShowFace: Boolean;
    FCardId: Int64;
    FIndex: Integer;
    FOnDelete: TOnCardListItemOpDeleted;
    FImg: TImage;
    FLbl: TText;
    FBtnAddOne: TCornerButton;
    FbtnFace: TCornerButton;
    FBtnDel: TCornerButton;

    procedure innerBtnAddOneClick(Sender: TObject);
    procedure innerBtnDelClick(Sender: TObject);
    procedure innerBtnFaceClick(Sender: TObject);
    procedure SetCardName(AValue: string);
    procedure SetFace(AValue: Integer);
    procedure SetShowFace(AValue: Boolean);
    function execImpl(ATID: TThreadID; AData: Int64): Int64;
    procedure termImpl(ATID: TThreadID; AData: Int64);
    procedure SetCardId(const Value: Int64);
  public
     constructor Create(AOwner: TComponent); Override;
     destructor Destroy; Override;
  published
    property CardId: Int64 read FCardId write SetCardId;
    property CardName: string read FCardName write SetCardName;
    property Card: ICard read FCard write FCard;
    property OnDelete: TOnCardListItemOpDeleted read FOnDelete write FOnDelete;
    property OnCardAddOneClicked: TOnCardAddOneClicked read FOnCardAddOneClicked write FOnCardAddOneClicked;
    property Index: Integer read FIndex write FIndex;
    property Face: Integer read FFace write SetFace;
    property ShowFace: Boolean read FShowFace write SetShowFace;
  end;

implementation

uses
  untDownloadImage, untUtils, untThread;

{ TCardOperItem }

constructor TCardOperItem.Create(AOwner: TComponent);
var
  layRight: TLayout;
begin
  inherited Create(AOwner);
  Parent := TFmxObject(AOwner);
  Align := TAlignLayout.Top;

  Width:= 340;
  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;

  layRight := TLayout.Create(Self);
  layRight.Parent := Self;
  layRight.Align := TAlignLayout.MostRight;
  layRight.Width := 116;
  layRight.Margins.Right := 8;

  FBtnDel := TCornerButton.Create(layRight);
  FBtnDel.Parent := layRight;
  FBtnDel.Align:= TAlignLayout.MostRight;
  FBtnDel.Margins.Right:= 0;
  FBtnDel.Margins.Top:= 20;
  FBtnDel.Margins.Bottom:= 20;
  FBtnDel.Margins.Left:= 0;
  FBtnDel.Text:= 'X';
  FBtnDel.Height:= 36;
  FBtnDel.Width:= 36;
  FBtnDel.XRadius := 18;
  FBtnDel.YRadius := 18;
  FBtnDel.TextSettings.Font.Family:= 'Microsoft Yahei';
  FBtnDel.TextSettings.Font.Size:= 12;
  FBtnDel.TextSettings.Font.Style := [TFontStyle.fsBold];
  FBtnDel.TextSettings.FontColor := TAlphaColors.White;
  FBtnDel.OnClick:= innerBtnDelClick;

  FBtnAddOne:= TCornerButton.Create(layRight);
  FBtnAddOne.Parent := layRight;
  FBtnAddOne.Align:= TAlignLayout.Right;
  FBtnAddOne.Margins.Top:= 20;
  FBtnAddOne.Margins.Bottom:= 20;
  FBtnAddOne.Margins.Right:= 4;
  FBtnAddOne.Height:= 36;
  FBtnAddOne.Width:= 36;
  FBtnAddOne.XRadius := 18;
  FBtnAddOne.YRadius := 18;
  FBtnAddOne.TextSettings.Font.Family:= 'Microsoft Yahei';
  FBtnAddOne.TextSettings.Font.Size:= 12;
  FBtnAddOne.TextSettings.Font.Style := [TFontStyle.fsBold];
  FBtnAddOne.TextSettings.FontColor := TAlphaColors.White;
  FBtnAddOne.Text:= '+1';
  FBtnAddOne.OnClick:= innerBtnAddOneClick;

  FbtnFace := TCornerButton.Create(layRight);
  FbtnFace.Parent := layRight;
  FbtnFace.Align:= TAlignLayout.Left;
  FbtnFace.Margins.Right:= 0;
  FbtnFace.Margins.Top:= 20;
  FbtnFace.Margins.Bottom:= 20;
  FbtnFace.Margins.Left:= 0;
  FbtnFace.Height:= 36;
  FbtnFace.Width:= 36;
  FbtnFace.XRadius := 18;
  FbtnFace.YRadius := 18;
  FbtnFace.TextSettings.Font.Family:= 'Microsoft Yahei';
  FbtnFace.TextSettings.Font.Size:= 12;
  FbtnFace.TextSettings.Font.Style := [TFontStyle.fsBold];
  FbtnFace.TextSettings.FontColor := TAlphaColors.White;
  FbtnFace.OnClick:= innerBtnFaceClick;

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

end;

destructor TCardOperItem.Destroy;
begin
  FImg.Free;
  FLbl.Free;
  FBtnDel.Free;
  FBtnAddOne.Free;
  FbtnFace.Free;
  inherited;
end;

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

procedure TCardOperItem.innerBtnAddOneClick(Sender: TObject);
begin
  if (Assigned(FOnCardAddOneClicked)) then begin
    FOnCardAddOneClicked(FCard);
  end;
end;

procedure TCardOperItem.innerBtnDelClick(Sender: TObject);
begin
  if (Assigned(FOnDelete)) then begin
    FOnDelete(FCardId, FIndex);
  end;
end;

procedure TCardOperItem.innerBtnFaceClick(Sender: TObject);
begin
  if (FFace = 0) then begin
    Face:= 1;
  end else begin
    Face:= 0;
  end;
end;

procedure TCardOperItem.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 TCardOperItem.SetCardName(AValue: string);
begin
  FCardName:=AValue;
  FLbl.Text:= FCardName;
end;

procedure TCardOperItem.SetFace(AValue: Integer);
begin
  FFace:=AValue;
  FbtnFace.Text:= EXT.IfThen<String>(FFace = 0, '正面', '反面');
  FCard.SetPosition(EXT.IfThen<TCardPosition>(FFace = 0, cpUp, cpDown));
end;

procedure TCardOperItem.SetShowFace(AValue: Boolean);
begin
  FShowFace:= AValue;
  FbtnFace.Visible:= FShowFace;
end;

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

end.
