#ifndef OverbyteIcsBasicWebServer1H
#define OverbyteIcsBasicWebServer1H

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

#include "OverbyteIcsTypes.hpp"
#include "OverbyteIcsSslBase.hpp"
#include "OverbyteIcsWndControl.hpp"
#include "OverbyteIcsWSocket.hpp"
#include "OverbyteIcsWSocketS.hpp"
#include "OverbyteIcsHttpSrv.hpp"
#include "OverbyteIcsHttpAppServer.hpp"
#include "OverbyteIcsWebSocketCli.hpp"
#include "OverbyteIcsWebSocketSrv.hpp"

class TMyHttpConnection : public THttpWSSrvConn
{
    typedef THttpWSSrvConn inherited;
public:
    __int64 CStartTick = 0;
    __int64 CLastRead = 0;
    __int64 CLastWrite = 0;
    String CPeerHostName;
    using THttpConnection::AuthGetMethod;
    using THttpConnection::Answer404;
    __fastcall virtual TMyHttpConnection(TComponent *AOwner);
    void __fastcall BgExceptionEvent(TObject *Sender, Exception *E, bool &CanClose);
};

class TWebServerForm : public TForm
{
__published:
    TPanel *ToolsPanel;
    TButton *StartButton;
    TButton *StopButton;
    TCheckBox *DisplayHeaderCheckBox;
    TMemo *DisplayMemo;
    TSslHttpAppSrv *SslHttpAppSrv;
    void __fastcall SslHttpAppSrvAfterAnswer(TObject *Sender, TObject *Client);
    void __fastcall SslHttpAppSrvAuthGetPassword(TObject *Sender, TObject *Client, String &Password);
    void __fastcall SslHttpAppSrvAuthGetType(TObject *Sender, TObject *Client);
    void __fastcall SslHttpAppSrvAuthResult(TObject *Sender, TObject *Client, bool Success);
    void __fastcall SslHttpAppSrvBeforeProcessRequest(TObject *Sender, TObject *Client);
    void __fastcall SslHttpAppSrvBgException(TObject *Sender, Exception *E, bool &CanClose);
    void __fastcall SslHttpAppSrvClientConnect(TObject *Sender, TObject *Client, System::Word Error);
    void __fastcall SslHttpAppSrvDisplay(TObject *Sender, const String Msg);
    void __fastcall SslHttpAppSrvProcDocument(TObject *Sender, TObject *Client, THttpGetFlag &Flags);
    void __fastcall SslHttpAppSrvHttpRequestDone(TObject *Sender, TObject *Client);
    void __fastcall SslHttpAppSrvHttpRespHdr(TObject *Sender, TObject *Client, const String Header);
    void __fastcall SslHttpAppSrvSslHandshakeDone(TObject *Sender, System::Word ErrCode, TX509Base *PeerCert, bool &Disconnect);
    void __fastcall StartButtonClick(TObject *Sender);
    void __fastcall StopButtonClick(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall SslHttpAppSrvHttpAcceptFilter(TObject *Sender, TObject *Client, TIcsSessIpInfo &SessIpInfo, bool &Allowed);
    void __fastcall SslHttpAppSrvHttpCustomError(TObject *Sender, TObject *Client, int ErrNr, const String Info, String &Body);
public:
    void __fastcall ClientWSLogEvent(TObject *Sender, const String Msg);
    void __fastcall ClientWSHandshakeEvent(THttpWSSrvConn *Client, bool &OK, String &WelcomeMsg);
    void __fastcall ClientWSFrameRcvdEvent(THttpWSSrvConn *Client, const String APacket, TWebSocketReceivedFrame *&AFrame);
    void __fastcall ClientWSFrameSentEvent(THttpWSSrvConn *Client, TWebSocketOutgoingFrame *&AFrame);
    void __fastcall ClientWSDisconnectedEvent(TObject *Sender);
    void __fastcall ClientWSReadyEvent(THttpWSSrvConn *Client);
    void Display(const String &Msg);
    const String &ServerInfo() const { return FServerInfo; }
    __fastcall TWebServerForm(TComponent *Owner);
    __fastcall virtual ~TWebServerForm();
private:
    bool FFinalized = false;
    String FDataDir;
    String FSrvCompName;
    String FServerInfo;
    void __fastcall ClientBgException(TObject *Sender, Exception *E, bool &CanClose);
    void CreateVirtualDocument_Demo(TMyHttpConnection *ClientCnx, THttpGetFlag &Flags);
    String BuildDemoURIs();
};

extern PACKAGE TWebServerForm *WebServerForm;

#endif
