unit fRevards;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  System.StrUtils,
  System.Rtti, FMX.Grid.Style, FMX.Grid, FMX.ScrollBox, uRecords,
  System.Generics.Collections,
  FMX.Controls.Presentation, FMX.EditBox, FMX.NumberBox, FMX.Edit,
  FMX.ComboEdit;

type
  TfrRevards = class(TFrame)
    Label1: TLabel;
    StringGrid1: TStringGrid;
    StringColumn1: TStringColumn;
    StringColumn2: TStringColumn;
    StringColumn3: TStringColumn;
    StringColumn4: TStringColumn;
    Label34: TLabel;
    ceCustomRevardTitle: TComboEdit;
    edtCustomRevardPrompt: TEdit;
    nbCustomRevardCost: TNumberBox;
    Label35: TLabel;
    Label2: TLabel;
    btnDelCustomRewards: TButton;
    btnAddCustomRewards: TButton;
    cbUserInput: TCheckBox;
    procedure StringGrid1CellClick(const Column: TColumn; const Row: Integer);
    procedure btnDelCustomRewardsClick(Sender: TObject);
    procedure btnAddCustomRewardsClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    CustomRewards: TList<TCustomRevards>;
    listRevards: tarray<TReward>;
    procedure LoadCustomRevards;
    procedure UpdateGrid;
  end;

implementation

{$R *.fmx}

uses ugeneral;

procedure TfrRevards.btnAddCustomRewardsClick(Sender: TObject);
var
  cr: TCustomRevards;
begin
  cr := ttw_API.createCustomReward(ceCustomRevardTitle.Text,
    inttostr(round(nbCustomRevardCost.Value)), edtCustomRevardPrompt.Text,
    cbUserInput.IsChecked);
  CustomRewards.Add(cr);
  UpdateGrid;
end;

procedure TfrRevards.btnDelCustomRewardsClick(Sender: TObject);
var
  cr: TCustomRevards;
  RowIndex: Integer;
begin
  RowIndex := StringGrid1.Row;
  if (RowIndex < 0) or (RowIndex >= CustomRewards.Count) then
    Exit;

  for cr in CustomRewards do
  begin
    if cr.title = StringGrid1.Cells[0, RowIndex] then
    begin
      ttw_API.deleteCustomReward(cr.id);
      CustomRewards.Delete(RowIndex);
      Break;
    end;
  end;
  UpdateGrid;
end;

procedure TfrRevards.LoadCustomRevards;
var
  cr: TCustomRevards;
begin
  CustomRewards.Clear;
  TTW_Bot.frEvents1.cbRevards.Items.Clear;
  ceCustomRevardTitle.Items.Clear;
  TTW_Bot.toLog('fRevards', 'LoadCustomRevards', 'Запрос наград с сервера', 3);
  ttw_API.getCustomReward(CustomRewards);

  for cr in CustomRewards do
  begin
    TTW_Bot.frEvents1.cbRevards.Items.Add(cr.title);
    ceCustomRevardTitle.Items.Add(cr.title);
  end;
  UpdateGrid;
end;

procedure TfrRevards.StringGrid1CellClick(const Column: TColumn;
  const Row: Integer);
begin
  ceCustomRevardTitle.ItemIndex := ceCustomRevardTitle.Items.IndexOf
    (StringGrid1.Cells[0, Row]);
  edtCustomRevardPrompt.Text := StringGrid1.Cells[2, Row];
  cbUserInput.IsChecked := StringGrid1.Cells[1, Row] = 'Да';
  nbCustomRevardCost.Value := strtoint(StringGrid1.Cells[3, Row]);
end;

procedure TfrRevards.UpdateGrid;
var
  cr: TCustomRevards;
  i: Integer;
begin
  StringGrid1.RowCount := 0;
  for cr in CustomRewards do
  begin
    StringGrid1.RowCount := StringGrid1.RowCount + 1;
    StringGrid1.Cells[0, StringGrid1.RowCount - 1] := cr.title;
    StringGrid1.Cells[1, StringGrid1.RowCount - 1] :=
      ifthen(cr.is_user_input_required, 'Да', 'Нет');
    StringGrid1.Cells[2, StringGrid1.RowCount - 1] := cr.promt;
    StringGrid1.Cells[3, StringGrid1.RowCount - 1] := inttostr(cr.cost);
  end;
end;

end.
