/**
 * @file lv_sysmon.c
 *
 */

/*********************
 *      INCLUDES
 *********************/

#include "lv_sysmon.h"

#if LV_USE_SYSMON

#include "../../core/lv_global.h"
#include "../../misc/lv_async.h"
#include "../../stdlib/lv_string.h"
#include "../../widgets/label/lv_label.h"

/*********************
 *      DEFINES
 *********************/
#define MY_CLASS &lv_sysmon_class

#define SYSMON_REFR_PERIOD_DEF 300 /* ms */

#if LV_USE_PERF_MONITOR
    #define sysmon_perf LV_GLOBAL_DEFAULT()->sysmon_perf
#endif

#if LV_USE_MEM_MONITOR
    #define sysmon_mem LV_GLOBAL_DEFAULT()->sysmon_mem
#endif

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void sysmon_backend_init_async_cb(void * user_data);

#if LV_USE_PERF_MONITOR
    static void perf_update_timer_cb(lv_timer_t * t);
    static void perf_observer_cb(lv_subject_t * subject, lv_observer_t * observer);
#endif

#if LV_USE_MEM_MONITOR
    static void mem_update_timer_cb(lv_timer_t * t);
    static void mem_observer_cb(lv_subject_t * subject, lv_observer_t * observer);
#endif

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

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

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

void _lv_sysmon_builtin_init(void)
{
#if LV_USE_PERF_MONITOR
    static lv_sysmon_perf_info_t perf_info;
    lv_subject_init_pointer(&sysmon_perf.subject, &perf_info);
    sysmon_perf.timer = lv_timer_create(perf_update_timer_cb, SYSMON_REFR_PERIOD_DEF, &perf_info);
#endif

#if LV_USE_MEM_MONITOR
    static lv_mem_monitor_t mem_info;
    lv_subject_init_pointer(&sysmon_mem.subject, &mem_info);
    sysmon_perf.timer = lv_timer_create(mem_update_timer_cb, SYSMON_REFR_PERIOD_DEF, &mem_info);
#endif

    lv_async_call(sysmon_backend_init_async_cb, NULL);
}

void _lv_sysmon_builtin_deinit(void)
{
    lv_async_call_cancel(sysmon_backend_init_async_cb, NULL);
#if LV_USE_PERF_MONITOR
    //    lv_subject_deinit(&sysmon_perf->subject);
    lv_timer_delete(sysmon_perf.timer);
#endif
}


lv_obj_t * lv_sysmon_create(lv_obj_t * parent)
{
    LV_LOG_INFO("begin");
    lv_obj_t * label = lv_label_create(parent);
    lv_obj_set_style_bg_opa(label, LV_OPA_50, 0);
    lv_obj_set_style_bg_color(label, lv_color_black(), 0);
    lv_obj_set_style_text_color(label, lv_color_white(), 0);
    lv_obj_set_style_pad_all(label, 3, 0);
    lv_label_set_text(label, "?");
    return label;
}



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


#if LV_USE_PERF_MONITOR

static void perf_monitor_disp_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_sysmon_perf_info_t * info = (lv_sysmon_perf_info_t *)lv_subject_get_pointer(&sysmon_perf.subject);

    switch(code) {
        case LV_EVENT_REFR_START:
            info->measured.refr_interval_sum += lv_tick_elaps(info->measured.refr_start);
            info->measured.refr_start = lv_tick_get();
            break;
        case LV_EVENT_REFR_FINISH:
            info->measured.refr_elaps_sum += lv_tick_elaps(info->measured.refr_start);
            info->measured.refr_cnt++;
            break;
        case LV_EVENT_RENDER_START:
            info->measured.render_start = lv_tick_get();
            break;
        case LV_EVENT_RENDER_READY:
            info->measured.render_elaps_sum += lv_tick_elaps(info->measured.render_start);
            info->measured.render_cnt++;
            break;
        case LV_EVENT_FLUSH_START:
            info->measured.flush_start = lv_tick_get();
            break;
        case LV_EVENT_FLUSH_FINISH:
            info->measured.flush_elaps_sum += lv_tick_elaps(info->measured.flush_start);
            info->measured.flush_cnt++;
            break;
        default:
            break;
    }
}

static void perf_update_timer_cb(lv_timer_t * t)
{
    lv_sysmon_perf_info_t * info = lv_timer_get_user_data(t);

    info->calculated.fps = info->measured.refr_interval_sum ? (1000 * info->measured.refr_cnt /
                                                               info->measured.refr_interval_sum) : 0;
    info->calculated.cpu = 100 - lv_timer_get_idle();
    info->calculated.refr_avg_time = info->measured.refr_cnt ? (info->measured.refr_elaps_sum / info->measured.refr_cnt) :
                                     0;
    info->calculated.render_avg_time = info->measured.render_cnt ? (info->measured.render_elaps_sum /
                                                                    info->measured.render_cnt) : 0;
    info->calculated.flush_avg_time = info->measured.flush_cnt ? (info->measured.flush_elaps_sum / info->measured.flush_cnt)
                                      : 0;
    info->calculated.render_real_avg_time = info->calculated.render_avg_time - info->calculated.flush_avg_time;

    lv_subject_set_pointer(&sysmon_perf.subject, info);

    uint32_t refr_start = info->measured.refr_start;
    lv_memzero(info, sizeof(lv_sysmon_perf_info_t));
    info->measured.refr_start = refr_start;

}

static void perf_observer_cb(lv_subject_t * subject, lv_observer_t * observer)
{
    lv_obj_t * label = lv_observer_get_target(observer);
    const lv_sysmon_perf_info_t * perf = lv_subject_get_pointer(subject);

#if LV_USE_PERF_MONITOR_LOG_MODE

    LV_LOG("sysmon: "
           "%" LV_PRIu32 " FPS (refr_cnt: %" LV_PRIu32 " | redraw_cnt: %" LV_PRIu32 " | flush_cnt: %" LV_PRIu32 "), "
           "refr %" LV_PRIu32 "ms (render %" LV_PRIu32 "ms | flush %" LV_PRIu32 "ms), "
           "CPU %" LV_PRIu32 "%%\n",
           perf->calculated.fps, perf->measured.refr_cnt, perf->measured.render_cnt, perf->measured.flush_cnt,
           perf->calculated.refr_avg_time, perf->calculated.render_real_avg_time, perf->calculated.flush_avg_time,
           perf->calculated.cpu);
#else
    lv_label_set_text_fmt(
        label,
        "%" LV_PRIu32" FPS, %" LV_PRIu32 "%% CPU\n"
        "%" LV_PRIu32" ms (%" LV_PRIu32" | %" LV_PRIu32")",
        perf->calculated.fps, perf->calculated.cpu,
        perf->calculated.refr_avg_time, perf->calculated.render_real_avg_time, perf->calculated.flush_avg_time
    );
#endif /*LV_USE_PERF_MONITOR_LOG_MODE*/
}

#endif

#if LV_USE_MEM_MONITOR

static void mem_update_timer_cb(lv_timer_t * t)
{
    lv_mem_monitor_t * mem_mon = lv_timer_get_user_data(t);
    lv_mem_monitor(mem_mon);
    lv_subject_set_pointer(&sysmon_mem.subject, mem_mon);
}

static void mem_observer_cb(lv_subject_t * subject, lv_observer_t * observer)
{
    lv_obj_t * label = lv_observer_get_target(observer);
    const lv_mem_monitor_t * mon = lv_subject_get_pointer(subject);

    uint32_t used_size = mon->total_size - mon->free_size;;
    uint32_t used_kb = used_size / 1024;
    uint32_t used_kb_tenth = (used_size - (used_kb * 1024)) / 102;
    lv_label_set_text_fmt(label,
                          "%"LV_PRIu32 ".%"LV_PRIu32 " kB, %d%%\n"
                          "%d%% frag.",
                          used_kb, used_kb_tenth, mon->used_pct,
                          mon->frag_pct);
}

#endif

static void sysmon_backend_init_async_cb(void * user_data)
{
    LV_UNUSED(user_data);

#if LV_USE_PERF_MONITOR
    lv_display_add_event(lv_display_get_default(), perf_monitor_disp_event_cb, LV_EVENT_ALL, NULL);

#if !LV_USE_PERF_MONITOR_LOG_MODE
    lv_obj_t * obj1 = lv_sysmon_create(lv_layer_sys());
    lv_obj_align(obj1, LV_USE_PERF_MONITOR_POS, 0, 0);
    lv_subject_add_observer_obj(&sysmon_perf.subject, perf_observer_cb, obj1, NULL);
#endif

#endif

#if LV_USE_MEM_MONITOR
    lv_obj_t * obj2 = lv_sysmon_create(lv_layer_sys());
    lv_obj_align(obj2, LV_USE_MEM_MONITOR_POS, 0, 0);
    lv_subject_add_observer_obj(&sysmon_mem.subject, mem_observer_cb, obj2, NULL);
#endif
}


#endif /*LV_USE_SYSMON*/
