#include "http_client.h"
#include "esp_http_client.h"
#include "esp_tls.h"
#include "esp_log.h"

#include <sys/param.h>

#include <string.h>

#define MAX_HTTP_RECV_BUFFER 512
#define MAX_HTTP_OUTPUT_BUFFER 2048
static const char *TAG = "http_client";

esp_err_t _http_event_handler(esp_http_client_event_t *evt)
{
    static char *output_buffer; // Buffer to store response of http request from event handler
    static int output_len;      // Stores number of bytes read
    switch (evt->event_id)
    {
    case HTTP_EVENT_ERROR:
        ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
        break;
    case HTTP_EVENT_ON_CONNECTED:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
        break;
    case HTTP_EVENT_HEADER_SENT:
        ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
        break;
    case HTTP_EVENT_ON_HEADER:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
        break;
    case HTTP_EVENT_ON_DATA:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
        // Clean the buffer in case of a new request
        if (output_len == 0 && evt->user_data)
        {
            // we are just starting to copy the output data into the use
            memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER);
        }
        /*
         *  Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
         *  However, event handler can also be used in case chunked encoding is used.
         */
        if (!esp_http_client_is_chunked_response(evt->client))
        {
            // If user_data buffer is configured, copy the response into the buffer
            int copy_len = 0;
            if (evt->user_data)
            {
                // The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access.
                copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len));
                if (copy_len)
                {
                    memcpy(evt->user_data + output_len, evt->data, copy_len);
                }
            }
            else
            {
                int content_len = esp_http_client_get_content_length(evt->client);
                if (output_buffer == NULL)
                {
                    // We initialize output_buffer with 0 because it is used by strlen() and similar functions therefore should be null terminated.
                    output_buffer = (char *)calloc(content_len + 1, sizeof(char));
                    output_len = 0;
                    if (output_buffer == NULL)
                    {
                        ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
                        return ESP_FAIL;
                    }
                }
                copy_len = MIN(evt->data_len, (content_len - output_len));
                if (copy_len)
                {
                    memcpy(output_buffer + output_len, evt->data, copy_len);
                }
            }
            output_len += copy_len;
        }

        break;
    case HTTP_EVENT_ON_FINISH:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
        if (output_buffer != NULL)
        {
            // Print buffer
            ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);

            free(output_buffer);
            output_buffer = NULL;
        }
        output_len = 0;
        break;
    case HTTP_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
        int mbedtls_err = 0;
        esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL);
        if (err != 0)
        {
            ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
            ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
        }
        if (output_buffer != NULL)
        {
            free(output_buffer);
            output_buffer = NULL;
        }
        output_len = 0;
        break;
    case HTTP_EVENT_REDIRECT:
        ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT");
        esp_http_client_set_header(evt->client, "From", "user@example.com");
        esp_http_client_set_header(evt->client, "Accept", "text/html");
        esp_http_client_set_redirection(evt->client);
        break;
    }
    return ESP_OK;
}

char *http_client_exec(const http_request_opts *opts)
{
    char *local_response_buffer = calloc(MAX_HTTP_OUTPUT_BUFFER + 1, 1);

    ESP_LOGI(TAG, "Perform HTTP request on %s", opts->url);

    esp_http_client_config_t config = {
        .event_handler = _http_event_handler,
        .user_data = local_response_buffer,
        .url = opts->url,
        .disable_auto_redirect = true,
        .cert_pem = opts->root_ca,
        .method = opts->method == MethodPOST ? HTTP_METHOD_POST : HTTP_METHOD_GET,
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);

    if (client == NULL)
    {
        ESP_LOGE(TAG, "Failed to initialize HTTP connection!");
        free(local_response_buffer);
        return NULL;
    }

    if (opts->content_type)
    {
        esp_http_client_set_header(client, "Content-Type", opts->content_type);
    }

    if (opts->body)
    {
        esp_http_client_set_post_field(client, opts->body, strlen(opts->body));
    }

    ESP_LOGD(TAG, "esp_http_client_perform start");
    esp_err_t err = esp_http_client_perform(client);
    ESP_LOGD(TAG, "esp_http_client_perform end");

    if (err != ESP_OK)
    {
        esp_http_client_cleanup(client);
        free(local_response_buffer);
        ESP_LOGE(TAG, "HTTP request failed with code %d!", err);
        return NULL;
    }

    const int status = esp_http_client_get_status_code(client);
    const int64_t resp_len = esp_http_client_get_content_length(client);

    esp_http_client_cleanup(client);

    if (status < 200 || status > 299)
    {
        ESP_LOGE(TAG, "HTTP request failed with status %d!", status);

        if (status > 299)
        {
            ESP_LOGI(TAG, "HTTP Response Body = %s", local_response_buffer);
        }

        free(local_response_buffer);
        return NULL;
    }

    local_response_buffer[resp_len] = 0;

    return local_response_buffer;
}

static uint32_t uri[] = {
    0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

    /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
    0xd000002d, /* 1101 0000 0000 0000  0000 0000 0010 1101 */

    /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
    0x50000000, /* 0101 0000 0000 0000  0000 0000 0000 0000 */

    /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
    0xb8000001, /* 1011 1000 0000 0000  0000 0000 0000 0001 */

    0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
    0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
    0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
    0xffffffff  /* 1111 1111 1111 1111  1111 1111 1111 1111 */
};

static u_char hex[] = "0123456789ABCDEF";

size_t http_client_escape_uri(unsigned char *dst, const u_char *src, size_t size)
{
    size_t n;

    if (dst == NULL)
    {
        /* find the number of the characters to be escaped */
        n = 0;

        while (size)
        {
            if (uri[*src >> 5] & (1U << (*src & 0x1f)))
            {
                n++;
                n++;
            }
            src++;
            size--;
            n++;
        }

        return n + 1;
    }

    while (size)
    {
        if (uri[*src >> 5] & (1U << (*src & 0x1f)))
        {
            *dst++ = '%';
            *dst++ = hex[*src >> 4];
            *dst++ = hex[*src & 0xf];
            src++;
        }
        else
        {
            *dst++ = *src++;
        }
        size--;
    }

    *dst++ = '\0';

    return 0;
}