unit frmSearchCard;

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.ListBox, FMX.Objects, FMX.Effects;

type
  TFormSearchCard = class(TForm)
    layMain: TLayout;
    edtCardName: TEdit;
    btnOK: TCornerButton;
    btnCancel: TCornerButton;
    lstCards: TListBox;
    btnCleanHistory: TCornerButton;
    txtHistory0: TText;
    txtHistory4: TText;
    txtHistory3: TText;
    txtHistory2: TText;
    txtHistory1: TText;
    txtHistory5: TText;
    txtHistory9: TText;
    txtHistory8: TText;
    txtHistory7: TText;
    txtHistory6: TText;
    btnSearch: TSearchEditButton;
    procedure btnCleanHistoryClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure edtCardNameKeyUp(Sender: TObject; var Key: Word;
      var KeyChar: WideChar; Shift: TShiftState);
    procedure lstCardsDblClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure txtHistory0Click(Sender: TObject);
  private
    FCardId: Int64;
    FHistory: array[0..9] of TText;
    procedure SearchCard(Keyword: string);
    procedure SaveHistory(ALast: string);
  public

  published
    property CardId: Int64 read FCardId;
  end;

var
  FormSearchCard: TFormSearchCard;

implementation

uses
  moduleData, untConfig, untSearchCardItem, untCard, untArrayHelper, frmStyleModule;

{$R *.fmx}

{ TFormSearchCard }

procedure TFormSearchCard.btnCleanHistoryClick(Sender: TObject);
var
  i: Integer;
  Arr: TArray<String>;
begin
  // clean history
  Arr := TConfig.GetSearchHistory();
  for i:= 0 to 9 do begin
    Arr[i] := '';
    FHistory[i].Text:= '';
  end;
  TConfig.SetSearchHistory(Arr);
end;

procedure TFormSearchCard.btnOKClick(Sender: TObject);
var
  item: TSearchCardListItem;
begin
  if lstCards.ItemIndex = -1 then Exit;

  item := TSearchCardListItem(lstCards.ListItems[lstCards.ItemIndex]);
  if (not item.Ready) then Exit;

  FCardId := item.CardId;
  self.ModalResult:= mrOK;
end;

procedure TFormSearchCard.btnSearchClick(Sender: TObject);
var
  keyword: string;
begin
  keyword:= edtCardName.Text;
  SearchCard(keyword);
end;

procedure TFormSearchCard.edtCardNameKeyUp(Sender: TObject; var Key: Word;
  var KeyChar: WideChar; Shift: TShiftState);
begin
  if (Key = vkReturn) then begin
    btnSearchClick(btnSearch);
  end;
end;

procedure TFormSearchCard.FormCreate(Sender: TObject);
var
  Arr: TArray<String>;
  i: Integer;
begin

  FHistory[0] := txtHistory0;
  FHistory[1] := txtHistory1;
  FHistory[2] := txtHistory2;
  FHistory[3] := txtHistory3;
  FHistory[4] := txtHistory4;
  FHistory[5] := txtHistory5;
  FHistory[6] := txtHistory6;
  FHistory[7] := txtHistory7;
  FHistory[8] := txtHistory8;
  FHistory[9] := txtHistory9;

  Arr := TConfig.GetSearchHistory;
  for i:= 0 to 9 do begin
    FHistory[i].Text:= Arr[i];
  end;
end;

procedure TFormSearchCard.lstCardsDblClick(Sender: TObject);
var
  idx: Integer;
begin
  idx := lstCards.ItemIndex;
  if idx > -1 then begin
    btnOKClick(btnOK);
  end;
end;

procedure TFormSearchCard.SaveHistory(ALast: string);
var
  Arr: TArray<String>;
  i: Integer;
begin
  Arr := TConfig.GetSearchHistory;
  for i := 0 to 9 do begin
    if Arr[i] = ALast then Exit;
  end;

  for i := 9 downto 1 do begin
    Arr[i] := Arr[i - 1];
    FHistory[i].Text:= Arr[i];
  end;
  Arr[0] := ALast;
  FHistory[0].Text:= Arr[0];
  TConfig.SetSearchHistory(Arr);
end;

procedure TFormSearchCard.SearchCard(Keyword: string);
var
  list: TArray<ICard>;
  i: Integer;
  item: TSearchCardListItem;
begin
  if (keyword = '') then Exit;
  lstCards.Enabled := False;
  list := DMData.SearchCards(Keyword);
  lstCards.Clear;
  lstCards.BeginUpdate;
  for i := 0 to list.Count - 1 do begin
    item := TSearchCardListItem.Create(lstCards);
    item.CardName := list[i].GetCardName;
    item.CardId := list[i].GetCardId;
  end;
  lstCards.EndUpdate;
  // deselect all
  lstCards.ItemIndex := -1;
  // save history
  SaveHistory(Keyword);
  lstCards.Enabled := True;
end;

procedure TFormSearchCard.txtHistory0Click(Sender: TObject);
var
  keyword: string;
begin
  keyword:= TText(Sender).Text;
  SearchCard(keyword);
end;

end.
