#ifndef OverbyteIcsWebAppServerMainH
#define OverbyteIcsWebAppServerMainH

#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <Winapi.Messages.hpp>

#include "OverbyteIcsTypes.hpp"
#include "OverbyteIcsWndControl.hpp"
#include "OverbyteIcsWSocket.hpp"
#include "OverbyteIcsWebSession.hpp"
#include "OverbyteIcsHttpSrv.hpp"
#include "OverbyteIcsHttpAppServer.hpp"
#include "OverbyteIcsWebAppServerPages.h"

const unsigned WM_APPSTARTUP = WM_USER + 1;

class TAppHttpConnection : public THttpAppSrvConnection
{
    typedef THttpAppSrvConnection inherited;
public:
    __int64 CStartTick = 0;
    __int64 CLastRead = 0;
    __int64 CLastWrite = 0;
    __fastcall virtual TAppHttpConnection(TComponent *AOwner) : THttpAppSrvConnection(AOwner) {}
};

class TWebAppSrvForm : public TForm
{
__published:
    TPanel *ToolsPanel;
    TMemo *DisplayMemo;
    THttpAppSrv *HttpAppSrv1;
    TTimer *HousekeepingTimer;
    void __fastcall FormShow(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall HttpAppSrv1ClientConnect(TObject *Sender, TObject *Client, System::Word Error);
    void __fastcall HttpAppSrv1GetDocument(TObject *Sender, TObject *Client, THttpGetFlag &Flags);
    void __fastcall HttpAppSrv1ServerStarted(TObject *Sender);
    void __fastcall HttpAppSrv1ServerStopped(TObject *Sender);
    void __fastcall HttpAppSrv1DeleteSession(TObject *Sender, TIcsWebSession *Session);
    void __fastcall HousekeepingTimerTimer(TObject *Sender);
    void __fastcall HttpAppSrv1AfterAnswer(TObject *Sender, TObject *Client);
    void __fastcall HttpAppSrv1BeforeProcessRequest(TObject *Sender, TObject *Client);
    void __fastcall HttpAppSrv1VirtualException(TObject *Sender, Exception *E, THttpMethod Method, const String Path);
    void __fastcall HttpAppSrv1BgException(TObject *Sender, Exception *E, bool &CanClose);
    void __fastcall HttpAppSrv1Display(TObject *Sender, const String Msg);
    void __fastcall HttpAppSrv1HttpAcceptFilter(TObject *Sender, TObject *Client, TIcsSessIpInfo &SessIpInfo, bool &Allowed);
public:
    void Display(const String &Msg);
    void __fastcall DisplayHandler(TObject *Sender, const String Msg);
    void __fastcall HttpAppSrvClientBgException(TObject *Sender, Exception *E, bool &CanClose);
    __fastcall TWebAppSrvForm(TComponent *Owner);
    __fastcall virtual ~TWebAppSrvForm();
private:
    TWebAppSrvDataModule FPagesData;
    String FIniFileName;
    bool FInitialized = false;
    String FDataDir;
    String FSessionFile;
    String FSrvCompName;
    void __fastcall WMAppStartup(TMessage &Msg);

    BEGIN_MESSAGE_MAP
        VCL_MESSAGE_HANDLER(WM_APPSTARTUP, TMessage, WMAppStartup)
    END_MESSAGE_MAP(TForm)
};

extern PACKAGE TWebAppSrvForm *WebAppSrvForm;

#endif
