/**
 * @file lv_theme_simple.c
 *
 */

#define LV_USE_THEME_SIMPLE_DOMOTIC 1
/*********************
 *      INCLUDES
 *********************/
#if LV_USE_THEME_SIMPLE_DOMOTIC
#include "lv_theme_domotic.h"
#include "lvgl_private.h"
#include <stdio.h>
#include <stdlib.h>

/*********************
 *      DEFINES
 *********************/
LV_FONT_DECLARE(montserrat_medium_12)
LV_FONT_DECLARE(montserrat_medium_18)
LV_FONT_DECLARE(montserrat_medium_24)
LV_FONT_DECLARE(roboto_medium_72)


void * theme_domotic;

#define theme_def (*(domotic_theme_t **)(&theme_domotic))

#define COLOR_SCR     lv_palette_lighten(LV_PALETTE_GREY, 4)
#define COLOR_WHITE   lv_color_white()
#define COLOR_LIGHT   lv_palette_lighten(LV_PALETTE_GREY, 2)
#define COLOR_DARK    lv_palette_main(LV_PALETTE_GREY)
#define COLOR_DIM     lv_palette_darken(LV_PALETTE_GREY, 2)
#define SCROLLBAR_WIDTH     2

/**********************
 *      TYPEDEFS
 **********************/
typedef struct {
    lv_style_t scr;
    lv_style_t transp;
    lv_style_t white;
    lv_style_t light;
    lv_style_t dark;
    lv_style_t dim;
    lv_style_t scrollbar;
    lv_style_t btn;
    lv_style_t card;
#if LV_USE_ARC
    lv_style_t arc_line;
    lv_style_t arc_knob;
#endif
#if LV_USE_TEXTAREA
    lv_style_t ta_cursor;
#endif
} my_theme_styles_t;

struct _domotic_theme_t {
    lv_theme_t base;
    my_theme_styles_t styles;
    lv_color_t color_scr;
    lv_color_t color_text;
    lv_color_t color_card;
    lv_color_t color_grey;
    bool inited;
};

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void style_init_reset(lv_style_t * style);
static void theme_apply(lv_theme_t * th, lv_obj_t * obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void style_init(domotic_theme_t * theme)
{
    theme->color_scr = lv_palette_lighten(LV_PALETTE_BLUE_GREY, 4);
    theme->color_text = lv_palette_lighten(LV_PALETTE_BLUE_GREY, 4);
    theme->color_card = lv_color_white();
    theme->color_grey = lv_palette_lighten(LV_PALETTE_BLUE_GREY, 2);

    style_init_reset(&theme->styles.scr);
    lv_style_set_bg_opa(&theme->styles.scr, LV_OPA_COVER);
    lv_style_set_bg_color(&theme->styles.scr, theme->color_scr);
    lv_style_set_text_color(&theme->styles.scr, theme->color_text);
    lv_style_set_text_font(&theme->styles.scr, theme->base.font_normal);
    //lv_style_set_pad_row(&theme->styles.scr, PAD_SMALL);
    //lv_style_set_pad_column(&theme->styles.scr, PAD_SMALL);
    //lv_style_set_rotary_sensitivity(&theme->styles.scr, theme->disp_dpi / 4 * 256);
    style_init_reset(&theme->styles.btn);
    //lv_style_set_radius(&theme->styles.btn,LV_DPX_CALC(theme->disp_dpi, theme->disp_size == DISP_LARGE ? 16 : theme->disp_size == DISP_MEDIUM ? 12: 8));
    lv_style_set_bg_opa(&theme->styles.btn, LV_OPA_COVER);
    lv_style_set_bg_color(&theme->styles.btn, theme->color_grey);
    /*if (!(theme->base.flags & MODE_DARK))
    {
        lv_style_set_shadow_color(&theme->styles.btn, lv_palette_main(LV_PALETTE_GREY));
        lv_style_set_shadow_width(&theme->styles.btn, LV_DPX(3));
        lv_style_set_shadow_opa(&theme->styles.btn, LV_OPA_50);
        lv_style_set_shadow_offset_y(&theme->styles.btn, LV_DPX_CALC(theme->disp_dpi, LV_DPX(4)));
    }*/
    lv_style_set_text_color(&theme->styles.btn, theme->color_text);
    //lv_style_set_pad_hor(&theme->styles.btn, PAD_DEF);
    //lv_style_set_pad_ver(&theme->styles.btn, PAD_SMALL);
    //lv_style_set_pad_column(&theme->styles.btn, LV_DPX_CALC(theme->disp_dpi, 5));
    //lv_style_set_pad_row(&theme->styles.btn, LV_DPX_CALC(theme->disp_dpi, 5));

    style_init_reset(&theme->styles.card);
    //lv_style_set_radius(&theme->styles.card, RADIUS_DEFAULT);
    lv_style_set_bg_opa(&theme->styles.card, 10);
    lv_style_set_bg_color(&theme->styles.card, theme->color_card);
    lv_style_set_border_color(&theme->styles.card, theme->color_grey);
    //lv_style_set_border_width(&theme->styles.card, BORDER_WIDTH);
    lv_style_set_border_post(&theme->styles.card, true);
    lv_style_set_text_color(&theme->styles.card, theme->color_text);
    lv_style_set_text_font(&theme->styles.card, theme->base.font_normal);
    lv_style_set_pad_all(&theme->styles.card, 16);
    lv_style_set_pad_row(&theme->styles.card, 10);
    lv_style_set_pad_column(&theme->styles.card, 10);
    lv_style_set_line_color(&theme->styles.card, lv_palette_main(LV_PALETTE_GREY));
    //lv_style_set_line_width(&theme->styles.card, LV_DPX_CALC(theme->disp_dpi, 1));
}

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

bool lv_theme_domotic_is_inited(void)
{
    domotic_theme_t * theme = theme_def;
    if(theme == NULL) return false;
    return theme->inited;
}

lv_theme_t * lv_theme_domotic_get(void)
{
    if(!lv_theme_domotic_is_inited()) {
        return NULL;
    }

    return (lv_theme_t *)theme_def;
}

void lv_theme_domotic_deinit(void)
{
    domotic_theme_t * theme = theme_def;
    if(theme) {
        if(theme->inited) {
            lv_style_t * theme_styles = (lv_style_t *)(&(theme->styles));
            uint32_t i;
            for(i = 0; i < sizeof(my_theme_styles_t) / sizeof(lv_style_t); i++) {
                lv_style_reset(theme_styles + i);
            }
        }
        lv_free(theme_def);
        theme_def = NULL;
    }
}

lv_theme_t * lv_theme_domotic_init(lv_display_t * disp)
{
    /*This trick is required only to avoid the garbage collection of
     *styles' data if LVGL is used in a binding (e.g. MicroPython)
     *In a general case styles could be in simple `static lv_style_t my_style...` variables*/
    if(!lv_theme_domotic_is_inited()) {
        theme_def  = lv_malloc_zeroed(sizeof(domotic_theme_t));
    }

    domotic_theme_t * theme = theme_def;

    theme->base.disp = disp;
    theme->base.font_small = &montserrat_medium_12;
    theme->base.font_normal = &montserrat_medium_18;
    theme->base.font_large = &montserrat_medium_24;
    theme->base.apply_cb = theme_apply;

    lv_theme_t * th_simple = lv_theme_simple_init(disp);
    lv_theme_set_parent((lv_theme_t *)theme, th_simple);

    style_init(theme);

    if(disp == NULL || lv_display_get_theme(disp) == (lv_theme_t *)theme) {
        lv_obj_report_style_change(NULL);
    }

    theme->inited = true;

    return (lv_theme_t *)theme_def;
}

static void theme_apply(lv_theme_t * th, lv_obj_t * obj)
{
    LV_UNUSED(th);

    domotic_theme_t * theme = theme_def;
    lv_obj_t * parent = lv_obj_get_parent(obj);

    if(parent == NULL) {
        lv_obj_add_style(obj, &theme->styles.scr, 0);
        lv_obj_add_style(obj, &theme->styles.scrollbar, LV_PART_SCROLLBAR);
        return;
    }

    // On ne passe pas par une variable KCONFIG pour ne pas avoir a recompiler
    const char *borderEnv = getenv("DOM_SHOW_BORDER");
    int border = atoi(borderEnv ? borderEnv : "0");
    if(border)
        lv_obj_set_style_border_width(obj,1,0);

    if (lv_obj_check_type(obj, &lv_obj_class))
    {
        lv_obj_add_style(obj, &theme->styles.card, 0);
        lv_obj_add_style(obj, &theme->styles.scrollbar, LV_PART_SCROLLBAR);
        //lv_obj_add_style(obj, &theme->styles.scrollbar_scrolled, LV_PART_SCROLLBAR | LV_STATE_SCROLLED);
    }
    //lv_obj_set_style_text_color(obj, lv_color_white(), 0);
    //lv_obj_set_style_text_font(obj, ((my_theme_t*)theme_domotic)->base.font_normal,0);

    if (lv_obj_check_type(obj, &lv_button_class)){
        lv_obj_add_style(obj, &theme->styles.btn,0);
        lv_obj_set_style_text_color(obj, lv_color_black(), 0);
        lv_obj_set_style_text_font(obj, theme->base.font_large, 0);
        //lv_obj_set_style_bg_color(obj, lv_color_hex(0x6B6969), 0);
        lv_obj_set_style_pad_all(obj,10,0);
        lv_obj_set_style_margin_all(obj, 0, 0);
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void style_init_reset(lv_style_t * style)
{
    if(lv_theme_simple_is_inited()) {
        lv_style_reset(style);
    }
    else {
        lv_style_init(style);
    }
}

#endif
