unit Main;
//Authors:
//Elias Sarraf
//Mac Stephens
//Cameron Hayes
interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Winapi.ShellApi,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, System.Generics.Collections, System.IniFiles,
  Auth.Service, Auth.Server.Module, Api.Service, Api.Server.Module, App.Server.Module,
  ExeInfo;

type
  TFMain = class(TForm)
    memoInfo: TMemo;
    btnApiSwaggerUI: TButton;
    btnExit: TButton;
    initTimer: TTimer;
    btnAuthSwaggerUI: TButton;
    ExeInfo1: TExeInfo;
    procedure btnAuthSwaggerUIClick(Sender: TObject);
    procedure btnApiSwaggerUIClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure ContactFormData(AText: String);
    procedure initTimerTimer(Sender: TObject);

  strict private
    procedure StartServers;
  end;

var
  FMain: TFMain;

implementation

uses
  Common.Logging,
  Common.Ini,
  Common.Config,
  Sparkle.Utils,
  Api.Database;

{$R *.dfm}

{ TMainForm }

procedure TFMain.ContactFormData(AText: String);
begin
  if memoInfo.CanFocus then
    TThread.Queue(nil, procedure
    begin
      memoInfo.Lines.Add(AText);
    end)
  else
    TThread.Synchronize(nil, procedure
    begin
      memoInfo.Lines.Add(AText);
    end);
end;

procedure TFMain.btnExitClick(Sender: TObject);
begin
  Close;
end;

procedure TFMain.btnAuthSwaggerUIClick(Sender: TObject);
begin
  ShellExecute(Handle, 'open', PChar(TSparkleUtils.CombineUrlFast(AuthServerModule.XDataServer.BaseUrl, 'swaggerui')), nil, nil, SW_SHOWNORMAL);
end;

procedure TFMain.btnApiSwaggerUIClick(Sender: TObject);
begin
  ShellExecute(Handle, 'open', PChar(TSparkleUtils.CombineUrlFast(ApiServerModule.XDataServer.BaseUrl, 'swaggerui')), nil, nil, SW_SHOWNORMAL);
end;

procedure TFMain.initTimerTimer(Sender: TObject);
begin
  initTimer.Enabled := False;
  Caption := Caption + ' ver ' + ExeInfo1.FileVersion;
  LoadServerConfig;
  StartServers;
end;

procedure TFMain.StartServers;
var
  iniFile: TIniFile;
  iniStr: string;
  bStop: boolean;
  devMode: boolean;
begin
  bStop := False;

  iniFile := TIniFile.Create( ChangeFileExt(Application.ExeName, '.ini') );
  try
    devMode := iniFile.ReadBool( 'Settings', 'devMode', True );
    Logger.Log( 1, 'devMode: ' + BoolToStr(devMode, True) );

    iniStr := iniFile.ReadString( 'Settings', 'webClientVersion', '' );
    if iniStr.IsEmpty then
    begin
      Logger.Log( 1, '--Settings->WebClientVersion: Entry not found - ERROR: ini entry required!!!');
      bStop := True;
    end
    else
      Logger.Log( 1, '--Settings->WebClientVersion: ' + iniStr );

    Logger.Log(1, '--- Database ---');
    iniStr := IniFile.ReadString( 'Database', 'Server', '' );
    if iniStr.IsEmpty then
    begin
      Logger.Log( 1, '----Database->Server: Entry not found - ERROR: ini entry required!!!' );
      bStop := True;
    end
    else
      Logger.Log( 1, '----Database->Server: ' + iniStr );

    iniStr := iniFile.ReadString('Database', 'Database', '');
    if iniStr.IsEmpty then
      Logger.Log( 1, '----Database->Database: ini entry not found - default: kg_order_entry' )
    else
      Logger.Log( 1, '----Database->Database: ini entry: ' + iniStr );

    iniStr := iniFile.ReadString('Database', 'Username', '');
    if iniStr.IsEmpty then
      Logger.Log( 1, '----Database->Username: Entry not found - default: root' )
    else
      Logger.Log( 1, '----Database->Username: ' + iniStr );

    iniStr := iniFile.ReadString('Database', 'Password', '');
    if iniStr.IsEmpty then
      Logger.Log( 1, '----Database->Password: Entry not found - default: xxxxxx' )
    else
      Logger.Log( 1, '----Database->Password: ini entry: xxxxxxxx' );

    Logger.Log( 1, '' );
  finally
    IniFile.Free;
  end;

  if bStop then
  begin
    Logger.Log( 1, 'ini configuration error: Existing program!' );
    if devMode then
      MessageDlgPos( 'ini configuration error: Existing program!', mtConfirmation, [mbOk], 0, 250, 350 );
    Close();
  end;

  AuthServerModule := TAuthServerModule.Create(Self);
  AuthServerModule.StartAuthServer(serverConfig.url, AUTH_MODEL);

  ApiServerModule := TApiServerModule.Create( Self );
  ApiServerModule.StartApiServer(serverConfig.url, API_MODEL);

  AppServerModule := TAppServerModule.Create(Self);
  AppServerModule.StartAppServer( serverConfig.url );
  Logger.Log(1, 'Exe=' + Application.ExeName);
end;


end.
