#ifndef OverbyteIcsSimpleSslServer1H
#define OverbyteIcsSimpleSslServer1H

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

#include "OverbyteIcsWSocket.hpp"
#include "OverbyteIcsWSocketS.hpp"
#include "OverbyteIcsWndControl.hpp"
#include "OverbyteIcsSslBase.hpp"
#include "OverbyteIcsTypes.hpp"

#include <memory>

const int WM_SSL_NOT_TRUSTED = WM_USER + 1;

class TTcpSrvClient : public TSslWSocketClient
{
public:
    String RcvdLine;
    TDateTime ConnectTime;
    __fastcall virtual TTcpSrvClient(TComponent *AOwner) : TSslWSocketClient(AOwner) {}
};

class TSimpleSslServerForm : public TForm
{
__published:
    TPanel *ToolsPanel;
    TMemo *DisplayMemo;
    TSslWSocketServer *SslWSocketServer1;
    TButton *StartButton;
    TLabel *Label1;
    TEdit *PortEdit;
    TLabel *Label3;
    TEdit *CertFileEdit;
    TLabel *Label7;
    TEdit *CAFileEdit;
    TLabel *Label6;
    TEdit *PrivKeyFileEdit;
    TLabel *Label4;
    TEdit *PassPhraseEdit;
    TLabel *Label2;
    TEdit *CAPathEdit;
    TCheckBox *VerifyPeerCheckBox;
    TButton *StopButton;
    TLabel *Label9;
    TLabel *Label8;
    TEdit *AcceptableHostsEdit;
    TSslContext *SslContext1;
    void __fastcall FormShow(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall StartButtonClick(TObject *Sender);
    void __fastcall SslWSocketServer1ClientConnect(TObject *Sender, TWSocketClient *Client, WORD Error);
    void __fastcall StopButtonClick(TObject *Sender);
    void __fastcall FormDestroy(TObject *Sender);
    void __fastcall ClientVerifyPeer(TObject *Sender, int &Ok, TX509Base *Cert);
    void __fastcall SslWSocketServer1SslHandshakeDone(TObject *Sender, WORD ErrCode, TX509Base *PeerCert, bool &Disconnect);
    void __fastcall SslWSocketServer1DebugDisplay(TObject *Sender, const String Msg);

private:
    String FIniFileName;
    bool FInitialized;
    std::unique_ptr<TStringList> FTrustedList;
    String FNotTrusted;
    void __fastcall ClientDataAvailable(TObject *Sender, WORD Error);
    void __fastcall ProcessData(TTcpSrvClient *Client);
    void __fastcall ClientBgException(TObject *Sender, Exception *E, bool &CanClose);
    void __fastcall ClientLineLimitExceeded(TObject *Sender, int Cnt, bool &ClearData);
    void __fastcall WMSslNotTrusted(TMessage &Msg);

public:
    __fastcall TSimpleSslServerForm(TComponent *Owner);
    __fastcall ~TSimpleSslServerForm();
    void __fastcall Display(const String &Msg);

BEGIN_MESSAGE_MAP
    VCL_MESSAGE_HANDLER(WM_SSL_NOT_TRUSTED, TMessage, WMSslNotTrusted)
END_MESSAGE_MAP(TForm)
};

extern PACKAGE TSimpleSslServerForm *SimpleSslServerForm;

#endif
