#include "../../lodepng/lodepng.h"
#include "../../lodepng_alloc.hpp"
#include "../../lv_setup.hpp"
#include "esp_heap_caps.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_random.h"
#include "lvgl.h"
#include <string.h>

internal const char *kTagDisplayImage = "API_DISPLAY_IMAGE";

internal esp_err_t api_display_image_handler(httpd_req_t *req)
{
  httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

  // We are generating PNG on the fly, don't let it be cached locally
  // immediately
  httpd_resp_set_hdr(req, "Cache-Control",
                     "no-cache, no-store, must-revalidate");
  httpd_resp_set_type(req, "image/png");

  if (xSemaphoreTake(g_LvglMutex, pdMS_TO_TICKS(5000)) != pdTRUE)
  {
    ESP_LOGE(kTagDisplayImage, "Failed to get LVGL mutex");
    httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "LVGL busy");
    return ESP_FAIL;
  }

  // Change the background color securely to a random grayscale value
  // esp_random() returns 32 bits, we just take the lowest 8.
  uint8_t rand_gray = esp_random() & 0xFF;
  lv_obj_t *active_screen = lv_screen_active();
  // lv_obj_set_style_bg_color(active_screen, lv_color_make(rand_gray,
  // rand_gray, rand_gray), LV_PART_MAIN);

  // Force a screen refresh to get the latest rendered frame
  lv_refr_now(g_LvglDisplay);

  lv_draw_buf_t *draw_buf = lv_display_get_buf_active(g_LvglDisplay);
  if (!draw_buf)
  {
    xSemaphoreGive(g_LvglMutex);
    ESP_LOGE(kTagDisplayImage, "No active draw buffer available");
    httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
                        "Display uninitialized");
    return ESP_FAIL;
  }

  uint32_t width = CONFIG_CALENDINK_DISPLAY_WIDTH;
  uint32_t height = CONFIG_CALENDINK_DISPLAY_HEIGHT;

  // We allocate a new buffer for the tightly packed 8-bit PNG grayscale data.
  // Converting RGB565 frame to 4-level grayscale (quantized to 0, 85, 170, 255).
  uint8_t *packed_data =
      (uint8_t *)heap_caps_malloc(width * height, MALLOC_CAP_SPIRAM);
  if (!packed_data)
  {
    packed_data = (uint8_t *)malloc(width * height); // Fallback
    if (!packed_data)
    {
      xSemaphoreGive(g_LvglMutex);
      ESP_LOGE(kTagDisplayImage, "Failed to allocate packed buffer");
      httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
                          "Out of memory");
      return ESP_FAIL;
    }
  }

  // LVGL renders into RGB565 (2 bytes per pixel).
  // Iterating to create an 8-bit grayscale PNG using 4 specific values.
  for (uint32_t y = 0; y < height; ++y)
  {
    const uint16_t *src_row =
        (const uint16_t *)((const uint8_t *)draw_buf->data +
                           (y * draw_buf->header.stride));
    uint8_t *dst_row = packed_data + (y * width);

    for (uint32_t x = 0; x < width; ++x)
    {
      uint16_t c = src_row[x];
      // Note: LVGL may use swapped bytes for SPI rendering depending on config,
      // but in memory RGB565 is standard if no SWAP is active. Usually standard
      // RGB565 format: R(5) G(6) B(5)
      uint8_t r_5 = (c >> 11) & 0x1F;
      uint8_t g_6 = (c >> 5) & 0x3F;
      uint8_t b_5 = c & 0x1F;

      // Expand to 8 bits
      uint8_t r = (r_5 << 3) | (r_5 >> 2);
      uint8_t g = (g_6 << 2) | (g_6 >> 4);
      uint8_t b = (b_5 << 3) | (b_5 >> 2);

      // Simple luminance calculation (fast)
      uint8_t lum = (r * 77 + g * 150 + b * 29) >> 8;

      // Quantize to 4 levels (0..3)
      uint8_t level = lum >> 6;
      
      // Expand level back to 8-bit for PNG: 0, 85, 170, 255
      dst_row[x] = level * 85;
    }
  }

  // Convert LVGL 8-bit L8 buffer to 8-bit grayscale PNG using LodePNG.
  // LCT_GREY = 0, bitdepth = 8
  unsigned char *png = nullptr;
  size_t pngsize = 0;

  // We are about to start a huge memory operation inside LodePNG.
  // We reset our 3MB PSRAM bump allocator to 0 bytes used.
  lodepng_allocator_reset();

  ESP_LOGI(kTagDisplayImage, "Encoding %lux%lu frame to PNG...", width, height);
  unsigned error = lodepng_encode_memory(&png, &pngsize, packed_data, width,
                                         height, LCT_GREY, 8);

  free(packed_data);

  xSemaphoreGive(g_LvglMutex);

  if (error)
  {
    ESP_LOGE(kTagDisplayImage, "PNG encoding error %u: %s", error,
             lodepng_error_text(error));
    httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
                        "PNG generation failed");
    return ESP_FAIL;
  }

  ESP_LOGI(kTagDisplayImage,
           "Prepared PNG, size: %zu bytes. Sending to client...", pngsize);
  esp_err_t res = httpd_resp_send(req, (const char *)png, pngsize);

  // No need to free(png) because it is managed by our bump allocator
  // which automatically resets the entire 2MB buffer to 0 next time
  // lodepng_allocator_reset() is called.

  return res;
}

httpd_uri_t api_display_image_uri = {.uri = "/api/display/image.png",
                                     .method = HTTP_GET,
                                     .handler = api_display_image_handler,
                                     .user_ctx = NULL};
