#pragma once

#include <Arduino.h>
#include <WebServer.h>
#include <freertos/semphr.h>
#include <vector>

#include "sdcardesp32cam.h"

class WebGallery
{
public:
    WebGallery();
    void begin(StorageSD &storage);
    void run();
    void notifyPhotoAdded(const char *filename);

private:
    enum class PhotoListResult
    {
        Ok,
        StorageBusy,
        StorageUnavailable,
        CacheUnavailable
    };

    WebServer server;
    StorageSD *storage = nullptr;
    SemaphoreHandle_t photoCacheMutex = nullptr;
    std::vector<uint32_t> cachedPhotoNumbers;
    bool photoCacheValid = false;

    bool connectToWifi();
    void handleGallery();
    void handleLogo();
    void handlePhotoList();
    void handlePhoto();
    void handleNotFound();
    PhotoListResult getPhotoNumbers(std::vector<uint32_t> &photoNumbers);
};
