program hdcDZApi;

{$IFDEF DEBUG}
  {$APPTYPE CONSOLE}
{$ENDIF}

{$I GlobalDefs.inc}

uses
  MSHeap,
  Vcl.SvcMgr,
  System.SysUtils,
  Winapi.Windows,
  System.Classes,

  Web.WebReq,

  MVCFramework.Middleware.Swagger,
  MVCFramework.Swagger.Commons,
  uWinService in 'uWinService.pas' {HDCApiService: TService},
  uWebMod in 'uWebMod.pas' {WebModule1: TWebModule},
  uDataMod in 'uDataMod.pas' {datMod: TDataModule},

  helTabsBIDs,

  uCommons in 'uCommons.pas',
  uCtrlBase in 'uCtrlBase.pas',
  uCtrlZamestnanci in 'uCtrlZamestnanci.pas',
  uCtrlKmenZbozi in 'uCtrlKmenZbozi.pas',
  uCtrlObehZbozi in 'uCtrlObehZbozi.pas',
  uCtrlVyroba in 'uCtrlVyroba.pas',
  uCtrlQMS in 'uCtrlQMS.pas',
  uCtrlOrganizace in 'uCtrlOrganizace.pas',
  uCtrlObecne in 'uCtrlObecne.pas',

  uHeOObj_Base in 'uHeOObj_Base.pas',
  uHeoObj_Vyroba in 'uHeoObj_Vyroba.pas',
  uHeoObj_QMS in 'uHeoObj_QMS.pas',

  uSvc_Base in 'uSvc_Base.pas',
  uSvc_ObehZbozi in 'uSvc_ObehZbozi.pas',
  uSvc_KmenZbozi in 'uSvc_KmenZbozi.pas',
  uSvc_Vyroba in 'uSvc_Vyroba.pas',
  uSvc_Organizace in 'uSvc_Organizace.pas',
  uSvc_Zamestnanci in 'uSvc_Zamestnanci.pas',
  uSvc_Obecne in 'uSvc_Obecne.pas',
  uSvc_QMS in 'uSvc_QMS.pas'

{$IFDEF CUSTOM_CTRL}
  ,uHeOObj_Custom in 'uHeOObj_Custom.pas'
  ,uCtrlCustom in 'uCtrlCustom.pas'
  ,uSvcCustom in 'uSvcCustom.pas'
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Kdynium}
  ,uHeOObj_Custom in '_custom\Kdynium\uHeOObj_Custom.pas'
  ,uCtrlCustom in '_custom\Kdynium\uCtrlCustom.pas'
  ,uSvcCustom in '_custom\Kdynium\uSvcCustom.pas'
{$ENDIF}

{$IFDEF CUSTOM_CTRL_GatemaSD}
  ,uHeOObj_CustomSDG in '_custom\GatemaSD\uHeOObj_CustomSDG.pas'
  ,uCtrlCustomSDG in '_custom\GatemaSD\uCtrlCustomSDG.pas'
  ,uSvcCustomSDG in '_custom\GatemaSD\uSvcCustomSDG.pas'
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Rootvin}
  ,uHeOObj_Custom in '_custom\Rootvin\uHeOObj_Custom.pas'
  ,uCtrlCustom    in '_custom\Rootvin\uCtrlCustom.pas' {TRTNController: TBaseController},
  uSvcCustom     in '_custom\Rootvin\uSvcCustom.pas'
  {$R '_custom\Rootvin\zdroje.RES'}
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Koramex}
   ,uHeOObj_Custom in '_custom\Koramex\uHeOObj_Custom.pas'
   ,uCtrlCustom in '_custom\Koramex\uCtrlCustom.pas'
   ,uSvcCustom in '_custom\Koramex\uSvcCustom.pas'
{$ENDIF}

{$IFDEF CUSTOM_CTRL_INCOSystems}
   ,uHeOObj_Custom in '_custom\INCOSystems\uHeOObj_Custom.pas'
   ,uCtrlCustom in '_custom\INCOSystems\uCtrlCustom.pas'
   ,uSvcCustom in '_custom\INCOSystems\uSvcCustom.pas'
  {$R .\_custom\INCOSystems\zdroje.RES}
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Gornicky}
   ,uHeOObj_Custom in '_custom\Gornicky\uHeOObj_Custom.pas'
   ,uCtrlCustom in '_custom\Gornicky\uCtrlCustom.pas'
   ,uSvcCustom in '_custom\Gornicky\uSvcCustom.pas'
  {$R '_custom\Gornicky\zdroje.RES'}
{$ENDIF}

{$IFDEF CUSTOM_CTRL_EMPolar}
   ,uHeOObj_Custom in '_custom\EMPolar\uHeOObj_Custom.pas'
   ,uCtrlCustom in '_custom\EMPolar\uCtrlCustom.pas'
   ,uSvcCustom in '_custom\EMPolar\uSvcCustom.pas'
  {$R '_custom\EMPolar\zdroje.RES'}
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Westra}
   ,uHeOObj_Custom in '_custom\Westra\uHeOObj_Custom.pas'
   ,uCtrlCustom in '_custom\Westra\uCtrlCustom.pas'
   ,uSvcCustom in '_custom\Westra\uSvcCustom.pas'
  {$R '_custom\Westra\zdroje.RES'}
{$ENDIF}
  ;


{$R *.RES}
{$R sqlZdroje.RES}


{$IFDEF CUSTOM_CTRL_Rootvin}
  {$R .\_custom\Rootvin\zdroje.RES}
{$ENDIF}

{$IFDEF CUSTOM_CTRL_INCOSystems}
  {$R .\_custom\INCOSystems\zdroje.RES}
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Gornicky}
  {$R .\_custom\Gornicky\zdroje.RES}
{$ENDIF}


procedure SpustitJakoPrg;
var Event      : TInputrecord;
    EventsRead : DWORD;
    Done       : Boolean;
 begin
  try
    var HDCDZApiService: THDCDZApiService;
    var started: boolean;
    var stopped: boolean;

    Application.Initialize;
    Application.CreateForm (TdatMod, datMod);
//    Application.CreateForm(TWebModule1, WebModule1);

  // In debug mode the server acts as a console application.
    // Create the TService descendant manually.

//    uWinService.UninstallMode:= true;
    HDCDZApiService:= THDCDZApiService.Create (nil);

    WriteLn (HDCDZApiService.Name + ' DEBUG mode');

    HDCDZApiService.ServiceAfterInstall (HDCDZApiService);


    if (WebRequestHandler<>nil) then
     WebRequestHandler.WebModuleClass := WebModuleClass;

    // Simulate service start.
    HDCDZApiService.ServiceStart (HDCDZApiService, started);
    // Keep the console box running (ServerContainer1 code runs in the background)
    WriteLn('Press ENTER to exit.');
    ReadLn;
    WriteLn('Ukoncuji thready...');
    HDCDZApiService.ServiceStop (HDCDZApiService, stopped);

    HDCDZApiService.ServiceBeforeUninstall (HDCDZApiService);
    HDCDZApiService.ServiceAfterUnInstall (HDCDZApiService);

    // On exit, destroy the service object.
    if (stopped) then
     FreeAndNil (HDCDZApiService);
  except
    on E: Exception do
    begin
      Writeln(E.ClassName, ': ', E.Message);
{$IFDEF DEBUG}
    Done:= False;
    repeat
        ReadConsoleInput(GetStdhandle(STD_INPUT_HANDLE), Event, 1, EventsRead);
        if Event.Eventtype = key_Event then
         begin
            if Event.Event.KeyEvent.bKeyDown then
             begin
                Done:= (Event.Event.KeyEvent.wVirtualKeyCode = VK_ESCAPE);
{
                if (not Done) and (Event.Event.KeyEvent.AsciiChar <> #0) then
                     WriteLn('You typed: ', Event.Event.KeyEvent.AsciiChar);
}
             end;
         end;
    until Done;
{$ENDIF}
//      WriteLn('Press ENTER to exit.');
//      ReadLn;
    end;
  end;
 end;




begin
   ReportMemoryLeaksOnShutdown:= true;

{$IFDEF DEBUG}
{
   if not Application.DelayInitialize or Application.Installing then
    Application.Initialize;

   Application.CreateForm(THDCDZApiService, HDCDZApiService);
   Application.CreateForm(TdatMod, datMod);
// thread čekající na ENTER
   TThread.CreateAnonymousThread(
    procedure
     var Stopped: Boolean;
    begin
      Writeln('Stiskem ENTER ukončete...');
      Readln;

      TThread.Queue(nil,
        procedure
        begin
          HDCDZApiService.ServiceStop(HDCDZApiService, Stopped);
        end);
    end
   ).Start;
   Application.Run;
}
   SpustitJakoPrg;
{$ELSE}
  if (FindCmdLineSwitch('dbg', true)) then
    SpustitJakoPrg
   else
    begin

  // Windows 2003 Server requires StartServiceCtrlDispatcher to be
  // called before CoRegisterClassObject, which can be called indirectly
  // by Application.Initialize. TServiceApplication.DelayInitialize allows
  // Application.Initialize to be called from TService.Main (after
  // StartServiceCtrlDispatcher has been called).
  //
  // Delayed initialization of the Application object may affect
  // events which then occur prior to initialization, such as
  // TService.OnCreate. It is only recommended if the ServiceApplication
  // registers a class object with OLE and is intended for use with
  // Windows 2003 Server.
  //
  // Application.DelayInitialize := True;
  //
     if not Application.DelayInitialize or Application.Installing then
       Application.Initialize;
     Application.CreateForm(THDCDZApiService, HDCDZApiService);
     Application.CreateForm(TdatMod, datMod);
     Application.Run;
    end;

{$ENDIF}

end.
