// This file was generated by SquareLine Studio
// SquareLine Studio version: SquareLine Studio 1.5.4
// LVGL version: 9.1.0
// Project name: SquareLine_Project

#include "../ui.h"
#include "../ui_audio_waveform.h"
#include "src/core/lv_obj.h"
#include <stdint.h>
#include <string.h>
#include "lang_config.h"

LV_IMAGE_DECLARE(gif_neutral);
LV_IMAGE_DECLARE(gif_happy);
LV_IMAGE_DECLARE(gif_sad);
LV_IMAGE_DECLARE(gif_angry);
LV_IMAGE_DECLARE(gif_surprised);
LV_IMAGE_DECLARE(gif_confused);
LV_IMAGE_DECLARE(gif_thinking);

lv_obj_t *ui_home                     = NULL;
lv_obj_t *ui_emoji_container          = NULL;
lv_obj_t *ui_emoji_gif                = NULL;
lv_obj_t *ui_chat_status_container    = NULL;
lv_obj_t *ui_chat_status_label        = NULL;
lv_obj_t *ui_audio_waveform_container = NULL;
// event funtions
void ui_event_home(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);

    if (event_code == LV_EVENT_GESTURE && lv_indev_get_gesture_dir(lv_indev_active()) == LV_DIR_BOTTOM) {
        lv_indev_wait_release(lv_indev_active());
        _ui_screen_change(&ui_setting, LV_SCR_LOAD_ANIM_MOVE_BOTTOM, 300, 0, &ui_setting_screen_init);
    }
    if (event_code == LV_EVENT_SCREEN_LOADED) {
        home_screen_loaded(e);
    }
    if (event_code == LV_EVENT_SCREEN_UNLOADED) {
        home_screen_unloaded(e);
    }
}

// build funtions

void ui_home_screen_init(void)
{
#if 0
    ui_home = lv_obj_create(NULL);
    lv_obj_remove_flag(ui_home, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    lv_obj_set_flex_flow(ui_home, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(ui_home, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    ui_object_set_themeable_style_property(ui_home, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_COLOR,
                                           _ui_theme_color_background);
    ui_object_set_themeable_style_property(ui_home, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_OPA,
                                           _ui_theme_alpha_background);
    // lv_obj_set_style_radius(ui_home, LV_RADIUS_CIRCLE,
    //                         LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_emoji_container = lv_obj_create(ui_home);
    lv_obj_remove_style_all(ui_emoji_container);
    lv_obj_set_width(ui_emoji_container, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height(ui_emoji_container, LV_SIZE_CONTENT); /// 1
    lv_obj_set_align(ui_emoji_container, LV_ALIGN_CENTER);
    lv_obj_remove_flag(ui_emoji_container,
                       LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE); /// Flags

    // Add gif to ui_emoji_container
    ui_emoji_gif = lv_gif_create(ui_emoji_container);
    lv_gif_set_src(ui_emoji_gif, &gif_neutral);
    lv_obj_align(ui_emoji_gif, LV_ALIGN_CENTER, 0, 0);

    ui_chat_status_container = lv_obj_create(ui_home);
    lv_obj_remove_style_all(ui_chat_status_container);
    lv_obj_set_width(ui_chat_status_container, lv_pct(100));
    lv_obj_set_height(ui_chat_status_container, lv_pct(30));
    lv_obj_set_align(ui_chat_status_container, LV_ALIGN_CENTER);
    lv_obj_set_flex_flow(ui_chat_status_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(ui_chat_status_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER,
                          LV_FLEX_ALIGN_CENTER);
    lv_obj_remove_flag(ui_chat_status_container,
                       LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE); /// Flags
    lv_obj_add_flag(ui_chat_status_container, LV_OBJ_FLAG_HIDDEN);      /// Flags

    ui_chat_status_label = lv_label_create(ui_chat_status_container);
    lv_obj_set_width(ui_chat_status_label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height(ui_chat_status_label, LV_SIZE_CONTENT); /// 1
    lv_obj_set_align(ui_chat_status_label, LV_ALIGN_CENTER);
    lv_label_set_text(ui_chat_status_label, "Listening");
    lv_obj_set_style_text_color(ui_chat_status_label, lv_color_hex(0x1C90FC), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_chat_status_label, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_chat_status_label, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    // Initialize audio waveform display
    ui_audio_waveform_init(ui_chat_status_container);
    ui_audio_waveform_container = ui_audio_waveform_get_container();

    lv_obj_add_event_cb(ui_home, ui_event_home, LV_EVENT_ALL, NULL);
#else
    ui_home = lv_obj_create(NULL);
    lv_obj_remove_flag(ui_home, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    ui_object_set_themeable_style_property(ui_home, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_COLOR,
                                           _ui_theme_color_background);
    ui_object_set_themeable_style_property(ui_home, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_OPA,
                                           _ui_theme_alpha_background);

    // Add gif
    ui_emoji_gif = lv_gif_create(ui_home);
    lv_gif_set_src(ui_emoji_gif, &gif_neutral);
    lv_obj_align(ui_emoji_gif, LV_ALIGN_CENTER, 0, 0);

    ui_chat_status_label = lv_label_create(ui_home);
    lv_obj_set_width(ui_chat_status_label, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height(ui_chat_status_label, LV_SIZE_CONTENT); /// 1
    lv_label_set_text(ui_chat_status_label, LISTENING);
    lv_obj_align_to(ui_chat_status_label, ui_emoji_gif, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_style_text_color(ui_chat_status_label, lv_color_hex(0x1C90FC), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_chat_status_label, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_chat_status_label, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_audio_waveform_init(ui_home);
    ui_audio_waveform_container = ui_audio_waveform_get_container();
    lv_obj_align_to(ui_audio_waveform_container, ui_chat_status_label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    lv_obj_add_flag(ui_chat_status_label, LV_OBJ_FLAG_HIDDEN);        /// Flags
    lv_obj_add_flag(ui_audio_waveform_container, LV_OBJ_FLAG_HIDDEN); /// Flags

    lv_obj_add_event_cb(ui_home, ui_event_home, LV_EVENT_ALL, NULL);
#endif
}

void ui_home_screen_destroy(void)
{
    // Destroy audio waveform first
    ui_audio_waveform_destroy();

    if (ui_home)
        lv_obj_del(ui_home);

    // NULL screen variables
    ui_home                     = NULL;
    ui_emoji_container          = NULL;
    ui_emoji_gif                = NULL;
    ui_chat_status_container    = NULL;
    ui_chat_status_label        = NULL;
    ui_audio_waveform_container = NULL;
}

void ui_home_screen_gif_enable(uint8_t enable)
{
    if (NULL == ui_emoji_gif) {
        return;
    }

    // start or stop gif animation
    if (enable) {
        LV_LOG_USER("ui_home_screen_gif_enable: start gif animation");
        lv_gif_resume(ui_emoji_gif);
    } else {
        LV_LOG_USER("ui_home_screen_gif_enable: stop gif animation");
        lv_gif_pause(ui_emoji_gif);
    }

    return;
}

void ui_home_screen_gif_update(const void *src)
{
    if (NULL == src || NULL == ui_emoji_gif) {
        return;
    }

    lv_gif_set_src(ui_emoji_gif, src);
    lv_gif_restart(ui_emoji_gif);
}

void ui_home_screen_waveform_enable(uint8_t enable)
{
    LV_LOG_USER("ui_home_screen_waveform_show: %d", enable);

    if (enable) {
        // lv_obj_clear_flag(ui_chat_status_container, LV_OBJ_FLAG_HIDDEN);
        if (NULL != ui_chat_status_label) {
            lv_obj_clear_flag(ui_chat_status_label, LV_OBJ_FLAG_HIDDEN); /// Flags
        }
        if (NULL != ui_audio_waveform_container) {
            lv_obj_clear_flag(ui_audio_waveform_container, LV_OBJ_FLAG_HIDDEN); /// Flags
        }
    } else {
        // lv_obj_add_flag(ui_chat_status_container, LV_OBJ_FLAG_HIDDEN);
        if (NULL != ui_chat_status_label) {
            lv_obj_add_flag(ui_chat_status_label, LV_OBJ_FLAG_HIDDEN); /// Flags
        }
        if (NULL != ui_audio_waveform_container) {
            lv_obj_add_flag(ui_audio_waveform_container, LV_OBJ_FLAG_HIDDEN); /// Flags
        }
    }

    lv_obj_invalidate(ui_home);

    return;
}

void ui_home_screen_waveform_start(float (*get_power_callback)(void))
{
    // Start the waveform animation with callback
    ui_audio_waveform_start(get_power_callback);
}

void ui_home_screen_waveform_stop(void)
{
    // Stop the waveform animation
    ui_audio_waveform_stop();
}

// app display functions
typedef struct {
    char       *emotion_str;
    const void *emotion_gif;
} UI_HOME_EMOTION_T;

static UI_HOME_EMOTION_T sg_home_emotion_list[] = {
    {
        .emotion_str = "NEUTRAL",
        .emotion_gif = &gif_neutral,
    },
    {
        .emotion_str = "HAPPY",
        .emotion_gif = &gif_happy,
    },
    {
        .emotion_str = "SAD",
        .emotion_gif = &gif_sad,
    },
    {
        .emotion_str = "ANGRY",
        .emotion_gif = &gif_angry,
    },
    {
        .emotion_str = "SURPRISE",
        .emotion_gif = &gif_surprised,
    },
    {
        .emotion_str = "CONFUSED",
        .emotion_gif = &gif_confused,
    },
    {
        .emotion_str = "THINKING",
        .emotion_gif = &gif_confused,
    },
};

void ui_set_emotion(const char *emotion)
{
    if (emotion == NULL) {
        return;
    }

    int gif_index = 0;
    for (int i = 0; i < sizeof(sg_home_emotion_list) / sizeof(sg_home_emotion_list[0]); i++) {
        if (strcmp(emotion, sg_home_emotion_list[i].emotion_str) == 0) {
            gif_index = i;
            break;
        }
    }

    lv_gif_set_src(ui_emoji_gif, sg_home_emotion_list[gif_index].emotion_gif);
}

static float __ui_get_audio_power(void)
{
    extern float app_ui_helper_get_audio_power(void);
    return app_ui_helper_get_audio_power();
}

void ui_set_device_status(const char *status)
{
    if (status == NULL) {
        return;
    }

    if (strcmp(status, LISTENING) == 0) {
        ui_home_screen_waveform_enable(1);
        ui_home_screen_waveform_start(__ui_get_audio_power);
    } else {
        ui_home_screen_waveform_stop();
        ui_home_screen_waveform_enable(0);
    }

    return;
}

void ui_set_user_msg(const char *msg)
{
    return;
}

void ui_set_assistant_msg(const char *msg)
{
    return;
}

void ui_set_system_msg(const char *msg)
{
    return;
}