#include <lgfx.h>
#include <lvgl.h>

#include "esp_err.h"
#include "esp_log.h"

#include "ff.h"
#include "image.h"
#include "storage.h"

#define screenWidth (480)
#define screenHeight (320)

const unsigned int lvBufferSize = screenWidth * screenHeight / 10 * (LV_COLOR_DEPTH / 8);
uint8_t lvBuffer1[lvBufferSize];
uint8_t lvBuffer2[lvBufferSize];

static const char *TAG = "main";

LGFX tft;
static FATFS fs;

void setup_tft(void)
{
    tft.begin();
    tft.setRotation(1);
    tft.setBrightness(255);
}

void flush(lv_display_t *display, const lv_area_t *area, unsigned char *data)
{
    uint32_t w = lv_area_get_width(area);
    uint32_t h = lv_area_get_height(area);
    lv_draw_sw_rgb565_swap(data, w * h);
    tft.pushImageDMA(area->x1, area->y1, w, h, (uint16_t *)data);
    lv_display_flush_ready(display);
}

void my_touch_read(lv_indev_t *indev_driver, lv_indev_data_t *data)
{
    uint16_t touchX, touchY;
    bool touched = tft.getTouch(&touchX, &touchY);
    if (!touched)
    {
        data->state = LV_INDEV_STATE_REL;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;
        data->point.x = touchX;
        data->point.y = touchY;
    }
}

void fatfs_lvgl_init(void)
{
    // 1. Das Dateisystem "mounten" (einmalig für FatFS)
    f_mount(&fs, "S:", 1); // "S:" ist der Laufwerksbuchstabe

    // 2. Eine Treiber-Variable deklarieren
    static lv_fs_drv_t drv;
    lv_fs_drv_init(&drv); // Treiberstruktur initialisieren

    // 3. Unsere Bridge-Funktionen zuweisen
    drv.letter = 'S'; // WICHTIG: Muss mit dem Buchstaben in f_mount übereinstimmen
    drv.open_cb = fs_open;
    drv.close_cb = fs_close;
    drv.read_cb = fs_read;
    drv.seek_cb = fs_seek;
    drv.tell_cb = fs_tell;

    // 4. Den Treiber bei LVGL registrieren
    lv_fs_drv_register(&drv);
}

void esp_lv_log_print(lv_log_level_t level, const char *buf)
{
    switch (level)
    {
    case LV_LOG_LEVEL_TRACE:
        ESP_LOGV("LVGL", "%s", buf);
        break;
    case LV_LOG_LEVEL_INFO:
        ESP_LOGI("LVGL", "%s", buf);
        break;
    case LV_LOG_LEVEL_WARN:
        ESP_LOGW("LVGL", "%s", buf);
        break;
    case LV_LOG_LEVEL_ERROR:
        ESP_LOGE("LVGL", "%s", buf);
        break;
    case LV_LOG_LEVEL_USER:
        ESP_LOGI("LVGL", "%s", buf);
        break;
    case LV_LOG_LEVEL_NONE:
        break;
    }
}

void setup()
{
    setup_tft();

    lv_init();

    lv_log_register_print_cb(esp_lv_log_print);

    fs_mount();
    fatfs_lvgl_init();

    static auto *display = lv_display_create(screenWidth, screenHeight);
    lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB565);
    lv_display_set_flush_cb(display, flush);
    lv_display_set_buffers(display, lvBuffer1, lvBuffer2, lvBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

    static auto *lvInput = lv_indev_create();
    lv_indev_set_type(lvInput, LV_INDEV_TYPE_POINTER);
    lv_indev_set_read_cb(lvInput, my_touch_read);

    ESP_LOGI(TAG, "create image");
#ifndef LOCAL
    static auto *image = lv_image_create(lv_scr_act());
    lv_image_set_src(image, &image_data);
    lv_obj_align(image, LV_ALIGN_TOP_LEFT, 0, 0);
#else
    lv_obj_t *img = lv_image_create(lv_screen_active());
    lv_image_set_src(img, "S:/response.png"); // Pfad beginnt mit "S:"
    lv_obj_center(img);
#endif
    ESP_LOGI(TAG, "image created");
}

void loop()
{
    lv_tick_inc(10);
    lv_timer_handler();
    vTaskDelay(pdMS_TO_TICKS(10));
}

void lvgl_task(void *pvParameter)
{
    setup();
    while (1)
    {
        loop();
    }
    fs_unmount();
}

extern "C" void app_main(void)
{
    xTaskCreatePinnedToCore(lvgl_task, "lvgl_task", 8192, NULL, 5, NULL, 1);
}
