// ihm_gateway.c
#include "ihm_gateway.h"
#include "ihm.h"
#include <stdio.h>
#include <stdlib.h>
#include "platform_detect.h"
#include "cJSON.h"
#include "esp_log.h"

static const char *TAG = "IHM_GW";


static QueueHandle_t xIHMEventQueue = NULL;

QueueHandle_t getIHMQueueHandle(void) {
    return xIHMEventQueue;
}

void ihm_gateway_init(void) {
    if (!xIHMEventQueue) {
        xIHMEventQueue = xQueueCreate(32, sizeof(xIHMEvent_t *));
    }
}

void ihm_gateway_post_event(xIHMEvent_t *evt) {
    if (!xIHMEventQueue) return;

    if (platform_is_pc()) {
        xQueueSend(xIHMEventQueue, &evt, 0); // non bloquant sur PC
    } else {
        xQueueSend(xIHMEventQueue, &evt, portMAX_DELAY); // bloquant ESP32
    }
}

extern lv_subject_t wifiStatus;

void ihm_gateway_process_queue(void) {
    if (!xIHMEventQueue) return;

    xIHMEvent_t *evt = NULL;

    if (platform_is_pc()) {
        if (xQueueReceive(xIHMEventQueue, &evt, 0) != pdTRUE) return;
    } else {
        if (xQueueReceive(xIHMEventQueue, &evt, pdMS_TO_TICKS(10)) != pdTRUE) return;
    }

    if (!evt) return;

        switch (evt->eEventType) {
            case IHM_EVT_WIFI_STATUS:
                lv_subject_set_int(&wifiStatus, *(bool *)evt->pvData);
                break;

            case IHM_EVT_TIME_SETTED:
                draw_time(evt->pvData);
                break;

            case IHM_EVT_OTA_STARTED:
                app_ota_display();
                break;

            case IHM_EVT_HUMID_TEMP:
                draw_temp((char *)evt->pvData);
                break;

            case IHM_EVT_PUISSANCE_EMISE: {
                int val = *(int*)evt->pvData;
                //if(val == 0) lv_chart_set_next_value(myChart, ser, LV_CHART_POINT_NONE);
                //else lv_chart_set_next_value(myChart, ser, val);
                break;
            }

            case IHM_EVT_ETAT_MACHINE: {
                char *etatMachine = evt->pvData;
                cJSON *root = cJSON_Parse(etatMachine);
                bool enRoute = cJSON_GetObjectItem(root,"state")->valueint;
                int timestamp = cJSON_GetObjectItem(root,"timestamp")->valueint;

                time_t rawtime = (time_t)timestamp;
                struct tm *heure_locale = localtime(&rawtime);
                char heureFormattee[50];
                strftime(heureFormattee, sizeof(heureFormattee), "%d/%m/%Y %H:%M:%S", heure_locale);

                char etatFormate[90];
                snprintf(etatFormate,sizeof(etatFormate),
                        enRoute ? "Machine en route depuis %s" : "Machine : Arrêt détecté depuis %s",
                        heureFormattee);

                //lv_label_set_text(lblEtatMachine, etatFormate);
                cJSON_Delete(root);
                break;
            }

            case IHM_EVT_HAUTEUR_CUVE: {
                float hauteur = *(float *)evt->pvData;
                //lv_label_set_text_fmt(lblHauteurCuve, "%.0f cm", hauteur);
                break;
            }

            default:
                ESP_LOGE(TAG, "Evt inconnu");
                break;
        }

        // Nettoyage mémoire sécurisé
        if (evt->bNeedToFreeData && evt->pvData) free(evt->pvData);
        free(evt);

}
