#pragma once

#include <stddef.h>
#include <stdint.h>
#include "esp_err.h"

/* Waveshare 7.3" E Ink Spectra 6 (E6) panel: 800x480, 4 bits/pixel packed
 * 2-pixels-per-byte, per the official EPD_7in3e.c reference driver. */
#define EPD_WIDTH 800
#define EPD_HEIGHT 480
#define EPD_BYTES_PER_ROW ((EPD_WIDTH + 1) / 2)
#define EPD_FRAME_BYTES (EPD_BYTES_PER_ROW * EPD_HEIGHT)

typedef enum {
    EPD_COLOR_BLACK = 0x0,
    EPD_COLOR_WHITE = 0x1,
    EPD_COLOR_YELLOW = 0x2,
    EPD_COLOR_RED = 0x3,
    EPD_COLOR_BLUE = 0x5,
    EPD_COLOR_GREEN = 0x6,
} epd_color_t;

/** Configures SPI + GPIO and runs the panel's power-on register init sequence. */
esp_err_t epd_init(void);

/** Fills the whole panel with a single color and refreshes. */
esp_err_t epd_clear(epd_color_t color);

/**
 * Called repeatedly by epd_display_stream() to fill up to chunk_size bytes
 * into chunk. Must return the number of bytes written, or 0 once exhausted.
 */
typedef size_t (*epd_read_fn_t)(uint8_t *chunk, size_t chunk_size, void *ctx);

/**
 * Streams a full frame (EPD_FRAME_BYTES bytes, packed 2 pixels/byte) to the
 * panel via read_fn and refreshes. Pulling from a caller-supplied source
 * instead of a single buffer lets callers feed the panel directly from an
 * HTTP response without holding the whole ~192KB frame in RAM.
 */
esp_err_t epd_display_stream(epd_read_fn_t read_fn, void *ctx);

/**
 * Like epd_display_stream(), but writes the frame into the panel's
 * internal buffer over SPI WITHOUT triggering the physical refresh (the
 * visible flash/flicker, which also takes 15-30+ seconds) -- call
 * epd_turn_on_display() separately to make it visible. Returns
 * ESP_ERR_INVALID_SIZE if read_fn didn't supply exactly EPD_FRAME_BYTES,
 * same as epd_display_stream(); either way nothing is refreshed, so the
 * visible screen is left untouched on error.
 *
 * If out_crc32 is non-NULL, it's set to a CRC32 of the bytes written --
 * lets a caller compare against the last-displayed frame's CRC and skip
 * the refresh entirely when nothing actually changed (e.g. redisplaying
 * the same photo after a reboot).
 */
esp_err_t epd_write_frame(epd_read_fn_t read_fn, void *ctx, uint32_t *out_crc32);

/**
 * Triggers the panel's physical refresh cycle (power on, refresh, power
 * off) -- the visible flash/flicker sequence, 15-30+ seconds. Call after
 * epd_write_frame() to make the written buffer visible.
 */
esp_err_t epd_turn_on_display(void);

/** Convenience wrapper around epd_display_stream() for an in-memory frame buffer. */
esp_err_t epd_display_buffer(const uint8_t *frame, size_t len);

/** Puts the panel into deep sleep to minimize power draw between refreshes. */
esp_err_t epd_sleep(void);
