unit fCommands;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, StrUtils,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  System.Rtti, FMX.Grid.Style, FMX.Memo.Types, FMX.Grid, FMX.Layouts, uRecords,
  FMX.ListBox, FMX.Memo, FMX.Edit, FMX.Controls.Presentation, FMX.ScrollBox,
  fSimpleGrid, fContruct, fGroupsRequest;

type
  TfrCommands = class(TFrame)
    sgCommands: TStringGrid;
    scCommand: TStringColumn;
    scResponse: TStringColumn;
    GroupBox1: TGroupBox;
    GroupBox9: TGroupBox;
    GroupBox8: TGroupBox;
    edtRandomName: TEdit;
    edtOt: TEdit;
    edtTo: TEdit;
    btnRandAdd: TButton;
    btnRandDel: TButton;
    sgRandomInt: TStringGrid;
    scRIntName: TStringColumn;
    IntegerColumn1: TIntegerColumn;
    scRIntTo: TIntegerColumn;
    GroupBox11: TGroupBox;
    GroupBox24: TGroupBox;
    OpenDialog1: TOpenDialog;
    GroupBox2: TGroupBox;
    frsgSounds: TfrSimpleGrid;
    frsgFiles: TfrSimpleGrid;
    frsgNeiro: TfrSimpleGrid;
    frContruct1: TfrContruct;
    frGroupsRequest1: TfrGroupsRequest;
    procedure btnRandAddClick(Sender: TObject);
    procedure btnRandDelClick(Sender: TObject);
    procedure frsgSoundsbtnSoundDelClick(Sender: TObject);
    procedure frsgSoundssgCellDblClick(const Column: TColumn;
      const Row: Integer);
    procedure frsgFilessgCellDblClick(const Column: TColumn;
      const Row: Integer);
    procedure frsgNeirosgCellDblClick(const Column: TColumn;
      const Row: Integer);
    procedure frContruct1btnAddCommandClick(Sender: TObject);
    procedure frContruct1btnEditCommandClick(Sender: TObject);
    procedure frContruct1btnRmCommandClick(Sender: TObject);
    procedure sgCommandsCellClick(const Column: TColumn; const Row: Integer);
    procedure sgRandomIntCellDblClick(const Column: TColumn;
      const Row: Integer);
    procedure frGroupsRequest1lbRandomGroupDblClick(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
    RandomCounters: TArray<TRandomCounters>;
    listSounds: TArray<TListCommands>;
    listFiles: TArray<TListCommands>;
    listNeiro: TArray<TListCommands>;
    listCommands: TArray<TListCommands>;

    procedure UpdateGridFromArray;
  end;

implementation

{$R *.fmx}

uses uGeneral;

procedure TfrCommands.btnRandDelClick(Sender: TObject);
var
  i, RowIndex: Integer;
begin
  RowIndex := sgRandomInt.Row;
  if (RowIndex < 0) or (RowIndex > High(RandomCounters)) then
    Exit;

  // Сдвигаем элементы массива
  for i := RowIndex to High(RandomCounters) - 1 do
    RandomCounters[i] := RandomCounters[i + 1];
  SetLength(RandomCounters, Length(RandomCounters) - 1);
  UpdateGridFromArray;
  DB.SaveRecordArray<TRandomCounters>('RandomCounters', RandomCounters);
end;

procedure TfrCommands.frContruct1btnAddCommandClick(Sender: TObject);
 var
  sCommand: string;
  NewRec: TListCommands;
begin
  sCommand := frContruct1.edtCommand.text;
  if (sCommand = '') or (sCommand = '!') or (frContruct1.mResponse.text = '') then
    exit;
  if (sCommand <> '') and (sCommand[1] = '!') then
  begin
    Delete(sCommand, 1, 1);
  end;

  NewRec.R1 := sCommand;
  NewRec.R2 := frContruct1.mResponse.text;
  SetLength(listCommands, Length(listCommands) + 1);
  listCommands[High(listCommands)] := NewRec;
  UpdateGridFromArray;
  DB.SaveRecordArray<TListCommands>('listCommands', listCommands);
end;

procedure TfrCommands.frContruct1btnEditCommandClick(Sender: TObject);
var RowIndex: integer; sCommand:string;
begin
   RowIndex := sgCommands.Row;

    sCommand := frContruct1.edtCommand.text;
  if (sCommand = '') or (sCommand = '!') or (frContruct1.mResponse.text = '') then
    exit;
  if (sCommand <> '') and (sCommand[1] = '!') then
  begin
    Delete(sCommand, 1, 1);
  end;

  if (RowIndex < 0) or (RowIndex > High(listCommands)) then
    exit;
  listCommands[RowIndex].R1 := sCommand;
  listCommands[RowIndex].R2 := frContruct1.mResponse.text;

  UpdateGridFromArray;
  DB.SaveRecordArray<TListCommands>('listCommands', listCommands);
end;

procedure TfrCommands.frContruct1btnRmCommandClick(Sender: TObject);
var
  i, RowIndex: Integer;
begin
  RowIndex := sgCommands.Row;
  if (RowIndex < 0) or (RowIndex > High(listCommands)) then
    exit;
  for i := RowIndex to High(listCommands) - 1 do
    listCommands[i] := listCommands[i + 1];
  SetLength(listCommands, Length(listCommands) - 1);
  UpdateGridFromArray;
  DB.SaveRecordArray<TListCommands>('listCommands', listCommands);

end;

procedure TfrCommands.frGroupsRequest1lbRandomGroupDblClick(Sender: TObject);
begin
frContruct1.mResponse.Lines.text := frContruct1.mResponse.Lines.text + ' {{' + frGroupsRequest1.lbRandomGroup.Items
    [frGroupsRequest1.lbRandomGroup.ItemIndex] + '}}';
end;

procedure TfrCommands.frsgFilessgCellDblClick(const Column: TColumn;
  const Row: Integer);
begin
  frContruct1.mResponse.Lines.text := frContruct1.mResponse.Lines.text + ' ' +
    frsgFiles.Hint + frsgFiles.sg.Cells[0, Row] + frsgFiles.Hint;
end;

procedure TfrCommands.frsgNeirosgCellDblClick(const Column: TColumn;
  const Row: Integer);
begin
  frContruct1.mResponse.Lines.text := frContruct1.mResponse.Lines.text + ' ' +
    frsgNeiro.Hint + frsgNeiro.sg.Cells[0, Row] + frsgNeiro.Hint;
end;

procedure TfrCommands.frsgSoundsbtnSoundDelClick(Sender: TObject);
var
  i, RowIndex: Integer;
begin
  RowIndex := frsgSounds.sg.Row;
  if (RowIndex < 0) or (RowIndex > High(listSounds)) then
    Exit;

  // Сдвигаем элементы массива
  for i := RowIndex to High(listSounds) - 1 do
    listSounds[i] := listSounds[i + 1];

  SetLength(listSounds, Length(listSounds) - 1);

  UpdateGridFromArray;
  DB.SaveRecordArray<TListCommands>('listSounds', listSounds);
end;

procedure TfrCommands.frsgSoundssgCellDblClick(const Column: TColumn;
  const Row: Integer);
begin
  frContruct1.mResponse.Lines.text := frContruct1.mResponse.Lines.text + ' ' +
    frsgSounds.Hint + frsgSounds.sg.Cells[0, Row] + frsgSounds.Hint;
end;

procedure TfrCommands.sgCommandsCellClick(const Column: TColumn;
  const Row: Integer);
begin
 frContruct1.edtCommand.text := sgCommands.Cells[0, Row];
 frContruct1.mResponse.text := sgCommands.Cells[1, Row];
end;

procedure TfrCommands.sgRandomIntCellDblClick(const Column: TColumn;
  const Row: Integer);
begin
   frContruct1.mResponse.Lines.text := frContruct1.mResponse.Lines.text + ' [[' + sgRandomInt.Cells
    [0, Row] + ']]';
end;

procedure TfrCommands.UpdateGridFromArray;
var
  i: Integer;
begin
  sgRandomInt.BeginUpdate;
  try
    sgRandomInt.RowCount := 0; // Сбрасываем строки (оставляем только заголовки)

    for i := 0 to High(RandomCounters) do
    begin
      sgRandomInt.RowCount := i +1;
      sgRandomInt.Cells[0, i] := RandomCounters[i].rndName;
      sgRandomInt.Cells[1, i] := IntToStr(RandomCounters[i].Ot);
      sgRandomInt.Cells[2, i] := IntToStr(RandomCounters[i].ToValue);
    end;
  finally
    sgRandomInt.EndUpdate;
  end;
  sgCommands.BeginUpdate;
  try
    sgCommands.RowCount := 0; // Сбрасываем строки (оставляем только заголовки)

    for i := 0 to High(listCommands) do
    begin
      sgCommands.RowCount := i+1 ;
      sgCommands.Cells[0, i] := listCommands[i].R1;
      sgCommands.Cells[1, i] := listCommands[i].R2;           //9067254178
    end;
  finally
    sgCommands.EndUpdate;
  end;
end;

procedure TfrCommands.btnRandAddClick(Sender: TObject);
var
  NewRec: TRandomCounters;
begin
  NewRec.rndName := edtRandomName.text;
  NewRec.Ot := StrToIntDef(edtOt.text, 0);
  NewRec.ToValue := StrToIntDef(edtTo.text, 100);

  SetLength(RandomCounters, Length(RandomCounters) + 1);
  RandomCounters[High(RandomCounters)] := NewRec;

  UpdateGridFromArray;
  // DB.SaveGridToTable('sgRandomInt', sgRandomInt);
  DB.SaveRecordArray<TRandomCounters>('RandomCounters', RandomCounters);
  edtRandomName.text := '';
  edtOt.text := '0';
  edtTo.text := '100';
end;

end.
