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

#include "../ui.h"

#include "lang_config.h"
#include "src/core/lv_obj_style_gen.h"

#include "tal_api.h"
#include <string.h>

#define UI_CHAT_MSG_MAX_COUNT (20)

lv_obj_t *ui_home                    = NULL;
lv_obj_t *ui_home_status_bar         = NULL;
lv_obj_t *ui_home_emoji              = NULL;
lv_obj_t *ui_home_status_label       = NULL;
lv_obj_t *ui_home_icon               = NULL;
lv_obj_t *ui_home_chat               = NULL;
lv_obj_t *ui_home_systemmsgcontainer = NULL;
lv_obj_t *ui_home_usermsgcontainer   = NULL;
lv_obj_t *ui_home_aimsgcontainer     = NULL;

static void scroll_to_bottom_task(lv_timer_t *timer);

// 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_SCREEN_LOADED) {
        home_screen_loaded(e);

        // ui chat container scroll to bottom
        lv_timer_create(scroll_to_bottom_task, 100, ui_home_chat);
    }
    if (event_code == LV_EVENT_SCREEN_UNLOADED) {
        home_screen_unloaded(e);
    }
}

void ui_event_home_status_bar(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);

    if (event_code == LV_EVENT_CLICKED) {
        _ui_screen_change(&ui_setting, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_setting_screen_init);
    }
}

// Create a delayed task to scroll to the bottom in the next frame
static void scroll_to_bottom_task(lv_timer_t *timer)
{
    lv_obj_t *parent = (lv_obj_t *)timer->user_data;

    // Print ui_home_chat container info
    lv_area_t parent_coords;
    lv_obj_get_coords(parent, &parent_coords);
    int32_t scroll_y      = lv_obj_get_scroll_y(parent);
    int32_t scroll_bottom = lv_obj_get_scroll_bottom(parent);

    // Print each child info
    int32_t child_count = lv_obj_get_child_count(parent);

    for (int i = 0; i < child_count; i++) {
        lv_obj_t *child = lv_obj_get_child(parent, i);
        lv_area_t child_coords;
        lv_obj_get_coords(child, &child_coords);
    }

    // Calculate if last child is fully visible
    // extra_padding accounts for: shadow (12px) + border (3px)
    int32_t target_scroll_y = 0;

    if (child_count > 0) {
        lv_obj_t *last_child = lv_obj_get_child(parent, -1);
        lv_area_t last_coords;
        lv_obj_get_coords(last_child, &last_coords);

        // Calculate required scroll position to show last child fully
        // Note: Increasing scroll_y moves content UP, making bottom items visible
        target_scroll_y = scroll_y + (last_coords.y2 - parent_coords.y2);

        // Clamp to maximum scrollable range
        int32_t max_scroll = scroll_y + scroll_bottom;
        if (target_scroll_y > max_scroll) {
            target_scroll_y = max_scroll;
        }
    }

    // Scroll to calculated position to show last child fully
    if (target_scroll_y > 0) {
        lv_obj_scroll_to_y(parent, target_scroll_y, LV_ANIM_ON);
    }

    lv_timer_del(timer); // Delete one-shot timer
}

static void __ui_home_chat_child_created(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if (event_code == LV_EVENT_CHILD_CREATED) {
        // Get the target object of the event (the parent object, which should be ui_home_chat)
        lv_obj_t *parent = lv_event_get_target_obj(e);
        // Get the newly created child object (passed via event parameter)
        lv_obj_t *new_child = (lv_obj_t *)lv_event_get_param(e);

        // Only handle direct children of ui_home_chat (not grandchildren)
        if (new_child && lv_obj_get_parent(new_child) == parent && parent == ui_home_chat) {
            int child_count = lv_obj_get_child_count(parent);

            if (child_count > UI_CHAT_MSG_MAX_COUNT) {
                lv_obj_del(lv_obj_get_child(parent, 0));
            }

            // Scroll to bottom
            lv_timer_create(scroll_to_bottom_task, 100, parent);
        }
    }
}
// build funtions

void ui_home_screen_init(void)
{
    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_START, 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);

    ui_home_status_bar = lv_obj_create(ui_home);
    lv_obj_remove_style_all(ui_home_status_bar);
    lv_obj_set_width(ui_home_status_bar, lv_pct(100));
    lv_obj_set_height(ui_home_status_bar, LV_SIZE_CONTENT); /// 1
    lv_obj_set_align(ui_home_status_bar, LV_ALIGN_CENTER);
    lv_obj_set_flex_flow(ui_home_status_bar, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(ui_home_status_bar, LV_FLEX_ALIGN_SPACE_AROUND, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
    lv_obj_remove_flag(ui_home_status_bar, LV_OBJ_FLAG_SCROLLABLE); /// Flags
    ui_object_set_themeable_style_property(ui_home_status_bar, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_COLOR,
                                           _ui_theme_color_statusBarBG);
    ui_object_set_themeable_style_property(ui_home_status_bar, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_OPA,
                                           _ui_theme_alpha_statusBarBG);
    lv_obj_set_style_pad_left(ui_home_status_bar, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_home_status_bar, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_top(ui_home_status_bar, 3, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_home_status_bar, 3, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_home_emoji = lv_image_create(ui_home_status_bar);
    lv_image_set_src(ui_home_emoji, &ui_img_image_emoji_twemoji32_neutral_32_png);
    lv_obj_set_width(ui_home_emoji, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height(ui_home_emoji, LV_SIZE_CONTENT); /// 1
    lv_obj_set_align(ui_home_emoji, LV_ALIGN_CENTER);
    lv_obj_add_flag(ui_home_emoji, LV_OBJ_FLAG_CLICKABLE);     /// Flags
    lv_obj_remove_flag(ui_home_emoji, LV_OBJ_FLAG_SCROLLABLE); /// Flags

    ui_home_status_label = lv_label_create(ui_home_status_bar);
    lv_obj_set_width(ui_home_status_label, lv_pct(60));
    lv_obj_set_align(ui_home_status_label, LV_ALIGN_CENTER);
    lv_label_set_long_mode(ui_home_status_label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_set_style_pad_top(ui_home_status_label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_bottom(ui_home_status_label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_left(ui_home_status_label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_right(ui_home_status_label, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    // Set font and text properties BEFORE setting text to ensure proper glyph caching
    lv_obj_set_style_text_font(ui_home_status_label, &ui_font_puhui_18_2, LV_PART_MAIN | LV_STATE_DEFAULT);
    ui_object_set_themeable_style_property(ui_home_status_label, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_TEXT_COLOR,
                                           _ui_theme_color_PrimaryText);
    ui_object_set_themeable_style_property(ui_home_status_label, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_TEXT_OPA,
                                           _ui_theme_alpha_PrimaryText);
    lv_obj_set_style_text_align(ui_home_status_label, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_label_set_text(ui_home_status_label, INITIALIZING);

    ui_home_icon = lv_image_create(ui_home_status_bar);
    lv_image_set_src(ui_home_icon, &ui_img_image_icon_wifi_wifi_connect_30_png);
    lv_obj_set_width(ui_home_icon, LV_SIZE_CONTENT);  /// 1
    lv_obj_set_height(ui_home_icon, LV_SIZE_CONTENT); /// 1
    lv_obj_set_align(ui_home_icon, LV_ALIGN_CENTER);
    lv_obj_add_flag(ui_home_icon, LV_OBJ_FLAG_CLICKABLE);     /// Flags
    lv_obj_remove_flag(ui_home_icon, LV_OBJ_FLAG_SCROLLABLE); /// Flags

    ui_home_chat = lv_obj_create(ui_home);
    lv_obj_remove_style_all(ui_home_chat);
    lv_obj_set_width(ui_home_chat, lv_pct(98));

    // Use Flex grow to automatically fill remaining space
    lv_obj_set_flex_grow(ui_home_chat, 1);

    lv_obj_set_align(ui_home_chat, LV_ALIGN_TOP_MID);
    lv_obj_set_flex_flow(ui_home_chat, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(ui_home_chat, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    ui_object_set_themeable_style_property(ui_home_chat, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_COLOR,
                                           _ui_theme_color_background);
    ui_object_set_themeable_style_property(ui_home_chat, LV_PART_MAIN | LV_STATE_DEFAULT, LV_STYLE_BG_OPA,
                                           _ui_theme_alpha_background);

    lv_obj_add_event_cb(ui_home_chat, __ui_home_chat_child_created, LV_EVENT_CHILD_CREATED, NULL);

    lv_obj_add_event_cb(ui_home_status_bar, ui_event_home_status_bar, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ui_home, ui_event_home, LV_EVENT_ALL, NULL);
}

void ui_home_screen_destroy(void)
{
    if (ui_home)
        lv_obj_del(ui_home);

    // NULL screen variables
    ui_home                    = NULL;
    ui_home_status_bar         = NULL;
    ui_home_emoji              = NULL;
    ui_home_status_label       = NULL;
    ui_home_icon               = NULL;
    ui_home_chat               = NULL;
    ui_home_systemmsgcontainer = NULL;
    ui_home_usermsgcontainer   = NULL;
    ui_home_aimsgcontainer     = NULL;
}

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

static UI_HOME_EMOTION_T sg_home_emotion_list[] = {
    {
        .emotion_str       = "NEUTRAL",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_neutral_32_png,
    },
    {
        .emotion_str       = "HAPPY",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_happy_32_png,
    },
    {
        .emotion_str       = "SAD",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_sad_32_png,
    },
    {
        .emotion_str       = "ANGRY",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_angry_32_png,
    },
    {
        .emotion_str       = "SURPRISE",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_surprised_32_png,
    },
    {
        .emotion_str       = "CONFUSED",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_confused_32_png,
    },
    {
        .emotion_str       = "THINKING",
        .emotion_image_src = &ui_img_image_emoji_twemoji32_thinking_32_png,
    },
};

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

    int img_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) {
            img_index = i;
            break;
        }
    }

    lv_image_set_src(ui_home_emoji, sg_home_emotion_list[img_index].emotion_image_src);

    return;
}

void ui_setting_wifi_update(uint8_t connected)
{
    lv_obj_t *img_obj = NULL;

    lv_obj_t *current_screen = lv_scr_act();
    if (current_screen == ui_home) {
        img_obj = ui_home_icon;
    } else if (current_screen == ui_setting) {
        img_obj = ui_setting_icon;
    }

    if (NULL == img_obj) {
        return;
    }

    if (connected) {
        lv_image_set_src(img_obj, &ui_img_image_icon_wifi_wifi_connect_30_png);
    } else {
        lv_image_set_src(img_obj, &ui_img_image_icon_wifi_wifi_disconnect_30_png);
    }
}

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

    if (strcmp(status, LISTENING) == 0) {
        lv_label_set_text(ui_home_status_label, LISTENING);
    } else if (strcmp(status, SPEAKING) == 0) {
        lv_label_set_text(ui_home_status_label, SPEAKING);
    } else {
        lv_label_set_text(ui_home_status_label, STANDBY);
    }

    return;
}

void ui_set_user_msg(const char *msg)
{
    if (msg == NULL || ui_home_chat == NULL) {
        return;
    }

    lv_obj_t *user_msg_container = NULL;

    user_msg_container = ui_usermsgcontainer_create(ui_home_chat);
    lv_obj_t *label    = ui_comp_get_child(user_msg_container, UI_COMP_USERMSGCONTAINER_USER_MSG);

    if (label == NULL) {
        return;
    }

    lv_label_set_text(label, msg);
}

void ui_set_assistant_msg(const char *msg)
{
    if (msg == NULL || ui_home_chat == NULL) {
        return;
    }

    lv_obj_t *ai_msg_container = NULL;

    ai_msg_container = ui_aimsgcontainer_create(ui_home_chat);
    lv_obj_t *label  = ui_comp_get_child(ai_msg_container, UI_COMP_AIMSGCONTAINER_AI_MSG);

    if (label == NULL) {
        return;
    }

    lv_label_set_text(label, msg);
}

void ui_set_system_msg(const char *msg)
{
    if (msg == NULL || ui_home_chat == NULL) {
        return;
    }

    lv_obj_t *system_msg_container = NULL;

    system_msg_container = ui_systemmsgcontainer_create(ui_home_chat);
    lv_obj_t *label      = ui_comp_get_child(system_msg_container, UI_COMP_SYSTEMMSGCONTAINER_LABEL);

    if (label == NULL) {
        return;
    }

    lv_label_set_text(label, msg);
}
/***************************** app display functions end *****************************/
