#include <rtthread.h>
#include <rtdevice.h>
#include "littlevgl2rtt.h"
#include "lvgl.h"
#include "gui_app_fwk.h"

#include "app_clock_main.h"
#include "app_mem.h"
#include "log.h"


/* Image decalration */
LV_IMG_DECLARE(apng_dice);
LV_IMG_DECLARE(clock_bk);

typedef struct
{
    lv_obj_t *apng;
} app_clock_apng_t;


static rt_int16_t g_pngIndex= 0;

static app_clock_apng_t *p_clk_apng = NULL;

static rt_int32_t resume_callback(void)
{
    /* Resume apng animation refresh */
    //lv_ezipa_resume(p_clk_apng->apng);
    return RT_EOK;
}

static rt_int32_t pause_callback(void)
{
    /* Pause apng animation refresh */
    //lv_ezipa_pause(p_clk_apng->apng);
    return RT_EOK;
}



static rt_int32_t init(lv_obj_t *parent)
{
    p_clk_apng = (app_clock_apng_t *)rt_malloc(sizeof(app_clock_apng_t));
    RT_ASSERT(p_clk_apng);

    lv_obj_t *img = lv_img_create(parent);
    RT_ASSERT(img);
    lv_img_set_src(img, &clock_bk);
    //lv_obj_set_pos(img, lv_disp_get_hor_res(NULL) / 2, lv_disp_get_ver_res(NULL) / 2);
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
    lv_img_set_zoom(img, 390*256/480.0f);
    p_clk_apng->apng = img;

    return RT_EOK;
}

static rt_int32_t deinit(void)
{
    if (p_clk_apng->apng) {
        lv_obj_del(p_clk_apng->apng);
        p_clk_apng->apng = NULL;
    }
    
    rt_free(p_clk_apng);
    p_clk_apng = NULL;

    return RT_EOK;
}

static const app_clock_ops_t ops =
{
    .init = init,
    .pause = pause_callback,
    .resume = resume_callback,
    .deinit = deinit,
};

void app_clock_apng_register(void)
{
    app_clock_register("apng", &ops);
}
