unit frmSearchCard;

{$mode Delphi}{$H+}

interface

uses
  Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, orca_scene2d, untData, untCard, fgl;

type

  { TFormSearchCard }

  TFormSearchCard = class(TForm)
    btnSearchCard: TD2CornerButton;
    btnCancel: TD2CornerButton;
    btnOK: TD2CornerButton;
    CircleButton1: TD2CircleButton;
    D2Resources1: TD2Resources;
    Label1: TD2Label;
    Layout1: TD2Layout;
    Layout2: TD2Layout;
    layHistory: TD2Layout;
    Layout3: TD2Layout;
    Layout4: TD2Layout;
    Layout5: TD2Layout;
    lstCards: TD2ListBox;
    Root: TD2Background;
    Scene: TD2Scene;
    edtCardName: TD2TextBoxClearBtn;
    txtHistory1: TD2Text;
    txtHistory4: TD2Text;
    txtHistory3: TD2Text;
    txtHistory2: TD2Text;
    txtHistory5: TD2Text;
    txtHistory9: TD2Text;
    txtHistory8: TD2Text;
    txtHistory7: TD2Text;
    txtHistory6: TD2Text;
    txtHistory0: TD2Text;
    procedure btnOKClick(Sender: TObject);
    procedure btnSearchCardClick(Sender: TObject);
    procedure CircleButton1Click(Sender: TObject);
    procedure edtCardNameKeyUp(Sender: TObject; var Key: Word;
      var KeyChar: System.Widechar; Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure lstCardsClick(Sender: TObject);
    procedure lstCardsDblClick(Sender: TObject);
    procedure txtHistory0Click(Sender: TObject);
  private
    FCardId: Int64;
    FHistory: array[0..9] of TD2Text;
    procedure SearchCard(Keyword: string);
    procedure SaveHistory(ALast: string);
  public

  published
    property CardId: Int64 read FCardId;
  end;

var
  FormSearchCard: TFormSearchCard;

implementation

uses
  untListItem, untConfig;

{$R *.lfm}

{ TFormSearchCard }

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

procedure TFormSearchCard.CircleButton1Click(Sender: TObject);
var
  i: Integer;
  Arr: TStringArray;
begin
  Arr := TConfigReader.GetSearchHistory();
  for i:= 0 to 9 do begin
    Arr[i] := '';
    FHistory[i].Text:= '';
  end;
  TConfigReader.SetSearchHistory(Arr);
end;

procedure TFormSearchCard.edtCardNameKeyUp(Sender: TObject; var Key: Word;
  var KeyChar: System.Widechar; Shift: TShiftState);
begin
  if (Key = VK_RETURN) then begin
    btnSearchCardClick(btnSearchCard);
  end;
end;

procedure TFormSearchCard.FormCreate(Sender: TObject);
var
  AScale: Double;
  Arr: TStringArray;
  i: Integer;
begin
  AScale:= TConfigReader.GetScale();
  self.Width:= Trunc(600 * AScale);
  self.Height:= Trunc(800 * AScale);
  Root.Scale.X:= AScale;
  Root.Scale.Y:= AScale;

  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 := TConfigReader.GetSearchHistory();
  for i:= 0 to 9 do begin
    FHistory[i].Text:= Arr[i];
  end;
end;

procedure TFormSearchCard.lstCardsClick(Sender: TObject);
begin
  btnOK.Enabled:= lstCards.ItemIndex > -1;
end;

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

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

procedure TFormSearchCard.SearchCard(Keyword: string);
var
  list: TFPGList<TCard>;
  i: Integer;
  item: TCardListItem;
begin
  if (keyword = '') then Exit;
  list := Cards.SearchCards(keyword);
  lstCards.Clear;
  lstCards.BeginUpdate;
  for i := 0 to list.Count - 1 do begin
    item := TCardListItem.Create(lstCards);
    item.CardName:= list[i].CardName;
    item.Id:= list[i].CardId;
  end;
  lstCards.EndUpdate;
  FreeCards(list);
  btnOK.Enabled:= False;
  // deselect all
  lstCards.ItemIndex:= -1;
  // save history
  SaveHistory(keyword);
end;

procedure TFormSearchCard.SaveHistory(ALast: string);
var
  Arr: TStringArray;
  i: Integer;
begin
  Arr := TConfigReader.GetSearchHistory();
  for i := 0 to 9 do begin
    if Arr[i] = ALast then begin
      Exit;
    end;
  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];
  TConfigReader.SetSearchHistory(Arr);
end;

procedure TFormSearchCard.btnOKClick(Sender: TObject);
begin
  FCardId := TCardListItem(lstCards.Items[lstCards.ItemIndex]).Id;
  self.ModalResult:= mrOK;
end;

end.

