#pragma once
#define DEBUGLOG_DEFAULT_LOG_LEVEL_INFO

// System includes
#include <Arduino.h>
#include <DebugLog.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include <filesystem>
#include <FS.h>

class WebPage
{
    const uint8_t m_port = 80;
    fs::FS &m_filesystem;
    AsyncWebServer m_webserver;
    AsyncWebSocket m_websocket;
    bool m_upload_failed = false;
    fs::File m_upload_file;

public:
    WebPage(const uint8_t port, fs::FS &filesystem);
    ~WebPage();

    void sendWsData(const String &data);

private:
    void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
              AwsEventType type, void *arg, uint8_t *data, size_t len);

    void onUploadRequest(AsyncWebServerRequest *request);
    void onUploadHandler(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final);

    void onStart(AsyncWebServerRequest *request);
    void onStop(AsyncWebServerRequest *request);
    void onDownload(AsyncWebServerRequest *request);

};
