#include "ihm.h"
#include "misc/lv_types.h"
#include "esp_log.h"
#include "string.h"
#include "meteofrance.h"
#include "cJSON.h"
#include "eventsManager.h"
#include "bsp/esp-bsp.h"
#include "lvgl_private.h"
#include "lv_examples.h"

#include "lv_theme_domotic.h"


#define upEvent "monter"
#define downEvent "descendre"

LV_FONT_DECLARE(montserrat_medium_12);
LV_FONT_DECLARE(montserrat_medium_18);
LV_FONT_DECLARE(montserrat_medium_24);
LV_FONT_DECLARE(roboto_medium_36);
LV_FONT_DECLARE(roboto_medium_72);
LV_FONT_DECLARE(super_malibu_80);

lv_subject_t dateHeureSubj;
lv_obj_t *lblTempInt2;
char tempExtStr[6];
char tempIntStr[6];
char hauteurCuveStr[9];
char hauteurCuveEvolStr[9];
lv_subject_t tempIntSubj;
lv_subject_t tempExtSubj;
lv_subject_t hauteurCuveSubj;
lv_subject_t hauteurCuveEvolSubj;
lv_subject_t forecastD1Subj;
lv_subject_t forecastD2Subj;
lv_subject_t forecastD3Subj;
lv_subject_t *tmpSubj[3] = {&forecastD1Subj, &forecastD2Subj, &forecastD3Subj};
lv_subject_t forecastH1Subj;
lv_subject_t forecastH2Subj;
lv_subject_t forecastH3Subj;
lv_subject_t *tmpHSubj[3] = {&forecastH1Subj, &forecastH2Subj, &forecastH3Subj};

lv_subject_t meteoStatus;
char dateHeureStr[30];
lv_obj_t *jour;

static lv_style_t no_padding;
static const char *TAG = "IHM";
lv_subject_t wifiStatus;

LV_IMAGE_DECLARE(wifi_ok);
LV_IMAGE_DECLARE(wifi_ko);

static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject)
{
    ESP_LOGV(TAG, "On passe dans le callback de chgt de statut; %li", lv_subject_get_int(subject));
    bsp_display_lock(0);
    if(lv_layer_top()!=NULL && lv_obj_get_child_cnt(lv_layer_top())){
        lv_obj_t * wifiSt = lv_obj_get_child(lv_obj_get_child(lv_layer_top(), 0),2);
        if(lv_obj_check_type(wifiSt, &lv_image_class)){
            switch (lv_subject_get_int(subject))
            {
            case 0:
                lv_image_set_src(wifiSt,&wifi_ko);
                break;
            case 1:
                lv_image_set_src(wifiSt,&wifi_ok);
                break;
            default:
                break;
            }
        }else{
            ESP_LOGE(TAG, "L'objet recuip en semble pas etre du bon type");
        }
    }else{
        ESP_LOGI(TAG,"Pour le moment l'icone de statut n'existe pas");
    }

    bsp_display_unlock();
    //int32_t prev_v = lv_subject_get_previous_int(subject);
    //int32_t cur_v = lv_subject_get_int(subject);

    //lv_obj_t * btn = lv_observer_get_target(observer);
}

void draw_time(struct tm *dateHeure){
    //if(display_lock("updateTime")){
        //lv_label_set_text(jour, dateHeure);
        lv_subject_set_pointer(&timeSubj, dateHeure);
        //display_unlock("updateTime");
    //}            
}

void draw_temp(char * tempHumid){
    if(display_lock("draw_temp")){
       lv_label_set_text(lblTempInt2,tempHumid);
       display_unlock("draw_temp");
    }            
}

lv_obj_t* myChart;
lv_chart_series_t * ser;
lv_obj_t *lblHauteurCuve;
lv_obj_t* lblEtatMachine;
static lv_style_t style_lbvValue;
static lv_style_t style_btn;

lv_theme_t *lv_theme_create(void)
{
    lv_theme_t *theme = lv_zalloc(sizeof(*theme));
    LV_ASSERT_MALLOC(theme);
    return theme;
}

void lv_theme_copy(lv_theme_t *dst, const lv_theme_t *src)
{
    if (!dst || !src)
    {
        LV_LOG_WARN("Refusing to copy null themes");
        return;
    }
    lv_memcpy(dst, src, sizeof(*src));
}
static void create_ui(void)
{
    // Initialisation du thème
    lv_theme_t *th_new = lv_theme_domotic_init(lv_display_get_default());
    lv_display_set_theme(lv_display_get_default(), th_new);

    // Initialisation des sujets / observers
    lv_subject_init_int(&wifiStatus, 0);
    lv_subject_add_observer_obj(&wifiStatus, wifiStatus_obs_cb, NULL, NULL);

    // Création des écrans / widgets
    app_main_display();  // ton écran principal
    drawHome();          // écran Home
    // chart, labels, boutons, etc.
    // par exemple : lblEtatMachine, lblHauteurCuve, myChart, ser...
}

static void ihm_process_queue(QueueHandle_t xIHMEventQueue)
{
    xIHMEvent_t *xReceivedEvent = NULL;

    while (xQueueReceive(xIHMEventQueue, &xReceivedEvent, 0) == pdTRUE && xReceivedEvent) {

    }
}

// draw_ihm.c
#include "ihm_gateway.h"
#include <unistd.h> // usleep
#include "platform_detect.h"

void drawIhm(void *param) {
    // Init display + LVGL
    init_display_ihm();
    //lv_init();
    lv_theme_t *th_new = lv_theme_domotic_init(lv_display_get_default());
    lv_display_set_theme(lv_display_get_default(), th_new);

    // Init queue
    //ihm_gateway_init();

    create_ui();

    // Loop unifiée
    while (1) {
        ihm_gateway_process_queue();
        uint32_t idle_time = lv_timer_handler();

        if (platform_is_pc()) {
            // SDL backend
            usleep(idle_time * 1000);
        } else {
            // ESP32 : task FreeRTOS
            vTaskDelay(pdMS_TO_TICKS(idle_time));
        }
    }
}

static void event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    // lv_obj_t *obj = (lv_obj_t *)lv_event_get_target(e);
    char *evtData = (char *)lv_event_get_user_data(e);

    switch (code)
    {
    case LV_EVENT_PRESSED:
        // LV_LOG_USER("%s was pressed\n", evtData);
        break;
    case LV_EVENT_CLICKED:
        ESP_LOGI(TAG, "%s was clicked\n", evtData);
        send_event(EVT_BTN_VOLET,evtData);
        break;
    case LV_EVENT_LONG_PRESSED:
        LV_LOG_USER("%s was long pressed\n", evtData);
        break;
    case LV_EVENT_LONG_PRESSED_REPEAT:
        LV_LOG_USER("%s was long press repeat\n", evtData);
        break;
    default:
        break;
    }
}

#if CONFIG_IDF_TARGET_LINUX
    static lv_display_t *display;
    static lv_indev_t *mouse;
    static lv_indev_t *mouse_wheel;
    static lv_indev_t *keyboard;
#endif

void init_display_ihm(){

    lv_init();
    #if CONFIG_IDF_TARGET_LINUX
        printf("Hello from sim\n");
        display = lv_sdl_window_create(1024, 600);
        mouse = lv_sdl_mouse_create();
        mouse_wheel = lv_sdl_mousewheel_create();
        keyboard = lv_sdl_keyboard_create();
    #else
    lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
    lvgl_cfg.task_priority=15;
    bsp_display_cfg_t cfg = {
        .lvgl_port_cfg = lvgl_cfg,
        .buffer_size = 1024*600,//BSP_LCD_DRAW_BUFF_SIZE,
        .double_buffer = 1,
        .flags = {
            .buff_dma = false,
            .buff_spiram = false,
            .sw_rotate = true
        }
    };
    ESP_LOGE(TAG,"On demarre le display");
    lv_display_t *dsp = bsp_display_start_with_config(&cfg);
    //bsp_display_rotate(dsp,LV_DISP_ROTATION_180);

    bsp_display_backlight_on();
    bsp_display_brightness_set(50);
    #endif

    /*
    */
    mainState.display_init=true;

    time_t now;
    struct tm timeinfo;
    time(&now);
    localtime_r(&now, &timeinfo);

    lv_subject_init_pointer(&timeSubj, &timeinfo);

}

lv_obj_t * otaStatus;
lv_obj_t * arcProgress;
static void value_changed_event_cb(lv_event_t * e)
{
    //lv_obj_t * arc = lv_event_get_target_obj(e);
    lv_obj_t * label = (lv_obj_t *)lv_event_get_user_data(e);

    lv_label_set_text_fmt(label, "%" LV_PRId32 "%%", lv_arc_get_value(arcProgress));

    /*Rotate the label to the current position of the arc*/
    lv_arc_rotate_obj_to_angle(arcProgress, label, 25);
}

void app_ota_display(){
    if(display_lock("app_ota_display")){
        lv_obj_clean(lv_scr_act());
        lv_obj_t * label = lv_label_create(lv_screen_active());
        otaStatus=lv_label_create(lv_scr_act());
        lv_label_set_text(otaStatus, "Mise à jour OTA en cours");
        /*Create an Arc*/
        arcProgress = lv_arc_create(lv_screen_active());
        lv_obj_set_size(arcProgress, 150, 150);
        lv_arc_set_rotation(arcProgress, 135);
        lv_arc_set_bg_angles(arcProgress, 0, 270);
        lv_arc_set_value(arcProgress, 0);
        lv_obj_center(arcProgress);
        lv_obj_add_event_cb(arcProgress, value_changed_event_cb, LV_EVENT_VALUE_CHANGED, label);

        /*Manually update the label for the first time*/
        lv_obj_send_event(arcProgress, LV_EVENT_VALUE_CHANGED, NULL);
        display_unlock("app_ota_display");
    }
}

void setOTAProgress(int value){
    if(display_lock("setOTAProgress")){
        lv_arc_set_value(arcProgress, value);
        lv_obj_send_event(arcProgress, LV_EVENT_VALUE_CHANGED, NULL);
        display_unlock("setOTAProgress");
    }
}

static lv_style_t style_gradient;
static const lv_color_t grad_colors[2] = {
    LV_COLOR_MAKE(0x9B, 0x18, 0x42),
    LV_COLOR_MAKE(0x00, 0x00, 0x00),
};

void app_main_display()
{

    if(display_lock("meteoStatus")){
        lv_subject_init_int(&meteoStatus, -1);
        display_unlock("meteoStatus");
    }

    
    struct meteodailyforecast_data d;
    struct dailyforecast_prev p;
    lv_strcpy(p.desc, "");
    d.previsions = p;
    d.isValid=false;
    ESP_LOGI(TAG, "Init forecastD1Subj");

    lv_subject_init_pointer(&forecastD1Subj, &d);
    lv_subject_init_pointer(&forecastD2Subj, &d);
    lv_subject_init_pointer(&forecastD3Subj, &d);

    struct meteoforecast_data d1;
    struct forecast_prev p1;
    lv_strcpy(p1.desc, "");

    d1.previsions = p1;
    d1.isValid=false;
    lv_subject_init_pointer(&forecastH1Subj, &d1);
    lv_subject_init_pointer(&forecastH2Subj, &d1);
    lv_subject_init_pointer(&forecastH3Subj, &d1);


    lv_subject_init_string(&dateHeureSubj, dateHeureStr, NULL, 64, "--");

    lv_style_init(&no_padding);
    lv_style_set_pad_all(&no_padding, 0);


    lv_style_init(&style_gradient);

    /*First define a color gradient. In this example we use a purple to black color map.*/
    static lv_grad_dsc_t grad;

    lv_gradient_init_stops(&grad, grad_colors, NULL, NULL, sizeof(grad_colors) / sizeof(lv_color_t));

    /*Make a radial gradient with the center in the middle of the object, extending to the farthest corner*/
    lv_grad_radial_init(&grad, LV_GRAD_CENTER, LV_GRAD_CENTER, LV_GRAD_RIGHT, LV_GRAD_BOTTOM, LV_GRAD_EXTEND_PAD);

    /*Set gradient as background*/
    lv_style_set_bg_grad(&style_gradient, &grad);

    lv_obj_t *cont_status = lv_obj_create(lv_layer_top());
    lv_obj_add_style(cont_status, &no_padding, 0);
    lv_obj_align(cont_status, LV_ALIGN_TOP_RIGHT, 0, 0);
    lv_obj_set_flex_flow(cont_status, LV_FLEX_FLOW_ROW);
    lv_obj_set_height(cont_status, LV_SIZE_CONTENT);
    lv_obj_set_width(cont_status, LV_SIZE_CONTENT);

    jour = lv_label_create(cont_status);
    lv_obj_set_style_text_font(jour,lv_theme_get_font_normal(jour),0);
    lv_obj_set_width(jour, LV_SIZE_CONTENT);
    lv_label_bind_text(jour, &dateHeureSubj, NULL);

    lv_obj_refr_size(cont_status);
    // lv_obj_align(jour, LV_ALIGN_CENTER,0,0);

    lv_obj_t *meteoR = lv_label_create(cont_status);
    lv_obj_set_style_text_font(meteoR,lv_theme_get_font_normal(jour),0);
    lv_label_set_text(meteoR, "\xEF\x83\x82");
    lv_obj_set_style_text_color(meteoR, lv_color_make(0x00, 0xff, 0xff), 0);
    lv_subject_add_observer_obj(&meteoStatus, meteo_obs_cb, meteoR, NULL);

    // lv_label_bind_text(meteoR, &meteoStatus, "Meteo %d");

    lv_obj_t *wifi = lv_image_create(cont_status);
    lv_obj_set_style_bg_color(wifi, lv_palette_lighten(LV_PALETTE_GREY, 3), 0);
    lv_obj_set_style_image_recolor_opa(wifi, 255, 0);
    lv_obj_set_style_image_recolor(wifi, lv_color_white(), 0);
    // lv_obj_set_style_border_width(wifi,1,0);
    // lv_obj_set_style_border_color(wifi,lv_palette_darken(LV_PALETTE_GREY, 3),0);
    //  lv_label_set_text(wifi,"Wifi Ok");
    //  lv_label_bind_text(wifi, &wifiStatus, "Wifi %d");

    lv_image_create(cont_status);
    // lv_label_set_text(mqtt,"Mqtt Ok");
    // lv_label_bind_text(mqtt, &mqttStatus, "Mqtt %d");

    lv_obj_t *scr = lv_scr_act();
    lv_obj_add_style(scr, &style_gradient, 0);

    /* Your LVGL objects code here .... */

    /* Label */
    lv_obj_t *label = lv_label_create(scr);
    lv_obj_set_width(label, lv_pct(100));
    lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_set_style_text_font(label,lv_theme_get_font_large(label),0);
    lv_obj_set_style_text_color(label, lv_color_make(255,255,255),0);
    lv_label_set_text(label, "Bienvenue sur DomoTIC");
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 20);
}

bool display_lock_logChoose(const char *TAG, bool logIt)
{
    if (logIt)
        ESP_LOGI(TAG, "Obtention mutex");
    if (bsp_display_lock(3000))
    {
        if(logIt)
            ESP_LOGI(TAG, "Mutex obtenu");
        return true;
    }
    else
    {
        ESP_LOGE(TAG, "Impossible d'obtenir le mutex");
        return false;
    }
}

bool display_lock(const char *TAG)
{
    return display_lock_logChoose(TAG, true);
}

void display_unlock_logChoose(const char *TAG, bool logIt)
{
    if (logIt)
        ESP_LOGI(TAG, "Libération mutexx");
    bsp_display_unlock();
}

void display_unlock(const char *TAG)
{
    display_unlock_logChoose(TAG, true);
}

// Ce callback est applé lorsque meteoStatus change
void meteo_obs_cb(lv_observer_t *observer, lv_subject_t *subject)
{
        ESP_LOGI(TAG, "On passe dans le callback de chgt de statut meteo; %li", lv_subject_get_int(subject));
        if (display_lock("meteo_obs_cb"))
        {
            lv_obj_t *meteoSt = lv_observer_get_target(observer);
            switch (lv_subject_get_int(subject))
            {
            case 0:
                lv_obj_set_style_text_color(meteoSt, lv_color_make(0x00, 0xff, 0xff), 0);
                break;
            case 1:
                lv_obj_set_style_text_color(meteoSt, lv_color_make(0xff, 0x00, 0x00), 0);
                break;
            }
            display_unlock("meteo_obs_cb");
        }else{
            ESP_LOGE(TAG,"Impossible d'obtenir le mutex dans meteo_obs_cb");
        }
}

//LV_IMAGE_DECLARE(plan_rdc);
//LV_IMAGE_DECLARE(plan_etage);

static lv_style_t style_container;

// Callback pour mettre à jour une météo journaliere
// On pointe sur un tableau de meteodailyforecast_data
static void weatherdata_obs_cb(lv_observer_t *observer, lv_subject_t *subject)
{

    // Retrieve weatherdata
    const struct meteodailyforecast_data *data = subject->value.pointer;
    bool isMinimal = observer->user_data;
    ESP_LOGI(TAG, "CB meteo jour declenché. Meteo valide : %b - Mode minimal : %d", data->isValid, isMinimal);
    if(!data->isValid){
        ESP_LOGI(TAG,"Données meteo non valides");
        return;
    }
    // printffd(data);
    if (data->isValid)
    {
        char buff[40] = {};
        snprintf(buff,sizeof(buff),"%s %.1f %.1f", data->previsions.desc, data->previsions.min, data->previsions.max);
        ESP_LOGI(TAG, "CB meteo jour declenché. Meteo valide : %s", buff);
        lv_obj_t *parent = (lv_obj_t *)(lv_observer_get_target(observer));
        lv_obj_dump_tree(parent->parent);
        ESP_LOGI(TAG, "On accede au parent %p %s", (void*)parent, lv_obj_get_name(parent));
        if(parent!=NULL){
            if(isMinimal){
                lv_obj_t *desc_icon = lv_obj_get_child(parent, 0);
                showMeteoIcon(data->previsions.icon, desc_icon, 0);
            }
            else
            {
                lv_obj_t *datefld = lv_obj_get_child(parent, 0);
                lv_obj_t *desc_icon = lv_obj_get_child(parent, 1);
                lv_obj_t *temps = lv_obj_get_child(parent, 2);
                if (datefld && desc_icon && temps)
                {
                    showMeteoIcon(data->previsions.icon, desc_icon, 0);
                    char buffer[80];
                    dtToString(data->datetime, buffer);

                    lv_label_set_text(datefld, buffer);
                    lv_label_set_text(lv_obj_get_child(desc_icon, 1), data->previsions.desc);
                    lv_label_set_text_fmt(lv_obj_get_child(temps, 0), "%.1f°C", data->previsions.max);
                    lv_label_set_text_fmt(lv_obj_get_child(temps, 1), "%.1f°C", data->previsions.min);
                }
            }
        }
    }
}

// Callback pour mettre à jour une météo horaire
// On pointe sur un tableau de meteoforecast_data
static void weatherdataH_obs_cb(lv_observer_t *observer, lv_subject_t *subject)
{
    ESP_LOGV("MeteoFrance", "CB meteo horaire declenché");
    // Retrieve weatherdata
    const struct meteoforecast_data *data = subject->value.pointer;
    if(data->isValid){

     //printffd(data);
     char buff[40] = {};
      snprintf(buff,sizeof(buff),"%.1f", data->previsions.value);
      ESP_LOGV(TAG, "On a recu [%s]", buff);

    lv_obj_t *parent = (lv_obj_t *)(lv_observer_get_target(observer));
    lv_obj_t *datefld = lv_obj_get_child(parent, 0);
    lv_obj_t *temp_desc_icon = lv_obj_get_child(parent, 1);
    showMeteoIcon(data->previsions.icon, temp_desc_icon, 1);

    char buffer[80];
    dtHToString(data->datetime, buffer);

    lv_label_set_text(datefld, buffer);
    lv_label_set_text(lv_obj_get_child(temp_desc_icon, 2), data->previsions.desc);
    lv_label_set_text_fmt(lv_obj_get_child(temp_desc_icon, 0), "%.1f°C", data->previsions.value);

        }else{
                  ESP_LOGE(TAG, "Pas de odnnées valides");
        }

}

void showMeteoIcon(const char *icon, lv_obj_t *desc_icon, int childNr)
{
    lv_obj_t *img = lv_obj_get_child(desc_icon, childNr);
    //char *str1 = "A:/littlefs/";
    char *str1 = "A:/home/marc/domotic/components/domotic_display/images_meteo";
    int sizeOfStr;
    sizeOfStr = strlen(str1) + strlen(icon) + 6;
    char *result = malloc(sizeOfStr);
    snprintf(result,sizeOfStr, "%s%s.png", str1, icon);
    ESP_LOGV(TAG,"On affiche l'image %s", result);
    lv_image_set_src(img, "A:/home/marc/esp/domotic/domotic/components/domotic_display/images_meteo/p4j.png");
    free(result);
}

static lv_style_t tempStyle;

// Ce fragment affiche une prévision journaliere (date,icone, min et max)
static void weatherDay_fragment_create_obj(int dayNr, lv_obj_t *parent, bool minimal)
{


    if (minimal)
    {
        //lv_obj_set_flex_flow(sup, LV_FLEX_FLOW_COLUMN);
        //lv_obj_set_size(sup, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        //lv_obj_set_flex_align(sup, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
        //lv_obj_set_style_border_width(sup, 1, 0);
        //lv_obj_set_style_border_color(sup, lv_color_white(), 0);

        lv_obj_t *container = lv_obj_create(parent);
        lv_obj_set_align(container, LV_ALIGN_CENTER);
        lv_obj_set_name(container, "container_meteo");
        // lv_obj_remove_style_all(container);
        // lv_obj_set_style_border_width(container, 1, 0);
        // lv_obj_set_style_border_color(container, lv_color_make(0, 255, 0), 0);
        lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
        lv_obj_add_style(container, &no_padding, 0);
        lv_obj_set_size(container, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        lv_obj_add_flag(container, LV_OBJ_FLAG_EVENT_BUBBLE);

        lv_obj_t *img1 = lv_image_create(container);
        lv_obj_set_name(img1, "meteo_img");
        lv_obj_add_flag(img1, LV_OBJ_FLAG_EVENT_BUBBLE);

        lv_obj_remove_style_all(img1);
        //lv_obj_set_style_bg_color(img1, lv_color_white(),0);
        //lv_obj_set_style_border_width(img1,1,0);
        lv_obj_add_style(img1, &no_padding, 0);
        lv_obj_set_style_bg_color(img1, lv_palette_lighten(LV_PALETTE_BLUE_GREY, 4), 0);
        lv_image_set_inner_align(img1, LV_IMAGE_ALIGN_TOP_LEFT);
        lv_image_set_offset_y(img1, -8);
        lv_image_set_offset_x(img1, -5);
        lv_obj_set_size(img1, 40, 35);
        lv_image_set_src(img1, LV_SYMBOL_DUMMY);
        //lv_obj_set_style_image_recolor_opa(img1, 100, 0);
        //lv_obj_set_style_image_recolor(img1, lv_color_black(), 0);
        // lv_obj_set_style_border_width(img1,2,0);
        // lv_obj_set_style_border_color(img1, lv_palette_main(LV_PALETTE_BLUE_GREY), 0);
        lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);

        lv_obj_t *desc = lv_label_create(container);
        lv_obj_set_name(desc, "meteo_desc");
        lv_label_set_long_mode(desc, LV_LABEL_LONG_MODE_WRAP);
        lv_obj_set_size(desc, 250, LV_SIZE_CONTENT);
        //lv_obj_set_style_text_color(desc, lv_color_white(), 0);
        lv_label_set_text(desc, "Temps ensoleillé! \nProfitez bien !");
        lv_obj_set_style_text_align(desc, LV_TEXT_ALIGN_CENTER, 0);

        // On positionne un observer sur le subjet correspondant au jour du widget
        bool isMinimal = true;
        lv_subject_add_observer_obj(tmpSubj[dayNr], weatherdata_obs_cb, parent, &isMinimal);
    }
    else
    {
        lv_obj_t *sup = lv_obj_create(parent);
        lv_obj_set_name(sup, "sup_container_meteo");
        // lv_obj_remove_style_all(sup);
        lv_obj_set_size(sup, LV_PCT(100), LV_PCT(100));
        // lv_obj_set_style_border_width(sup, 1, 0);
        // lv_obj_set_style_border_color(sup, lv_color_make(0, 255, 255), 0);
        lv_obj_add_flag(sup, LV_OBJ_FLAG_EVENT_BUBBLE);
        // lv_obj_remove_style_all(sup);
        lv_obj_set_flex_flow(sup, LV_FLEX_FLOW_ROW);
        lv_obj_add_style(sup, &style_container, 0);
        lv_obj_set_size(sup, LV_SIZE_CONTENT, LV_SIZE_CONTENT);

        lv_obj_t *dateField = lv_label_create(sup);
        lv_label_set_text(dateField, "--");

        lv_obj_t *container = lv_obj_create(sup);
        lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
        lv_obj_add_style(container, &no_padding, 0);
        lv_obj_set_width(container, 150);
        lv_obj_set_height(container, 80);

        lv_obj_t *img1 = lv_image_create(container);
        lv_obj_add_style(img1, &no_padding, 0);
        lv_obj_set_style_bg_color(img1, lv_palette_lighten(LV_PALETTE_BLUE_GREY, 4), 0);
        lv_image_set_inner_align(img1, LV_IMAGE_ALIGN_TOP_LEFT);
        lv_image_set_offset_y(img1, -8);
        lv_image_set_offset_x(img1, -5);
        lv_obj_set_size(img1, 40, 32);
        lv_image_set_src(img1, LV_SYMBOL_DUMMY);
        // lv_obj_set_style_border_width(img1,2,0);
        // lv_obj_set_style_border_color(img1, lv_palette_main(LV_PALETTE_BLUE_GREY), 0);
        lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);

        lv_obj_t *desc = lv_label_create(container);
        lv_label_set_text(desc, "--");
        lv_obj_set_style_text_font(desc, lv_theme_get_font_normal(desc), 0);

        lv_obj_t *container2 = lv_obj_create(sup);
        lv_obj_set_width(container2, LV_SIZE_CONTENT);
        lv_obj_set_height(container2, 80);
        lv_obj_add_style(container2, &no_padding, 0);
        lv_obj_set_style_pad_top(container2, 5, 0);
        lv_obj_set_style_pad_bottom(container2, 5, 0);
        lv_obj_set_flex_flow(container2, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_flex_align(container2, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);

        lv_style_init(&tempStyle);
        // lv_style_set_text_font(&tempStyle,&notomedium16);

        lv_obj_t *max = lv_label_create(container2);
        lv_label_set_text(max, "--");
        lv_obj_add_style(max, &tempStyle, 0);
        lv_obj_set_style_text_font(max, lv_theme_get_font_normal(max), 0);
        lv_obj_set_style_text_color(max, lv_color_hex(0xFF0000), 0);

        lv_obj_t *min = lv_label_create(container2);
        lv_label_set_text(min, "--");
        lv_obj_add_style(min, &tempStyle, 0);
        lv_obj_set_style_text_font(min, lv_theme_get_font_normal(max), 0);
        lv_obj_set_style_text_color(min, lv_color_hex(0x3000FF), 0);
        // On positionne un observer sur le subjet correspondant au jour du widget
        lv_subject_add_observer_obj(tmpSubj[dayNr], weatherdata_obs_cb, sup, NULL);
    }

    //return container;
}

static lv_style_t tempStyle;

// Ce fragment affiche une prévision horaire (date,icone, temp moyenne)
static lv_obj_t* weatherH_fragment_create_obj(int horaireNr, lv_obj_t *parent)
{

    lv_obj_t *sup = lv_obj_create(parent);

    lv_obj_t *title = lv_label_create(sup);
    lv_label_set_text(title, "--");

    lv_obj_set_flex_flow(sup, LV_FLEX_FLOW_ROW);
    lv_obj_add_style(sup, &style_container, 0);
    lv_obj_set_size(sup, LV_SIZE_CONTENT, LV_SIZE_CONTENT);

    lv_obj_t *container = lv_obj_create(sup);
    lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_add_style(container, &no_padding, 0);
    lv_obj_set_width(container, 150);
    lv_obj_set_height(container, LV_SIZE_CONTENT);

    //lv_style_init(&tempStyle);
    // lv_style_set_text_font(&tempStyle,&notomedium16);

    lv_obj_t *temp = lv_label_create(container);
    lv_label_set_text(temp, "--");
    lv_obj_add_style(temp, &tempStyle, 0);
    lv_obj_set_style_text_font(temp,lv_theme_get_font_normal(temp),0);
    // lv_obj_set_style_text_color(temp,lv_color_hex(0xFF0000),0);

    lv_obj_t *img1 = lv_image_create(container);
    lv_obj_add_style(img1, &no_padding, 0);
    lv_obj_set_style_bg_color(img1, lv_palette_lighten(LV_PALETTE_BLUE_GREY, 4), 0);
    lv_image_set_inner_align(img1, LV_IMAGE_ALIGN_TOP_LEFT);
    lv_image_set_offset_y(img1, -8);
    lv_image_set_offset_x(img1, -5);
    lv_obj_set_size(img1, 40, 32);
    lv_image_set_src(img1, LV_SYMBOL_DUMMY);
    // lv_obj_set_style_border_width(img1,2,0);
    // lv_obj_set_style_border_color(img1, lv_palette_main(LV_PALETTE_BLUE_GREY), 0);
    lv_obj_align(img1, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *desc = lv_label_create(container);
    lv_label_set_text(desc, "--");
    lv_obj_set_style_text_font(desc, lv_theme_get_font_normal(desc), 0);

    // On positionne un observer sur le subjet correspondant a l'horaire du widget
    ESP_LOGV(TAG, "on positionne obs sur horaire %d", horaireNr);
    lv_subject_add_observer_obj(tmpHSubj[horaireNr], weatherdataH_obs_cb, sup, NULL);
    return container;
}

uint32_t oldTab;

/*void tabChgEvt(lv_event_t *event)
{

    uint32_t tabNr = lv_tabview_get_tab_active(event->user_data);
    switch (oldTab)
    {
    case 1:
        lv_obj_clean(tabVolets);
        break;
    case 2:
        //lv_fragment_delete_obj(fragment2);;
        //lv_fragment_manager_remove(manager2,fragment2);
        ESP_LOGE(TAG, "On appelle lv_fragment_delete_obj sur l'onglet");
        lv_fragment_delete_obj(fragment2);
        ESP_LOGE(TAG, "On appelle lv_fragment_manager_pop sur l'onglet");
        lv_fragment_manager_pop(manager2);
        ESP_LOGE(TAG, "On appelle obj_clean sur l'onglet");
        lv_obj_clean(tabMeteo);
        break;
    case 3:
        lv_obj_clean(tabCuve);
        break;

    default:
        break;
    }

    oldTab = tabNr;

    printf("Tab change %lu\n", tabNr);
    switch (tabNr)
    {
    case 1:
        draw_tabVolets();
        break;
    case 2: // Onglet Météo
        draw_tabMeteo();
        break;

    case 3: // Onglet Cuve
        draw_tabCuve();
        break;

    case 4: // Onglet Settings
        draw_tabSettings();
        break;

    default:
        break;
    }
}*/

void btnRestart_cb(lv_event_t * e){
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        //esp_restart();
    }

}

static void log_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        LV_UNUSED(obj);
        char *tag=lv_event_get_user_data(e);
        if(lv_obj_has_state(obj, LV_STATE_CHECKED)){
            esp_log_level_set(tag,ESP_LOG_DEBUG);
        }else{
            esp_log_level_set(tag,ESP_LOG_INFO);
        }
        LV_LOG_USER("State for %s: %s\n", tag, lv_obj_has_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
    }
}

#define MIN(a,b) ((a) < (b) ? (a) : (b))
static void draw_event_cb(lv_event_t * e)
{
    lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
    lv_draw_dsc_base_t * base_dsc = lv_draw_task_get_draw_dsc(draw_task);

    if(base_dsc->part != LV_PART_ITEMS) {
        return;
    }

    lv_draw_fill_dsc_t * fill_dsc = lv_draw_task_get_fill_dsc(draw_task);
    if(fill_dsc) {
        lv_obj_t * chart = lv_event_get_target(e);
        int32_t * y_array = lv_chart_get_y_array(chart, ser);
        int32_t v = y_array[(base_dsc->id2+lv_chart_get_x_start_point(myChart,ser))%30];
        //ESP_LOGE(TAG, "Dessin de l'index %li, valeur: %li, start point : %li",base_dsc->id2, v, lv_chart_get_x_start_point(myChart,ser));
        /*for(int i=0;i<20;i++){
            ESP_LOGE(TAG,"Tab %i : %li", i, y_array[i]);
        }
        */
        fill_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_RED), lv_palette_main(LV_PALETTE_BLUE), MIN(v*255/1500,255));
    }
}

void draw_tabMinuteur(lv_obj_t * parent)
{
    lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
    lblEtatMachine = lv_label_create(parent);
    lv_label_set_text(lblEtatMachine, "Machine: ");
    lv_obj_set_style_text_font(lblEtatMachine, lv_theme_get_font_normal(lblEtatMachine), 0);
    myChart = lv_chart_create(parent);
    lv_chart_set_type(myChart, LV_CHART_TYPE_BAR);
    lv_chart_set_point_count(myChart, 30);
    lv_obj_set_style_pad_column(myChart, 2, 0);
    lv_obj_set_size(myChart, 600, 400);
    lv_chart_set_range(myChart, LV_CHART_AXIS_PRIMARY_Y, 0, 1500);
    lv_obj_center(myChart);

    ser = lv_chart_add_series(myChart, lv_color_hex(0xff0000), LV_CHART_AXIS_PRIMARY_Y);
    lv_obj_add_event_cb(myChart, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
    lv_obj_add_flag(myChart, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
    // uint32_t i;
    lv_chart_set_update_mode(myChart, LV_CHART_UPDATE_MODE_SHIFT);
    /*for(i = 0; i < 60; i++) {
        lv_chart_set_next_value(myChart, ser, (int32_t)lv_rand(10, 1500));
    }*/
    /*for(int i=0; i<30; i++){
        ser->y_points[i]=LV_CHART_POINT_NONE;
    }*/

}

void draw_tabSettings(lv_obj_t * parent)
{
    lv_obj_t *btnGrp = lv_obj_create(parent);
    lv_obj_set_height(btnGrp, 400);
    lv_obj_set_flex_flow(btnGrp, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(btnGrp, LV_FLEX_ALIGN_END, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    lv_obj_align(btnGrp,LV_ALIGN_BOTTOM_MID,0,0);
    lv_obj_t *btnRestart = lv_button_create(btnGrp);
    lv_obj_t *lblRestart = lv_label_create(btnRestart);
    lv_label_set_text(lblRestart,"Restart");
    lv_obj_add_event_cb(btnRestart, btnRestart_cb, LV_EVENT_CLICKED, NULL);

    lv_label_set_text(lv_label_create(btnGrp),"Meteo Debug");
    lv_obj_t *sw = lv_switch_create(btnGrp);
    lv_obj_add_state(sw, LV_STATE_DEFAULT);
    lv_obj_add_event_cb(sw, log_event_handler, LV_EVENT_ALL, "MeteoFrance");

    lv_label_set_text(lv_label_create(btnGrp),"Cuve");
    sw = lv_switch_create(btnGrp);
    lv_obj_add_state(sw, LV_STATE_DEFAULT);
    lv_obj_add_event_cb(sw, log_event_handler, LV_EVENT_ALL, "ImgDwn");

    lv_label_set_text(lv_label_create(btnGrp),"MQTT");
    sw = lv_switch_create(btnGrp);
    lv_obj_add_state(sw, LV_STATE_DEFAULT);
    lv_obj_add_event_cb(sw, log_event_handler, LV_EVENT_ALL, "domo_mqtt");


}

    lv_obj_t *tabview;

static void cuve_click_cb(lv_event_t * e){
    lv_tabview_set_active(tabview, 3, false);
}

void draw_tabHome(lv_obj_t* parent){
        lv_obj_t *supmain = parent;
    lv_obj_set_flex_flow(supmain, LV_FLEX_FLOW_COLUMN);

    lv_obj_t *main = lv_obj_create(supmain);
    lv_obj_add_style(main, &no_padding, 0);
    lv_obj_set_size(main, lv_pct(100), lv_pct(100));

    lv_obj_set_flex_flow(main, LV_FLEX_FLOW_ROW);
    lv_obj_set_style_pad_column(main, 1, 0);

    /*Conteneur Colonne: Températures*/
    lv_obj_t *cont_colTemp = lv_obj_create(main);
    lv_obj_set_style_pad_all(cont_colTemp, 5, 0);
    lv_obj_set_size(cont_colTemp, lv_pct(40), lv_pct(100));
    lv_obj_align(cont_colTemp, LV_ALIGN_TOP_LEFT, 0, 0);
    lv_obj_set_flex_flow(cont_colTemp, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(cont_colTemp, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    /*Create a container with COLUMN flex direction*/
    lv_obj_t *cont_colVolets = lv_obj_create(main);
    lv_obj_set_style_pad_all(cont_colVolets, 5, 0);
    lv_obj_set_flex_align(cont_colVolets, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_size(cont_colVolets, lv_pct(20), lv_pct(100));
    lv_obj_align_to(cont_colVolets, cont_colTemp, LV_ALIGN_OUT_TOP_RIGHT, 0, 0);
    lv_obj_set_flex_flow(cont_colVolets, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(cont_colVolets, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER);

    /*Conteneur Colonne: Meteo*/
    lv_obj_t *cont_col3 = lv_obj_create(main);
    lv_obj_set_style_pad_all(cont_col3, 5, 0);
    lv_obj_set_size(cont_col3, lv_pct(40), lv_pct(100));
    lv_obj_set_flex_flow(cont_col3, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(cont_col3, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);


    lv_obj_t *label1 = lv_label_create(cont_colTemp);
    lv_obj_set_style_text_font(label1, lv_theme_get_font_small(label1), 0);
    lv_label_set_text(label1, "Températures");
    lv_obj_set_pos(label1, 0, 0);

    //Create a container with COLUMN flex direction
    lv_obj_t *cont_tempExt = lv_obj_create(cont_colTemp);
    lv_obj_add_style(cont_tempExt, &style_container, 0);
    // lv_obj_set_height(cont_tempExt,50);
    lv_obj_set_flex_flow(cont_tempExt, LV_FLEX_FLOW_ROW);
    lv_obj_t *lblExt = lv_label_create(cont_tempExt);
    lv_label_set_text(lblExt, "ext.");
    lv_obj_t *lblTempExt = lv_label_create(cont_tempExt);

    lv_obj_add_style(lblTempExt, &style_lbvValue, 0);
    lv_label_set_text(lblTempExt, "");
    lv_label_bind_text(lblTempExt, &tempExtSubj, "%s °C");

    //Create a container with COLUMN flex direction
    /*
    lv_obj_t *cont_tempInt = lv_obj_create(cont_colTemp);
    lv_obj_add_style(cont_tempInt, &style_container, 0);
    // lv_obj_set_height(cont_tempInt,50);
    lv_obj_set_flex_flow(cont_tempInt, LV_FLEX_FLOW_ROW);
    lv_obj_t *lblInt = lv_label_create(cont_tempInt);
    lv_label_set_text(lblInt, "int.");
    lv_obj_t *lblTempInt = lv_label_create(cont_tempInt);
    lv_obj_add_style(lblTempInt, &style_lbvValue, 0);
    lv_label_set_text(lblTempInt, "");
    lv_label_bind_text(lblTempInt, &tempIntSubj, "%s °C");
    */

    //Create a container with COLUMN flex direction
    lv_obj_t *cont_tempInt2 = lv_obj_create(cont_colTemp);
    lv_obj_add_style(cont_tempInt2, &style_container, 0);
    // lv_obj_set_height(cont_tempInt,50);
    lv_obj_set_flex_flow(cont_tempInt2, LV_FLEX_FLOW_ROW);
    lv_obj_t * lblInt = lv_label_create(cont_tempInt2);
    lv_label_set_text(lblInt, "int.");
    lblTempInt2 = lv_label_create(cont_tempInt2);
    lv_obj_add_style(lblTempInt2, &style_lbvValue, 0);
    lv_obj_set_style_text_font(lblTempInt2,lv_theme_get_font_large(lblTempInt2),0);

    lv_label_set_text(lblTempInt2, "");

    //Create a container with COLUMN flex direction
    lv_obj_t *cont_Cuve = lv_obj_create(cont_colTemp);
    lv_obj_add_event_cb(cont_Cuve, cuve_click_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_style(cont_Cuve, &style_container, 0);
    lv_obj_set_flex_flow(cont_Cuve, LV_FLEX_FLOW_ROW_WRAP);
    // lv_obj_set_height(cont_Cuve,80);

    lv_obj_t *lblHauteurEau = lv_label_create(cont_Cuve);
    lv_label_set_text(lblHauteurEau, "Cuve: ");
    lblHauteurCuve = lv_label_create(cont_Cuve);
    lv_obj_add_style(lblHauteurCuve, &style_lbvValue, 0);
    //lv_label_bind_text(lblHauteurCuve, &hauteurCuveSubj, "%s cm");

    lv_obj_t *btnUp = lv_button_create(cont_colVolets);
    lv_obj_add_style(btnUp, &style_btn, 0);
    lv_obj_add_event_cb(btnUp, event_handler, LV_EVENT_ALL, upEvent);
    lv_obj_align(btnUp, LV_ALIGN_CENTER, 0, -40);
    lv_obj_remove_flag(btnUp, LV_OBJ_FLAG_PRESS_LOCK);
    lv_obj_t *label = lv_label_create(btnUp);
    lv_obj_add_style(label, &style_lbvValue, 0);
    lv_label_set_text(label, LV_SYMBOL_UP);
    lv_obj_center(label);

    lv_obj_t *btnDwn = lv_button_create(cont_colVolets);
    lv_obj_add_style(btnDwn, &style_btn, 0);
    lv_obj_add_event_cb(btnDwn, event_handler, LV_EVENT_ALL, downEvent);
    lv_obj_align(btnDwn, LV_ALIGN_CENTER, 0, -40);
    lv_obj_remove_flag(btnDwn, LV_OBJ_FLAG_PRESS_LOCK);
    label = lv_label_create(btnDwn);
    lv_obj_add_style(label, &style_lbvValue, 0);
    lv_label_set_text(label, LV_SYMBOL_DOWN);
    lv_obj_center(label);

    weatherH_fragment_create_obj(0, cont_col3);
    weatherH_fragment_create_obj(1, cont_col3);
    weatherH_fragment_create_obj(2, cont_col3);
}

void draw_tabCuve(lv_obj_t* parent)
{
    lv_obj_t *imgGraf = lv_image_create(parent);
    lv_image_set_src(imgGraf, "A:/sdcard/hello2.png");
}

void draw_tabMeteo(lv_obj_t * parent)
{
    weatherDay_fragment_create_obj(0,parent, false);
    weatherDay_fragment_create_obj(1,parent, false);
    weatherDay_fragment_create_obj(2,parent, false);
    //manager2 = lv_fragment_manager_create(NULL);
    //fragment2 = lv_fragment_create(&meteodailyforecast_cls, (void *)1);
    //lv_fragment_manager_add(manager2, fragment2, &parent);
}

void draw_tabVolets(lv_obj_t* parent)
{
    lv_obj_t *tabVolet = lv_obj_create(parent);
    lv_obj_set_flex_flow(tabVolet, LV_FLEX_FLOW_COLUMN);
    lv_obj_add_style(tabVolet, &no_padding, 0);

    lv_obj_set_size(tabVolet, lv_pct(85), lv_pct(100));
    lv_obj_t *cont_rdc = lv_obj_create(tabVolet);
    lv_obj_set_size(cont_rdc, lv_pct(100), lv_pct(100));
    lv_obj_add_style(cont_rdc, &no_padding, 0);
    lv_obj_add_style(tabVolet, &no_padding, 0);
    lv_point_t positions_rdc[] = {
        {5, 105},
        {55, 45},
        {110, 20},
        {155, 25},
        {225, 45},
        {275, 115},
        {295, 185}};
    lv_obj_t *img = lv_image_create(cont_rdc);
    //lv_img_set_src(img, &plan_rdc);
    float coef = 1.5;
    for (size_t i = 1; i < 8; i++)
    {
        lv_obj_t *btnVolet = lv_checkbox_create(cont_rdc);
        lv_obj_set_pos(btnVolet, positions_rdc[i - 1].x * coef, positions_rdc[i - 1].y * coef);
        lv_obj_set_style_text_font(btnVolet,lv_theme_get_font_normal(btnVolet),0);
        
        // lv_obj_set_size(btnVolet,100,24);

        // char *nom[10];
        // sprintf(nom,"%d", i);
        lv_checkbox_set_text(btnVolet, "");
    }

    lv_obj_t *cont_btn_volets = lv_obj_create(parent);
    lv_obj_add_style(cont_btn_volets, &no_padding, 0);
    lv_obj_set_size(cont_btn_volets, lv_pct(15), lv_pct(100));

    lv_obj_set_layout(cont_btn_volets, LV_LAYOUT_GRID);
    static int32_t column_dsc[] = {LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST}; /*2 columns */
    static int32_t row_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
    lv_obj_set_grid_dsc_array(cont_btn_volets, column_dsc, row_dsc);

    lv_obj_t *btnUp_bis = lv_button_create(cont_btn_volets);
    lv_obj_t *lblButtonUp = lv_label_create(btnUp_bis);
    lv_label_set_text(lblButtonUp, LV_SYMBOL_UP);

    lv_obj_t *btnDwn_bis = lv_button_create(cont_btn_volets);
    lv_obj_t *lblButtonDwn = lv_label_create(btnDwn_bis);
    lv_label_set_text(lblButtonDwn, LV_SYMBOL_DOWN);

    lv_obj_set_grid_cell(btnUp_bis, LV_GRID_ALIGN_STRETCH, 0, 1,
                         LV_GRID_ALIGN_STRETCH, 0, 1);
    lv_obj_set_grid_cell(btnDwn_bis, LV_GRID_ALIGN_STRETCH, 0, 1,
                         LV_GRID_ALIGN_STRETCH, 1, 1);

    lv_obj_t *cont_etage = lv_obj_create(tabVolet);
    lv_obj_set_size(cont_etage, lv_pct(100), lv_pct(100));
    lv_obj_add_style(cont_etage, &no_padding, 0);

    img = lv_image_create(cont_etage);
    //lv_img_set_src(img, &plan_etage);

    lv_point_t positions_etage[] = {
        {100, 15},
        {250, 15},
        {400, 15},
        {480, 300},
        {460, 360},
        {320, 415}};
    for (size_t i = 1; i < 7; i++)
    {
        lv_obj_t *btnVolet = lv_checkbox_create(cont_etage);
        lv_obj_set_pos(btnVolet, positions_etage[i - 1].x, positions_etage[i - 1].y);
        lv_obj_set_style_text_font(btnVolet,lv_theme_get_font_large(btnVolet),0);
        // lv_obj_set_size(btnVolet,100,24);

        // char *nom[10];
        // sprintf(nom,"%d", i);
        lv_checkbox_set_text(btnVolet, "");
    }
}


static void time_observer_cb(lv_observer_t *observer, lv_subject_t *subject)
{
    LV_UNUSED(subject);
    lv_obj_t *dateTimeObj = lv_observer_get_target_obj(observer);

    //char buf[16];

    lv_obj_t *spangroup = lv_obj_find_by_name(dateTimeObj, "date"); //lv_obj_get_child_by_id(dateTimeObj, "date");

    // lv_span_set_text(lv_spangroup_get_child(spangroup, 2), lv_subject_get_string(lv_subject_get_group_element(subject, 0)));

    if (display_lock("updateTime"))
    {
        struct tm *dateHeure =  (struct tm *)lv_subject_get_pointer(subject);
        char buff[13];

        snprintf(buff, 8, "%02" LV_PRIu32 "/",  (uint32_t)dateHeure->tm_mday);
        ESP_LOGE(TAG, "%s", buff);
        lv_span_set_text(lv_spangroup_get_child(spangroup, 0), buff);
        snprintf(buff, 4, "%02" LV_PRIu32,  (uint32_t)((dateHeure->tm_mon) + 1));
        lv_span_set_text(lv_spangroup_get_child(spangroup, 1), buff);
        lv_spangroup_refresh(spangroup);

        spangroup = lv_obj_find_by_name(dateTimeObj, "time");
        snprintf(buff,sizeof(buff), "%02" LV_PRId32,  (uint32_t)dateHeure->tm_hour);
        lv_span_set_text(lv_spangroup_get_child(spangroup, 0), buff);
       snprintf(buff,sizeof(buff), ":%02" LV_PRId32,  (uint32_t)dateHeure->tm_min);
        lv_span_set_text(lv_spangroup_get_child(spangroup, 1), buff);
        lv_spangroup_refresh(spangroup);

        display_unlock("updateTime");
    }
    }

/* ------------------------------------------------------------ */
/* Dessin IHM                                                   */
/* ------------------------------------------------------------ */

lv_color_t base = LV_COLOR_MAKE(126, 94, 133);
lv_color_t accent = LV_COLOR_MAKE(204, 16, 16);
lv_color_t bgColor = LV_COLOR_MAKE(235, 199, 158);

static lv_style_t objstyle;
static lv_style_t txtstyle;
static lv_style_t txtstyle_inv;
static lv_style_t no_padding;
static lv_style_t objstyle;
static lv_style_t txtstyle;

static char *btnDownId = "btnDwn";

typedef struct _class_info_t
{
    const lv_obj_class_t *class_p;
    uint32_t obj_count;
} class_info_t;
void *objid_array;
uint32_t objid_count;

void lv_obj_assign_id(const lv_obj_class_t *class_p, lv_obj_t *obj)
{
    LV_ASSERT(obj && class_p);

    uint32_t i;
    uint32_t id = 0;
    lv_global_t *global = LV_GLOBAL_DEFAULT();
    class_info_t *info = NULL;

    if (obj == NULL || class_p == NULL)
        return;
    if (global == NULL)
        return;

    obj->id = NULL;

    for (i = 0; i < objid_count; i++)
    {
        info = ((class_info_t *)objid_array) + i;
        if (class_p == info->class_p)
            break;
    }

    /*Resize array*/
    if (i == objid_count)
    {
        void *array = lv_realloc(objid_array, sizeof(class_info_t) * (objid_count + 1));
        LV_ASSERT_MALLOC(array);
        if (array == NULL)
            return;
        objid_array = array;
        objid_count++;
        info = ((class_info_t *)objid_array) + i;
        info->obj_count = 0;
        info->class_p = class_p;
    }

    id = ++info->obj_count;

    obj->id = (void *)(lv_uintptr_t)id;
}

void lv_obj_set_id(lv_obj_t *obj, void *id)
{
    LV_ASSERT_NULL(obj);
    if (obj->id)
        lv_obj_free_id(obj);
    obj->id = id;
}

void lv_obj_free_id(lv_obj_t *obj)
{
    LV_UNUSED(obj);
    obj->id = NULL;
}

int lv_obj_id_compare(const void *id1, const void *id2)
{
    return id1 == id2 ? 0 : 1;
}

const char *lv_obj_stringify_id(lv_obj_t *obj, char *buf, uint32_t len)
{
    const char *nameClass;
    const char *name;
    if (obj == NULL || obj->class_p == NULL)
        return NULL;
    if (buf == NULL)
        return NULL;

    nameClass = obj->class_p->name;
    if (nameClass == NULL)
        nameClass = "nameless";

    name = lv_obj_get_name(obj);
    if (name == NULL)
        name = nameClass;
    if ((lv_uintptr_t)obj->id >= 0x1000)
    {
        lv_snprintf(buf, len, " name_id:%s_%s", name, obj->id);
    }
    else
    {
        lv_snprintf(buf, len, " name:%s", name);
    }
    return buf;
}

static void blink_timer_cb(lv_timer_t *timer)
{
    lv_obj_t *obj = timer->user_data;

    if (lv_obj_has_flag(obj, LV_OBJ_FLAG_HIDDEN))
        lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
    else
        lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
}

void blink_start(lv_obj_t *obj, uint32_t period_ms)
{
    lv_timer_create(blink_timer_cb, period_ms, obj);
}
static void anim_opa_cb(void *obj, int32_t v)
{
    if (display_lock_logChoose("msg fade", false))
    {
        lv_obj_set_style_opa(obj, v, 0);
        display_unlock_logChoose("msg fade",false);
    }
}
void blink_fade(lv_obj_t *obj)
{
    lv_anim_t a;
    lv_anim_init(&a);

    lv_anim_set_var(&a, obj);
    lv_anim_set_exec_cb(&a, anim_opa_cb);
    lv_anim_set_values(&a, LV_OPA_TRANSP, LV_OPA_COVER);
    lv_anim_set_time(&a, 500);
    lv_anim_set_playback_time(&a, 1000);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);

    lv_anim_start(&a);
}

void messageCardContent(lv_obj_t *cont_messages)
{
    lv_obj_t * msg = lv_label_create(cont_messages);
    lv_label_set_text(msg, "\xEF\x89\xBA Vous avez un message");
    //blink_start(msg, 1000); // clignote toutes les 500 ms
    blink_fade(msg);
    // lv_obj_set_style_text_color(msg, lv_color_white(), 0);
}

    void coverCardContent(lv_obj_t * cont_colVolets)
    {
        lv_obj_set_style_pad_all(cont_colVolets, 5, 0);
        lv_obj_set_flex_align(cont_colVolets, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
        // lv_obj_set_size(cont_colVolets, lv_pct(60), lv_pct(70));
        lv_obj_set_flex_flow(cont_colVolets, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_flex_align(cont_colVolets, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER);

        lv_obj_t *btnUp = lv_button_create(cont_colVolets);
        lv_obj_set_id(btnUp, (void *)"CV_BT_UP");
        lv_obj_add_style(btnUp, &style_btn, 0);
        lv_obj_add_event_cb(btnUp, event_handler, LV_EVENT_ALL, upEvent);
        // lv_obj_align(btnUp, LV_ALIGN_CENTER, 0, -40);
        lv_obj_remove_flag(btnUp, LV_OBJ_FLAG_PRESS_LOCK);
        lv_obj_t *label = lv_label_create(btnUp);
        lv_obj_add_style(label, &style_lbvValue, 0);
        lv_label_set_text(label, LV_SYMBOL_UP);
        lv_obj_center(label);

        lv_obj_t *btnDwn = lv_button_create(cont_colVolets);
        lv_obj_set_id(btnDwn, (void *)"CV_BT_DW");
        lv_obj_set_name(btnDwn, "btnDown_#");

        lv_obj_add_style(btnDwn, &style_btn, 0);
        lv_obj_add_event_cb(btnDwn, event_handler, LV_EVENT_ALL, downEvent);
        // lv_obj_align(btnDwn, LV_ALIGN_CENTER, 0, -40);
        lv_obj_remove_flag(btnDwn, LV_OBJ_FLAG_PRESS_LOCK);
        label = lv_label_create(btnDwn);
        lv_obj_add_style(label, &style_lbvValue, 0);
        lv_label_set_text(label, LV_SYMBOL_DOWN);
        lv_obj_center(label);
    }
    void meteoCardContent(lv_obj_t * container)
    {
        // lv_obj_t *txt = lv_label_create(container);
        // lv_label_set_text(txt, "Meteo");
        weatherDay_fragment_create_obj(0, container, true);
        // lv_subject_add_observer_obj(&forecastD1Subj, weatherdata_obs_cb, container, NULL);
    }
    void minuteurCardContent(lv_obj_t * container)
    {
        lv_obj_t *txt = lv_label_create(container);
        lv_label_set_text(txt, "Rien à signaler !");
    }

    static void backCb(lv_event_t * e)
    {
        ESP_LOGE(TAG, "We're here !!!!!!!!!!!!!!!!!!!!!!");
        drawHome();
    }

    static void event_cb(lv_event_t * e)
    {
        LV_LOG_USER("Clicked");

        static uint32_t cnt = 1;
        lv_obj_t *btn = lv_event_get_target_obj(e);
        lv_obj_t *label = lv_obj_get_child(btn, 0);
        lv_label_set_text_fmt(label, "%" LV_PRIu32, cnt);
        cnt++;
    }

    void drawMessagerie(lv_obj_t *base_obj)
    {

        lv_obj_t *meteoContainer = lv_obj_create(base_obj);
        //lv_obj_remove_style_all(meteoContainer);
        lv_obj_set_size(meteoContainer, LV_PCT(100), LV_PCT(100));
        lv_obj_set_flex_flow(meteoContainer, LV_FLEX_FLOW_COLUMN);

        lv_obj_t *back = lv_label_create(meteoContainer);
        lv_obj_add_flag(back, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_id(back, "backBtn");
        lv_label_set_text(back, LV_SYMBOL_HOME);
        //lv_obj_set_style_border_color(back, lv_color_make(255, 0, 0), 0);
        //lv_obj_set_style_border_width(back, 1, 0);
        lv_obj_set_style_text_font(back, lv_theme_get_font_large(back), 0);
        //lv_obj_set_style_text_color(back, lv_color_white(), 0);
        lv_obj_add_event_cb(back, backCb, LV_EVENT_CLICKED, NULL);

        lv_obj_t *title = lv_label_create(meteoContainer);
        lv_obj_set_id(title, "titleMeteoLbl");
        lv_label_set_text(title, "Centre de messages");
        lv_obj_set_style_text_font(title, lv_theme_get_font_large(title), 0);
        lv_obj_set_style_text_color(title, lv_color_white(), 0);
        lv_obj_add_event_cb(title, backCb, LV_EVENT_CLICKED, NULL);

    }

    void drawMinuteur(lv_obj_t *base_obj)
    {
        // lv_example_event_click();

        lv_obj_t *meteoContainer = lv_obj_create(base_obj);
        //lv_obj_remove_style_all(meteoContainer);
        lv_obj_set_size(meteoContainer, LV_PCT(100), LV_PCT(100));
        lv_obj_set_flex_flow(meteoContainer, LV_FLEX_FLOW_COLUMN);

        lv_obj_t *back = lv_label_create(meteoContainer);
        lv_obj_add_flag(back, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_id(back, "backBtn");
        lv_label_set_text(back, LV_SYMBOL_HOME);
        //lv_obj_set_style_border_color(back, lv_color_make(255, 0, 0), 0);
        //lv_obj_set_style_border_width(back, 1, 0);
        lv_obj_set_style_text_font(back, lv_theme_get_font_large(back), 0);
        lv_obj_set_style_text_color(back, lv_color_white(), 0);
        lv_obj_add_event_cb(back, backCb, LV_EVENT_CLICKED, NULL);

        lv_obj_t *title = lv_label_create(meteoContainer);
        lv_obj_set_id(title, "titleMeteoLbl");
        lv_label_set_text(title, "Qu'est ce qui tourne ?");
        lv_obj_set_style_text_font(title, lv_theme_get_font_large(title), 0);
        lv_obj_set_style_text_color(title, lv_color_white(), 0);
        lv_obj_add_event_cb(title, backCb, LV_EVENT_CLICKED, NULL);

        draw_tabMinuteur(meteoContainer);

    }

    void drawVolets(lv_obj_t *base_obj)
    {
        // lv_example_event_click();

        lv_obj_t *meteoContainer = lv_obj_create(base_obj);
        //lv_obj_remove_style_all(meteoContainer);
        lv_obj_set_size(meteoContainer, LV_PCT(100), LV_PCT(100));
        lv_obj_set_flex_flow(meteoContainer, LV_FLEX_FLOW_COLUMN);

        lv_obj_t *back = lv_label_create(meteoContainer);
        lv_obj_add_flag(back, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_id(back, "backBtn");
        lv_label_set_text(back, LV_SYMBOL_HOME);
        //lv_obj_set_style_border_color(back, lv_color_make(255, 0, 0), 0);
        //lv_obj_set_style_border_width(back, 1, 0);
        lv_obj_set_style_text_font(back, lv_theme_get_font_large(back), 0);
        lv_obj_set_style_text_color(back, lv_color_white(), 0);
        lv_obj_add_event_cb(back, backCb, LV_EVENT_CLICKED, NULL);

        lv_obj_t *title = lv_label_create(meteoContainer);
        lv_obj_set_id(title, "titleMeteoLbl");
        lv_label_set_text(title, "Gestion des volets");
        lv_obj_set_style_text_font(title, lv_theme_get_font_large(title), 0);
        lv_obj_set_style_text_color(title, lv_color_white(), 0);
        lv_obj_add_event_cb(title, backCb, LV_EVENT_CLICKED, NULL);

        draw_tabVolets(meteoContainer);

    }


    void drawMeteo(lv_obj_t *base_obj)
    {
        // lv_example_event_click();

        lv_obj_t *meteoContainer = lv_obj_create(base_obj);
        //lv_obj_remove_style_all(meteoContainer);
        lv_obj_set_size(meteoContainer, LV_PCT(100), LV_PCT(100));
        lv_obj_set_flex_flow(meteoContainer, LV_FLEX_FLOW_COLUMN);

        lv_obj_t *back = lv_label_create(meteoContainer);
        lv_obj_add_flag(back, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_id(back, "backBtn");
        lv_label_set_text(back, LV_SYMBOL_HOME);
        //lv_obj_set_style_border_color(back, lv_color_make(255, 0, 0), 0);
        //lv_obj_set_style_border_width(back, 1, 0);
        lv_obj_set_style_text_font(back, lv_theme_get_font_large(back), 0);
        lv_obj_set_style_text_color(back, lv_color_white(), 0);
        lv_obj_add_event_cb(back, backCb, LV_EVENT_CLICKED, NULL);

        lv_obj_t *title = lv_label_create(meteoContainer);
        lv_obj_set_id(title, "titleMeteoLbl");
        lv_label_set_text(title, "Météo du jour");
        lv_obj_set_style_text_font(title, lv_theme_get_font_large(title), 0);
        lv_obj_set_style_text_color(title, lv_color_white(), 0);
        lv_obj_add_event_cb(title, backCb, LV_EVENT_CLICKED, NULL);

        draw_tabMeteo(meteoContainer);

    }

    void messagerieCb(lv_obj_t *base_obj)
    {
        drawMessagerie(base_obj);
    }
    void minuteurCb(lv_obj_t *base_obj)
    {
        drawMinuteur(base_obj);
    }
    void voletsCb(lv_obj_t *base_obj)
    {
        drawVolets(base_obj);
    }
    void meteoCb(lv_obj_t *base_obj)
    {
        drawMeteo(base_obj);
    }

    void drawHome()
    {

        /*Initialize the styles*/
        lv_style_init(&style_btn);
        // lv_style_set_bg_color(&style_btn, lv_palette_main(LV_PALETTE_GREEN));
        // lv_style_set_border_color(&style_btn, lv_palette_darken(LV_PALETTE_GREEN, 3));
        // lv_style_set_border_width(&style_btn, 3);

        lv_subject_init_string(&tempExtSubj, tempExtStr, NULL, 6, "--");
        lv_subject_init_string(&tempIntSubj, tempIntStr, NULL, 6, "--");
        lv_subject_init_string(&hauteurCuveSubj, hauteurCuveStr, NULL, 9, "--");
        lv_subject_init_string(&hauteurCuveEvolSubj, hauteurCuveEvolStr, NULL, 9, "--");

        // keys.clear();
        lv_obj_clean(lv_scr_act());

        lv_obj_t *bg_cont = lv_obj_create(lv_scr_act());
        bg_cont->id = "bgObj1";
        lv_obj_set_id(bg_cont, (void *)"bgObj1");
        lv_obj_set_name(bg_cont, "bgObj1#");

        //lv_obj_remove_style_all(bg_cont);
        lv_obj_set_size(bg_cont, LV_PCT(100), LV_PCT(100));
        lv_obj_set_flex_flow(bg_cont, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_style_pad_top(bg_cont, 5, 0);
        lv_obj_set_style_bg_color(bg_cont, bgColor, 0);

        lv_style_init(&objstyle);
        lv_style_set_bg_color(&objstyle, accent);
        lv_style_set_outline_color(&objstyle, accent);

        /* info area */

        lv_obj_t *info_area = lv_obj_create(bg_cont);
        lv_obj_set_name(info_area, "infoArea");
        //lv_obj_remove_style_all(info_area);
        lv_obj_set_width(info_area, LV_PCT(100));
        // lv_obj_set_style_pad_left(info_area, c->sz->gap[10], 0);
        // lv_obj_set_style_pad_right(info_area, c->sz->gap[10], 0);
        lv_obj_set_flex_grow(info_area, 1);
        lv_obj_set_flex_flow(info_area, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(info_area, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

        lv_obj_t *date_and_time = lv_obj_create(info_area);
        lv_obj_set_align(date_and_time, LV_ALIGN_CENTER);
        lv_obj_set_flex_flow(date_and_time, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_flex_align(date_and_time, LV_FLEX_ALIGN_CENTER,LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
        lv_obj_set_name(date_and_time, "date_time");
        //lv_obj_remove_style_all(date_and_time);
        lv_obj_set_size(date_and_time, LV_SIZE_CONTENT, LV_SIZE_CONTENT);

        lv_obj_t *date = lv_spangroup_create(date_and_time);
        lv_obj_set_name(date, "date");
        lv_obj_set_id(date, "date");
        // lv_obj_add_style(date, &c->fonts[FONT_HEADING_MD], 0);
        // lv_obj_add_style(date, &c->styles[STYLE_COLOR_BASE][STYLE_TYPE_TEXT], 0);
        lv_span_t *day = lv_spangroup_add_span(date);
        lv_span_set_text(day, "19/");
        lv_style_set_text_font(lv_span_get_style(day), &super_malibu_80);
        lv_style_set_text_color(lv_span_get_style(day), base);
        lv_style_set_line_color(lv_span_get_style(day), lv_color_black());
        lv_style_set_line_width(lv_span_get_style(day), 2);
        lv_span_t *month = lv_spangroup_add_span(date);
        lv_span_set_text(month, "12");
        lv_style_set_text_font(lv_span_get_style(month), &super_malibu_80);

        lv_obj_t *time = lv_spangroup_create(date_and_time);
        lv_obj_set_name(time, "time");
        lv_obj_set_id(time, "time");
        // lv_obj_add_style(time, &txtstyle, 0);
        lv_span_t *hourSpan = lv_spangroup_add_span(time);
        lv_span_set_text(hourSpan, "21");
        lv_style_set_text_font(lv_span_get_style(hourSpan), &super_malibu_80);
        lv_span_t *minute = lv_spangroup_add_span(time);
        lv_span_set_text(minute, ":56");
        lv_style_set_text_color(lv_span_get_style(minute), base);
        lv_style_set_line_color(lv_span_get_style(minute), accent);
        lv_style_set_text_font(lv_span_get_style(minute), &super_malibu_80);
        //lv_subject_add_observer_obj(&c->th, theme_observer_accent_span_cb, time, minute);

        lv_subject_add_observer_obj(&timeSubj, time_observer_cb, date_and_time, NULL);

        lv_obj_t * subContent = lv_obj_create(info_area);
        lv_obj_set_flex_flow(subContent, LV_FLEX_FLOW_COLUMN);
        lv_obj_set_size(subContent, LV_PCT(70), LV_PCT(100));
        lv_obj_t *lblTempInt = lv_label_create(subContent);
        lv_label_set_text(lblTempInt, "\xEE\x86\xB0\xEF\x8B\x8A : 21°C");
        lv_obj_set_style_text_font(lblTempInt, lv_theme_get_font_large(lblTempInt),0);
        lv_obj_t *lblTempExt = lv_label_create(subContent);
        lv_label_set_text(lblTempExt, LV_SYMBOL_HOME "\xEF\x8B\x8A : 12°C");
        lv_obj_set_style_text_font(lblTempExt, lv_theme_get_font_large(lblTempExt),0);

        /*for (size_t i = 0; i < 2; i++)
        {
            lv_obj_t *lblNextEvt = lv_label_create(subContent);
            lv_obj_set_style_margin_top(lblNextEvt, 15, 0);
            lv_label_set_text(lblNextEvt, events[i].affichage);
            lv_obj_set_style_text_font(lblNextEvt, &roboto_medium_36, 0);
        }*/

        lv_obj_t *apps = lv_obj_create(bg_cont);
        lv_obj_set_size(apps, LV_PCT(100), LV_SIZE_CONTENT);
        /*lv_obj_set_style_pad_bottom(apps, 8, 0);
        lv_obj_set_style_pad_left(apps, 8, 0);
        lv_obj_set_style_pad_right(apps, 8, 0);
        lv_obj_set_style_border_side(apps, LV_BORDER_SIDE_BOTTOM, 0);
        lv_obj_set_style_border_color(apps, lv_color_black(), 0);
        lv_obj_set_style_border_width(apps, 1, 0);
        lv_obj_set_style_border_opa(apps, 8 * 255 / 100, 0);
        */
        lv_obj_set_flex_flow(apps, LV_FLEX_FLOW_ROW);
        lv_obj_set_style_pad_column(apps, 6, 0);

        create_card(apps, "Météo", meteoCardContent, meteoCb);

        create_card(apps, "Volets", coverCardContent, voletsCb);
        create_card(apps, "Minuteur", minuteurCardContent, minuteurCb);
        create_card(apps, "Messagerie", messageCardContent, messagerieCb);

        return;
    }

    void oldIhm(){
        // ***************************************************************************************************
        // Create a Tab view object
        tabview = lv_tabview_create(lv_screen_active());
        lv_tabview_set_tab_bar_position(tabview, LV_DIR_LEFT);
        lv_tabview_set_tab_bar_size(tabview, 80);
        // lv_obj_add_event_cb(tabview, tabChgEvt, LV_EVENT_VALUE_CHANGED, tabview); // Assign an event callback
        //  lv_obj_set_style_bg_color(tabview, lv_palette_lighten(LV_PALETTE_RED, 2), 0);

        lv_obj_t *tab_buttons = lv_tabview_get_tab_bar(tabview);
        lv_obj_set_width(tab_buttons, 120);
        lv_obj_set_style_text_font(tab_buttons, lv_theme_get_font_large(tab_buttons), 0);
        lv_obj_set_style_bg_color(tab_buttons, lv_palette_darken(LV_PALETTE_GREY, 3), 0);
        lv_obj_set_style_text_color(tab_buttons, lv_palette_lighten(LV_PALETTE_GREY, 5), 0);
        lv_obj_set_style_border_side(tab_buttons, LV_BORDER_SIDE_RIGHT, LV_PART_ITEMS | LV_STATE_CHECKED);

        // Add 3 tabs (the tabs are page (lv_page) and can be scrolled
        lv_obj_t *tab0 = lv_tabview_add_tab(tabview, LV_SYMBOL_HOME);
        lv_obj_t *tabVolets = lv_tabview_add_tab(tabview, "Volets");
        lv_obj_t *tabMeteo = lv_tabview_add_tab(tabview, "Météo");
        lv_obj_t *tabCuve = lv_tabview_add_tab(tabview, "\xEF\x95\xB5"
                                                        "Cuve");
        lv_obj_t *tabMinuteur = lv_tabview_add_tab(tabview, "Minuteur");
        lv_obj_t *tabSettings = lv_tabview_add_tab(tabview, "Settings");

        // lv_obj_set_style_bg_color(tab2b, lv_palette_lighten(LV_PALETTE_AMBER, 3), 0);
        lv_obj_set_style_bg_opa(tabVolets, LV_OPA_COVER, 0);

        // lv_style_init(&style_btn);

        // lv_style_set_bg_color(&style_btn, lv_color_hex(0x115588));
        // lv_style_set_bg_opa(&style_btn, LV_OPA_50);
        // lv_style_set_border_width(&style_btn, 2);
        // lv_style_set_border_color(&style_btn, lv_color_black());
        // lv_style_set_width(&style_btn, 80);
        // lv_style_set_height(&style_btn, 100);

        // Un style pour les conteneurs (température, cuve ...)
        lv_style_init(&style_container);
        lv_style_set_pad_all(&style_container, 5);
        // lv_style_set_height(&style_container,LV_SIZE_CONTENT);
        lv_style_set_height(&style_container, 80);
        lv_style_set_width(&style_container, lv_pct(80));
        lv_style_set_bg_color(&style_container, lv_palette_lighten(LV_PALETTE_BLUE_GREY, 4));
        lv_style_set_align(&style_container, LV_ALIGN_BOTTOM_LEFT);
        lv_style_set_flex_cross_place(&style_container, LV_FLEX_ALIGN_END);

        lv_style_init(&no_padding);
        lv_style_set_pad_all(&no_padding, 0);

        lv_style_init(&style_lbvValue);
        lv_style_set_text_font(&style_lbvValue, &lv_font_montserrat_40);

        draw_tabHome(tab0);
        draw_tabVolets(tabVolets);
        draw_tabMeteo(tabMeteo);
        draw_tabCuve(tabCuve);
        draw_tabSettings(tabSettings);
        draw_tabMinuteur(tabMinuteur);

        lv_obj_set_flex_flow(tabVolets, LV_FLEX_FLOW_ROW);

        lv_obj_set_flex_flow(tabMeteo, LV_FLEX_FLOW_COLUMN);
}

typedef void (*app_cb_t)(lv_obj_t *base_obj);

static void app_card_click_cb(lv_event_t *e)
{
    lv_obj_t *app_card = lv_event_get_current_target(e);
    app_cb_t app_cb = (app_cb_t)((uintptr_t)lv_event_get_user_data(e));

    lv_obj_t *base_obj = lv_obj_get_parent(lv_obj_get_parent(lv_obj_get_parent(app_card)));
    lv_obj_clean(base_obj);
    app_cb(base_obj);
}

void create_card(lv_obj_t *parent, char *lbl, void (*contentFct)(lv_obj_t *), app_cb_t app_cb)
{
    //lv_style_init(&objstyle);
    //lv_style_init(&txtstyle);
    lv_style_set_bg_color(&objstyle, base);
    lv_style_set_outline_color(&objstyle, base);

    lv_style_set_text_color(&txtstyle, lv_color_white());
    //lv_style_set_line_color(&txtstyle, accent);
    lv_style_set_text_font(&txtstyle, lv_theme_get_font_large(parent));
    // if(icon_img_dsc == NULL) return;
    lv_obj_t *app_card = lv_obj_create(parent);
    lv_obj_set_id(app_card, "app_card");
    lv_obj_set_name(app_card, "app_card");
    //lv_obj_remove_style_all(app_card);
    lv_obj_set_size(app_card, 300, 200);
    //lv_obj_set_size(app_card, 100, 60);
    lv_obj_set_style_radius(app_card, 20, 0);
    lv_obj_set_style_bg_opa(app_card, 50, 0);
    lv_obj_add_style(app_card, &objstyle, 0);
    lv_obj_add_event_cb(app_card, app_card_click_cb, LV_EVENT_CLICKED, (void *)((lv_uintptr_t)app_cb));

    lv_obj_t *label = lv_label_create(app_card);
    lv_obj_set_name(label, "card_label");
    lv_label_set_text(label, lbl);
    lv_obj_set_align(label, LV_ALIGN_BOTTOM_LEFT);
    lv_obj_add_style(label, &txtstyle, 0);

    lv_obj_t *icon = lv_image_create(app_card);
    lv_obj_set_name(icon, "card_icon");
    // lv_obj_set_size(icon, icon_img_dsc->header.w, icon_img_dsc->header.h);
    lv_image_set_inner_align(icon, LV_IMAGE_ALIGN_CENTER);
    // lv_image_set_src(icon, icon_img_dsc);
    lv_obj_set_style_bg_opa(icon, LV_OPA_COVER, 0);
    lv_obj_set_style_bg_color(icon, lv_color_white(), 0);
    lv_obj_set_style_radius(icon, LV_COORD_MAX, 0);
    // lv_obj_add_style(icon, &c->styles[STYLE_COLOR_ACCENT][STYLE_TYPE_A8_IMG], 0);
    lv_obj_add_flag(icon, LV_OBJ_FLAG_EVENT_BUBBLE);

    lv_obj_t *content = lv_obj_create(app_card);
    lv_obj_add_flag(content, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_set_name(content, "card_content");
    lv_obj_set_size(content, LV_PCT(100), LV_PCT(70));
    lv_obj_set_style_pad_all(content,1,0);
    lv_obj_set_style_bg_opa(content, 0, 0);
    //lv_obj_set_align(content, LV_ALIGN_BOTTOM_RIGHT);
    //lv_obj_set_style_border_width(content,4,0);
    contentFct(content);
}

void log_cb(lv_log_level_t level, const char * buf){
    ESP_LOGE(TAG, "%s",buf);
}
