#pragma once

#ifdef ESP_PLATFORM

#include <esp_http_client.h>
#include <Tactility/Logger.h>

namespace tt::network {

class EspHttpClient {

    const Logger logger = Logger("EspHttpClient");

    std::unique_ptr<esp_http_client_config_t> config = nullptr;
    esp_http_client_handle_t client = nullptr;
    bool isOpen = false;

public:

    ~EspHttpClient() {
        if (isOpen) {
            esp_http_client_close(client);
        }

        if (client != nullptr) {
            esp_http_client_cleanup(client);
        }
    }

    bool init(std::unique_ptr<esp_http_client_config_t> inConfig) {
        logger.info("init({})", inConfig->url);
        assert(this->config == nullptr);
        config = std::move(inConfig);
        client = esp_http_client_init(config.get());
        return client != nullptr;
    }

    bool open() {
        assert(client != nullptr);
        logger.info("open()");
        auto result = esp_http_client_open(client, 0);

        if (result != ESP_OK) {
            logger.error("open() failed: {}", esp_err_to_name(result));
            return false;
        }

        isOpen = true;
        return true;
    }

    bool fetchHeaders() const {
        assert(client != nullptr);
        logger.info("fetchHeaders()");
        return esp_http_client_fetch_headers(client) >= 0;
    }

    bool isStatusCodeOk() const {
        assert(client != nullptr);
        const auto status_code = getStatusCode();
        return status_code >= 200 && status_code < 300;
    }

    int getStatusCode() const {
        assert(client != nullptr);
        const auto status_code = esp_http_client_get_status_code(client);
        logger.info("Status code {}", status_code);
        return status_code;
    }

    int getContentLength() const {
        assert(client != nullptr);
        return esp_http_client_get_content_length(client);
    }

    int read(char* bytes, int size) const {
        assert(client != nullptr);
        logger.info("read({})", size);
        return esp_http_client_read(client, bytes, size);
    }

    int readResponse(char* bytes, int size) const {
        assert(client != nullptr);
        logger.info("readResponse({})", size);
        return esp_http_client_read_response(client, bytes, size);
    }

    bool close() {
        assert(client != nullptr);
        logger.info("close()");
        if (esp_http_client_close(client) == ESP_OK) {
            isOpen = false;
        }
        return !isOpen;
    }

    bool cleanup() {
        assert(client != nullptr);
        assert(!isOpen);
        logger.info("cleanup()");
        const auto result = esp_http_client_cleanup(client);
        client = nullptr;
        return result == ESP_OK;
    }
};

}

#endif