// This file was generated by SquareLine Studio
// SquareLine Studio version: SquareLine Studio 1.5.3
// LVGL version: 9.2.2
// Project name: PowerPico

#include "../ui.h"

lv_obj_t * ui_SetPage = NULL;
// backlight
static lv_obj_t * ui_PanelBL = NULL;
static lv_obj_t * ui_SliderBL = NULL;
static lv_obj_t * ui_LabelBL = NULL;
// key sound
static lv_obj_t * ui_PanelKS = NULL;
static lv_obj_t * ui_SwitchKS = NULL;
static lv_obj_t * ui_LabelKS = NULL;
// language
static lv_obj_t * ui_PanelLang = NULL;
static lv_obj_t * ui_SwitchLang = NULL;
static lv_obj_t * ui_LabelLang = NULL;
// chose rotation
static lv_obj_t * ui_PanelRotate = NULL;
static lv_obj_t * ui_LabelRotation = NULL;
static lv_obj_t * ui_LabelRotNum = NULL;
// about
static lv_obj_t * ui_PanelAbout = NULL;
static lv_obj_t * ui_LabelAbout = NULL;

static lv_obj_t * panels[5]; // 存储所有 panel 的指针
static int current_panel_index = 0; // 当前选中的 panel 索引

// event funtions

#include "key.h"
void ui_set_page_key_handler(uint8_t key_id)
{
    int panel_count = sizeof(panels) / sizeof(panels[0]);
    if(key_id == KEYB_NUM) { // key boot
        lv_lib_pm_next();
    } if (key_id == KEYL_NUM) { // key left
        lv_obj_clear_state(panels[current_panel_index], LV_STATE_CHECKED);
        current_panel_index = (current_panel_index - 1 + panel_count) % panel_count;
        lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED);
        lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON);
    } else if (key_id == KEYR_NUM) { // key right
        lv_obj_clear_state(panels[current_panel_index], LV_STATE_CHECKED);
        current_panel_index = (current_panel_index + 1) % panel_count;
        lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED);
        lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON);
    } else { // key yes or key neg
        switch(current_panel_index) {
            // Screen Brightness
            case 0:
                if (key_id == KEYY_NUM) {
                    int16_t slider_value = lv_slider_get_value(ui_SliderBL);
                    slider_value += 10;
                    if(slider_value > 100) slider_value = 100;
                    lv_slider_set_value(ui_SliderBL, slider_value, LV_ANIM_ON);
                    ui_set_back_light_level(slider_value);
                } else if (key_id == KEYN_NUM) {
                    int16_t slider_value = lv_slider_get_value(ui_SliderBL);
                    slider_value -= 10;
                    if(slider_value < 10) slider_value = 10;
                    lv_slider_set_value(ui_SliderBL, slider_value, LV_ANIM_ON);
                    ui_set_back_light_level(slider_value);
                }
                break;
            // key sound
            case 1:
                if (key_id == KEYY_NUM) {
                    lv_obj_add_state(ui_SwitchKS, LV_STATE_CHECKED);
                    ui_set_key_sound_enable(1);
                } else if(key_id == KEYN_NUM) {
                    lv_obj_clear_state(ui_SwitchKS, LV_STATE_CHECKED);
                    ui_set_key_sound_enable(0);
                }
                break;

            // language
            case 2:
                // do nothing
                break;

            // chose rotation
            case 3:
                uint16_t rotation = ui_get_display_rotation();
                if (key_id == KEYY_NUM) {
                    rotation = (rotation + 360 + 90) % 360;
                } else if(key_id == KEYN_NUM) {
                    rotation = (rotation + 360 - 90) % 360;
                }
                if (rotation == 0) {
                    lv_label_set_text(ui_LabelRotNum, "<  0  >");
                } else if (rotation == 90) {
                    lv_label_set_text(ui_LabelRotNum, "< 90 >");
                } else if (rotation == 180) {
                    lv_label_set_text(ui_LabelRotNum, "< 180 >");
                } else if (rotation == 270) {
                    lv_label_set_text(ui_LabelRotNum, "< 270 >");
                }
                ui_set_display_rotation(rotation);
                ui_full_screen_refresh(ui_SetPage);
                break;

            // about
            case 4:
                // do nothing
                break;

        }
        // save settings to eeprom
        ui_system_settings_save();
    }
}

static void _setting_init(void) {

    // backlight
    lv_slider_set_value(ui_SliderBL, ui_get_back_light_level(), LV_ANIM_OFF);

    // key sound
    if(ui_get_key_sound_enable())
        lv_obj_add_state(ui_SwitchKS, LV_STATE_CHECKED);
    else
        lv_obj_clear_state(ui_SwitchKS, LV_STATE_CHECKED);

    // lcd rotation
    uint16_t rotation = ui_get_display_rotation();
    if (rotation == 0) {
        lv_label_set_text(ui_LabelRotNum, "<  0  >");
    } else if (rotation == 90) {
        lv_label_set_text(ui_LabelRotNum, "< 90 >");
    } else if (rotation == 180) {
        lv_label_set_text(ui_LabelRotNum, "< 180 >");
    } else if (rotation == 270) {
        lv_label_set_text(ui_LabelRotNum, "< 270 >");
    }
}

// build funtions

void ui_SetPage_screen_init(void)
{
    ui_SetPage = lv_obj_create(NULL);

    ui_PanelBL = lv_obj_create(ui_SetPage);
    lv_obj_set_width(ui_PanelBL, 234);
    lv_obj_set_height(ui_PanelBL, 65);
    lv_obj_set_x(ui_PanelBL, 0);
    lv_obj_set_y(ui_PanelBL, 5);
    lv_obj_set_align(ui_PanelBL, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_PanelBL, LV_OBJ_FLAG_CHECKABLE);     /// Flags
    lv_obj_remove_flag(ui_PanelBL, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_PanelBL, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(ui_PanelBL, 255, LV_PART_MAIN | LV_STATE_CHECKED);

    ui_SliderBL = lv_slider_create(ui_PanelBL);
    lv_slider_set_value(ui_SliderBL, 50, LV_ANIM_OFF);
    if(lv_slider_get_mode(ui_SliderBL) == LV_SLIDER_MODE_RANGE) lv_slider_set_left_value(ui_SliderBL, 0, LV_ANIM_OFF);
    lv_obj_set_width(ui_SliderBL, 200);
    lv_obj_set_height(ui_SliderBL, 10);
    lv_obj_set_align(ui_SliderBL, LV_ALIGN_BOTTOM_MID);

    //Compensating for LVGL9.1 draw crash with bar/slider max value when top-padding is nonzero and right-padding is 0
    if(lv_obj_get_style_pad_top(ui_SliderBL, LV_PART_MAIN) > 0) lv_obj_set_style_pad_right(ui_SliderBL,
                                                                                               lv_obj_get_style_pad_right(ui_SliderBL, LV_PART_MAIN) + 1, LV_PART_MAIN);
    ui_LabelBL = lv_label_create(ui_PanelBL);
    lv_obj_set_width(ui_LabelBL, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelBL, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_LabelBL, 0);
    lv_obj_set_y(ui_LabelBL, -5);
    lv_label_set_text(ui_LabelBL, "Screen Brightness :");
    lv_obj_set_style_text_font(ui_LabelBL, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_PanelKS = lv_obj_create(ui_SetPage);
    lv_obj_set_width(ui_PanelKS, 234);
    lv_obj_set_height(ui_PanelKS, 45);
    lv_obj_set_x(ui_PanelKS, 0);
    lv_obj_set_y(ui_PanelKS, 75);
    lv_obj_set_align(ui_PanelKS, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_PanelKS, LV_OBJ_FLAG_CHECKABLE);     /// Flags
    lv_obj_remove_flag(ui_PanelKS, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_PanelKS, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(ui_PanelKS, 255, LV_PART_MAIN | LV_STATE_CHECKED);

    ui_SwitchKS = lv_switch_create(ui_PanelKS);
    lv_obj_set_width(ui_SwitchKS, 50);
    lv_obj_set_height(ui_SwitchKS, 25);
    lv_obj_set_align(ui_SwitchKS, LV_ALIGN_RIGHT_MID);
    lv_obj_add_state(ui_SwitchKS, LV_STATE_CHECKED);       /// States

    ui_LabelKS = lv_label_create(ui_PanelKS);
    lv_obj_set_width(ui_LabelKS, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelKS, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_LabelKS, LV_ALIGN_LEFT_MID);
    lv_label_set_text(ui_LabelKS, "Enable key sound");
    lv_obj_set_style_text_font(ui_LabelKS, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_PanelLang = lv_obj_create(ui_SetPage);
    lv_obj_set_width(ui_PanelLang, 234);
    lv_obj_set_height(ui_PanelLang, 45);
    lv_obj_set_x(ui_PanelLang, 0);
    lv_obj_set_y(ui_PanelLang, 125);
    lv_obj_set_align(ui_PanelLang, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_PanelLang, LV_OBJ_FLAG_CHECKABLE);     /// Flags
    lv_obj_remove_flag(ui_PanelLang, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_PanelLang, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(ui_PanelLang, 255, LV_PART_MAIN | LV_STATE_CHECKED);

    ui_SwitchLang = lv_switch_create(ui_PanelLang);
    lv_obj_set_width(ui_SwitchLang, 50);
    lv_obj_set_height(ui_SwitchLang, 25);
    lv_obj_set_align(ui_SwitchLang, LV_ALIGN_RIGHT_MID);

    ui_LabelLang = lv_label_create(ui_PanelLang);
    lv_obj_set_width(ui_LabelLang, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelLang, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_LabelLang, LV_ALIGN_LEFT_MID);
    lv_label_set_text(ui_LabelLang, "Enable Chinese");
    lv_obj_set_style_text_font(ui_LabelLang, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_PanelRotate = lv_obj_create(ui_SetPage);
    lv_obj_set_width(ui_PanelRotate, 234);
    lv_obj_set_height(ui_PanelRotate, 45);
    lv_obj_set_x(ui_PanelRotate, 0);
    lv_obj_set_y(ui_PanelRotate, 175);
    lv_obj_set_align(ui_PanelRotate, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_PanelRotate, LV_OBJ_FLAG_CHECKABLE);     /// Flags
    lv_obj_remove_flag(ui_PanelRotate, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_PanelRotate, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(ui_PanelRotate, 255, LV_PART_MAIN | LV_STATE_CHECKED);

    ui_LabelRotation = lv_label_create(ui_PanelRotate);
    lv_obj_set_width(ui_LabelRotation, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelRotation, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_LabelRotation, LV_ALIGN_LEFT_MID);
    lv_label_set_text(ui_LabelRotation, "Chose Rotation");
    lv_obj_set_style_text_font(ui_LabelRotation, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_LabelRotNum = lv_label_create(ui_PanelRotate);
    lv_obj_set_width(ui_LabelRotNum, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelRotNum, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_align(ui_LabelRotNum, LV_ALIGN_RIGHT_MID);
    lv_label_set_text(ui_LabelRotNum, "< 180 >");
    lv_obj_set_style_text_font(ui_LabelRotNum, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    ui_PanelAbout = lv_obj_create(ui_SetPage);
    lv_obj_set_width(ui_PanelAbout, 234);
    lv_obj_set_height(ui_PanelAbout, 100);
    lv_obj_set_x(ui_PanelAbout, 0);
    lv_obj_set_y(ui_PanelAbout, 225);
    lv_obj_set_align(ui_PanelAbout, LV_ALIGN_TOP_MID);
    lv_obj_add_flag(ui_PanelAbout, LV_OBJ_FLAG_CHECKABLE);     /// Flags
    lv_obj_remove_flag(ui_PanelAbout, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
    lv_obj_set_style_bg_color(ui_PanelAbout, lv_color_hex(0x606060), LV_PART_MAIN | LV_STATE_CHECKED);
    lv_obj_set_style_bg_opa(ui_PanelAbout, 255, LV_PART_MAIN | LV_STATE_CHECKED);

    ui_LabelAbout = lv_label_create(ui_PanelAbout);
    lv_obj_set_width(ui_LabelAbout, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_LabelAbout, LV_SIZE_CONTENT);    /// 1
    lv_label_set_text(ui_LabelAbout, "About\nPower-Pico\nA uA current meter\nV 1.0.0");
    lv_obj_set_style_text_font(ui_LabelAbout, &lv_font_montserrat_16, LV_PART_MAIN | LV_STATE_DEFAULT);

    // init setting values
    _setting_init();

    // store panels in array
    panels[0] = ui_PanelBL;
    panels[1] = ui_PanelKS;
    panels[2] = ui_PanelLang;
    panels[3] = ui_PanelRotate;
    panels[4] = ui_PanelAbout;
    lv_obj_add_state(panels[current_panel_index], LV_STATE_CHECKED);
    lv_obj_scroll_to_view(panels[current_panel_index], LV_ANIM_ON);

}

void ui_SetPage_screen_destroy(void)
{

}
