unit TestMainForm;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,
  Vcl.ExtCtrls;

type
  TMainForm = class(TForm)
    mmInfo: TMemo;
    btStart: TButton;
    btStop: TButton;
    btUpdateSphinx: TButton;
    btCreateUser: TButton;
    QuitButton: TButton;
    procedure btStartClick(Sender: TObject);
    procedure btStopClick(Sender: TObject);
    procedure btUpdateSphinxClick(Sender: TObject);
    procedure btCreateUserClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure QuitButtonClick(Sender: TObject);
  strict private
    FCanClose: Boolean;
    procedure UpdateGUI;
    procedure DoStart;
    procedure DoStop;
    procedure WriteLog(const Value: string);
  end;

var
  MainForm: TMainForm;

implementation

uses
  System.IOUtils,
  TestServer.Module,
  TestSphinx.Module,
  Test.DistributionConfig;

{$R *.dfm}

resourcestring
  SStatusStopped = 'Status: Stopped.';
  SStatusRunning = 'Status: Running.';
  SServerStarted = 'Server started.';
  SServerStopped = 'Server stopped.';

procedure TMainForm.btCreateUserClick(Sender: TObject);
var
  sUserId: string;
begin
  sUserId := SphinxModule.CreateDefaultUser('testuser', 'test@example.com', 'TestPass1!', 'Test', 'User');
  WriteLog('Test user ensured. Id=' + sUserId + '  (email test@example.com / password TestPass1!)');
end;

procedure TMainForm.btStartClick(Sender: TObject);
begin
  DoStart;
end;

procedure TMainForm.btStopClick(Sender: TObject);
begin
  DoStop;
end;

procedure TMainForm.btUpdateSphinxClick(Sender: TObject);
begin
  if not ServerContainer.IsRemoteDatabase then
    if not DirectoryExists(ServerContainer.LocalDatabasePath) then
      TDirectory.CreateDirectory(ServerContainer.LocalDatabasePath);

  Screen.Cursor := crHourGlass;
  try
    WriteLog('Sphinx schema update started…');
    SphinxModule.UpdateSphinx;
    WriteLog('Sphinx schema update completed.');
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TMainForm.DoStart;
begin
  ServerContainer.StartServer;
  UpdateGUI;
  WriteLog(SServerStarted);
end;

procedure TMainForm.DoStop;
begin
  ServerContainer.StopServer;
  UpdateGUI;
  WriteLog(SServerStopped);
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if not FCanClose then
    CanClose := False
  else if ServerContainer.MainDispatcher.Active then
    DoStop;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FCanClose := False;
  WriteLog('SphinxLogoutTest server — built-in settings in Shared\Test.DistributionConfig.pas');
  WriteLog('XData/Swagger: ' + ServerContainer.XDataServer.BaseUrl);
  WriteLog('Sphinx auth: ' + SphinxModule.SphinxServer1.BaseUrl);
  WriteLog('Database file: ' + Dist_DatabaseFile);
  WriteLog('Login app folder: ' + Dist_LoginAppFolder);
  UpdateGUI;
end;

procedure TMainForm.QuitButtonClick(Sender: TObject);
begin
  FCanClose := True;
  Close;
end;

procedure TMainForm.UpdateGUI;
begin
  btStart.Enabled := not ServerContainer.MainDispatcher.Active;
  btStop.Enabled := not btStart.Enabled;
  btUpdateSphinx.Enabled := btStart.Enabled;
  btCreateUser.Enabled := not btStart.Enabled;
end;

procedure TMainForm.WriteLog(const Value: string);
begin
  mmInfo.Lines.Add(Value);
end;

end.
