#include "esp_lcd_panel_ops.h"
#include "lcd_display_drv.h"
#include "log.h"
#include "rtos_drv.h"
#include "system_freertos_define.h"
#include "ui.h"
#include "events/ui_user_setting_screen_event.h"

#include "rtc_aip8563.h"
#include "parameter.h"
#include "system_control.h"

static const char *TAG = "ui_user_setting_screen_event";

bool SetMonthParam(uint16_t year, uint8_t month, uint8_t param_len);

#define USER_SETTING_YEAR_OFFSET ((uint16_t)2025)
#define USER_SETTING_YEAR_OPTION_OFFSET ((uint16_t)25)

void user_setting_screen_refresh(void);

void UserSettingSreenLoadStartCallback(lv_event_t * e)
{
  user_setting_screen_refresh();
}

void UserSettingSreenLoadedCallback(lv_event_t * e)
{

}

void UserSettingSreenUnloadedCallback(lv_event_t * e)
{
  lv_obj_t* target = lv_event_get_target(e);
  if (target != ui_UserSettingScreen) {
    USER_LOGE(TAG, "ui_UserSettingScreen target error");
    goto End;
  }

  lvgl_obj_delete(&ui_EngineerPasswordDot4Panel);
  lvgl_obj_delete(&ui_EngineerPasswordDot3Panel);
  lvgl_obj_delete(&ui_EngineerPasswordDot2Panel);
  lvgl_obj_delete(&ui_EngineerPasswordDot1Panel);
  lvgl_obj_delete(&ui_EngineerPasswordInputPanelKeyboard);
  lvgl_obj_delete(&ui_EngineerPasswordInputPanelErrorLable);
  lvgl_obj_delete(&ui_EngineerPasswordInputPanelDisArea);
  lvgl_obj_delete(&ui_EngineerPasswordReturnPanel);
  lvgl_obj_delete(&ui_EngineerPasswordInputPanelLable);
  lvgl_obj_delete(&ui_EngineerPasswordInputPanel);
  lvgl_obj_delete(&ui_EngineerPasswordBackPanel);
  lvgl_obj_delete(&ui_TimeSettingParamPanelMinuteLable);
  lvgl_obj_delete(&ui_TimeSettingParamPanelHourLable);
  lvgl_obj_delete(&ui_TimeSettingParamPanelDayLable);
  lvgl_obj_delete(&ui_TimeSettingParamPanelMonthLable);
  lvgl_obj_delete(&ui_TimeSettingParamPanelYearLable);
  lvgl_obj_delete(&ui_TimeSettingParamPanelMinuteRoller);
  lvgl_obj_delete(&ui_TimeSettingParamPanelHourRoller);
  lvgl_obj_delete(&ui_TimeSettingParamPanelDayRoller);
  lvgl_obj_delete(&ui_TimeSettingParamPanelMonthRoller);
  lvgl_obj_delete(&ui_TimeSettingParamPanelYearRoller);
  lvgl_obj_delete(&ui_TimeSettingParamPanel);
  lvgl_obj_delete(&ui_TimeSettingPanelChildPanelTickIconPanel);
  lvgl_obj_delete(&ui_TimeSettingPanelChildPanelRightIconPanel);
  lvgl_obj_delete(&ui_TimeSettingPanelChildPanelLabel);
  lvgl_obj_delete(&ui_TimeSettingPanelChildPanel);
  lvgl_obj_delete(&ui_TimeSettingPanel);
  lvgl_obj_delete(&ui_NetworkSynchronizationPanelChildPanelSwitch);
  lvgl_obj_delete(&ui_NetworkSynchronizationPanelChildPanelLabel);
  lvgl_obj_delete(&ui_NetworkSynchronizationPanelChildPanelLabel);
  lvgl_obj_delete(&ui_NetworkSynchronizationPanel);
  lvgl_obj_delete(&ui_Hours24PanelChildPanelSwitch);
  lvgl_obj_delete(&ui_Hours24PanelChildPanelLabel);
  lvgl_obj_delete(&ui_Hours24PanelChildPanel);
  lvgl_obj_delete(&ui_Hours24Panel);
  lvgl_obj_delete(&ui_DataAndTimePanelMainLabel);
  lvgl_obj_delete(&ui_DataAndTimePanelReturnPanel);
  lvgl_obj_delete(&ui_DataAndTimePanel);
  lvgl_obj_delete(&ui_UserSettingPanel5PanelIcon);
  lvgl_obj_delete(&ui_UserSettingPanel5PanelChildPanelLable);
  lvgl_obj_delete(&ui_UserSettingPanel5PanelChildPanel);
  lvgl_obj_delete(&ui_UserSettingPanel5Panel);
  lvgl_obj_delete(&ui_UserSettingPanel4PanelIcon);
  lvgl_obj_delete(&ui_UserSettingPanel4PanelChildPanelLable);
  lvgl_obj_delete(&ui_UserSettingPanel4PanelChildPanel);
  lvgl_obj_delete(&ui_UserSettingPanel4Panel);
  lvgl_obj_delete(&ui_UserSettingPanel3PanelChildPanelSwitch);
  lvgl_obj_delete(&ui_UserSettingPanel3PanelChildPanelLable);
  lvgl_obj_delete(&ui_UserSettingPanel3PanelChildPanel);
  lvgl_obj_delete(&ui_UserSettingPanel3Panel);
  lvgl_obj_delete(&ui_UserSettingPanel2PanelChildPanelAddPanel);
  lvgl_obj_delete(&ui_UserSettingPanel2PanelChildPanelValueLable);
  lvgl_obj_delete(&ui_UserSettingPanel2PanelChildPanelMinusPanel);
  lvgl_obj_delete(&ui_UserSettingPanel2PanelChildPanelLable);
  lvgl_obj_delete(&ui_UserSettingPanel2PanelChildPanel);
  lvgl_obj_delete(&ui_UserSettingPanel2Panel);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanelDis3Panel);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanelDis2Panel);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanelDis1Panel);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanelAddPanel);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanelMinusPanel);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanelLable);
  lvgl_obj_delete(&ui_UserSettingPanel1PanelChildPanel);
  lvgl_obj_delete(&ui_UserSettingPanel1Panel);
  lvgl_obj_delete(&ui_UserSettingPanelLabel);
  lvgl_obj_delete(&ui_UserSettingPanelReturnPanel);
  lvgl_obj_delete(&ui_UserSettingPanel);
  lvgl_obj_delete(&ui_UserSettingScreen);

End:
  return;
}

void UserSettingPanelReturnPanelClickedCallback(lv_event_t * e)
{
  _ui_screen_change(&ui_MenuScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_MenuScreen_screen_init);
}

void UserSettingPanel1PanelChildPanelAdjustPanelClickedCallback(lv_event_t * e)
{
  if(e->target == ui_UserSettingPanel1PanelChildPanelMinusPanel)
  {
    if(GetBL_level() > 50)
    {
      system_update_backlight(GetBL_level() - 25);
    }
    else
    {
      system_update_backlight(50);
    }
  }
  else if(e->target == ui_UserSettingPanel1PanelChildPanelAddPanel)
  {
    if(GetBL_level() < 100)
    {
      system_update_backlight(GetBL_level() + 25);
    }
    else
    {
      system_update_backlight(100);
    }
  }
  // user_setting_screen_refresh();
  /*亮度刷新*/
  if(GetBL_level() == 50)
  {
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(GetBL_level() == 75)
  {
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(GetBL_level() == 100)
  {
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
  }
}

void UserSettingPanel2PanelChildPanelAdjustPanelClickedCallback(lv_event_t * e)
{
  char text[20];
  uint8_t value = 0;
  if(e->target == ui_UserSettingPanel2PanelChildPanelAddPanel)
  {
    value = GetScreenOffTime() + 30;
    if(value > 180)
    {
      value = 180;
    }
    SetScreenOffTime(value);
  }
  else if(e->target == ui_UserSettingPanel2PanelChildPanelMinusPanel)
  {
    value = GetScreenOffTime() - 30;
    if(value < 60)
    {
      value = 60;
    }
    SetScreenOffTime(value);
  }

  lv_snprintf(text, sizeof(text), "%dS", GetScreenOffTime());
  _ui_label_set_property(ui_UserSettingPanel2PanelChildPanelValueLable,_UI_LABEL_PROPERTY_TEXT, text);
}

void UserSettingPanel3PanelChildPanelSwitchClickedCallback(lv_event_t * e)
{
  if(lv_obj_has_state(e->target, LV_STATE_CHECKED))
  {
    SetKey_Lock(1);
  }
  else
  {
    SetKey_Lock(0);
  }
}

void UserSettingPanel4PanelChildPanelClickedCallback(lv_event_t * e)
{
  char text[20];
  _ui_flag_modify(ui_DataAndTimePanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);

  _ui_flag_modify(ui_TimeSettingParamPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  _ui_flag_modify(ui_TimeSettingPanelChildPanelRightIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  _ui_flag_modify(ui_TimeSettingPanelChildPanelTickIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);

  /*刷新时间*/
  // rtc_time_cache_t rtc_time;
  // if(system_get_rtc_time(&rtc_time) == ESP_OK)
  //   {
  //     if((GetHour1224() == 0) && rtc_time.hour > 12)
  //     {
  //       lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", rtc_time.year, rtc_time.month,
  //               rtc_time.day, rtc_time.hour - 12, rtc_time.min);
  //     }
  //     else
  //     {
  //       lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", rtc_time.year, rtc_time.month,
  //               rtc_time.day, rtc_time.hour, rtc_time.min);
  //     }
      
  //     _ui_label_set_property(ui_TimeSettingPanelChildPanelLabel,
  //                         _UI_LABEL_PROPERTY_TEXT, text);
  //   }

  if(GetNetConnection())
  {
    _ui_flag_modify(ui_TimeSettingPanelChildPanelRightIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  }
}

void UserSettingPanel5PanelChildPanelClickedCallback(lv_event_t * e)
{
  //_ui_screen_change(&ui_EngineerSettingScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_EngineerSettingScreen_screen_init);
  _ui_flag_modify(ui_EngineerPasswordBackPanel, LV_OBJ_FLAG_HIDDEN,_UI_MODIFY_FLAG_REMOVE);

  /*清除密码界面所有信息*/
  lv_textarea_set_text(ui_EngineerPasswordInputPanelDisArea, "");
  _ui_state_modify(ui_EngineerPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  _ui_state_modify(ui_EngineerPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  _ui_state_modify(ui_EngineerPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  _ui_state_modify(ui_EngineerPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);

  _ui_flag_modify(ui_EngineerPasswordInputPanelErrorLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
}

void DataAndTimePanelReturnPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_DataAndTimePanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
}

void Hours24PanelChildPanelSwitchClickedCallback(lv_event_t * e)
{
  if(lv_obj_has_state(e->target, LV_STATE_CHECKED))
  {
    SetHour1224(1);
  }
  else
  {
    SetHour1224(0);
  }
}

void NetworkSynchronizationPanelChildPanelSwitchClicked(lv_event_t * e)
{
  if(lv_obj_has_state(e->target, LV_STATE_CHECKED))
  {
    SetNetConnection(1);

    /*定时状态刷新*/
    prog_sch_state_reset();
    /*编程状态刷新*/
    programme_state_reset();
    /*睡眠状态刷新*/
    prog_sleep_state_reset();
    /*设置时间不允许点击*/
    _ui_flag_modify(ui_TimeSettingPanelChildPanel, LV_OBJ_FLAG_CLICKABLE, _UI_MODIFY_FLAG_REMOVE);
    /*隐藏时间设置相关内容*/
    _ui_flag_modify(ui_TimeSettingParamPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_TimeSettingPanelChildPanelRightIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_TimeSettingPanelChildPanelTickIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  }
  else 
  {
    SetNetConnection(0);
    /*设置时间允许点击*/
    _ui_flag_modify(ui_TimeSettingPanelChildPanel, LV_OBJ_FLAG_CLICKABLE, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_TimeSettingPanelChildPanelRightIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  }
}

void TimeSettingPanelClickedCallback(lv_event_t * e)
{

}

void TimeSettingPanelChildPanelClickedCallback(lv_event_t * e)
{
  char text[50];
  if(GetNetConnection() == 0)
  {
    _ui_flag_modify(ui_TimeSettingParamPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
    _ui_flag_modify(ui_TimeSettingPanelChildPanelRightIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_TimeSettingPanelChildPanelTickIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);

    rtc_time_cache_t rtc_time;
    if(system_get_rtc_time(&rtc_time) == ESP_OK)
    {
    //   if((GetHour1224() == 0) && rtc_time.hour > 12)
    //   {
    //     lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", rtc_time.year, rtc_time.month,
    //             rtc_time.day, rtc_time.hour - 12, rtc_time.min);
    //   }
    //   else
    //   {
    //     lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", rtc_time.year, rtc_time.month,
    //             rtc_time.day, rtc_time.hour, rtc_time.min);
    //   }
    //   _ui_label_set_property(ui_TimeSettingPanelChildPanelLabel,
    //                       _UI_LABEL_PROPERTY_TEXT, text);

      uint8_t DayOfMonthNumber =
        lv_roller_get_option_cnt(ui_TimeSettingParamPanelDayRoller);
    SetMonthParam(rtc_time.year, rtc_time.month, DayOfMonthNumber);

    lv_roller_set_selected(ui_TimeSettingParamPanelYearRoller, rtc_time.year -2025, LV_ANIM_OFF);
    lv_roller_set_selected(ui_TimeSettingParamPanelMonthRoller, rtc_time.month -1, LV_ANIM_OFF);
    lv_roller_set_selected(ui_TimeSettingParamPanelDayRoller, rtc_time.day -1, LV_ANIM_OFF);
    lv_roller_set_selected(ui_TimeSettingParamPanelHourRoller, rtc_time.hour, LV_ANIM_OFF);
    lv_roller_set_selected(ui_TimeSettingParamPanelMinuteRoller, rtc_time.min, LV_ANIM_OFF);
    }
  }
  else
  {

  }
}

void TimeSettingPanelChildPanelRightIconPanelClickedCallback(lv_event_t * e)
{
  
}

void TimeSettingPanelChildPanelTickIconPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_TimeSettingParamPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  _ui_flag_modify(ui_TimeSettingPanelChildPanelTickIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  if(GetNetConnection() == 0)
  {
    _ui_flag_modify(ui_TimeSettingPanelChildPanelRightIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  }

  char text[33];
  uint16_t year = lv_roller_get_selected(ui_TimeSettingParamPanelYearRoller) +
                  USER_SETTING_YEAR_OFFSET;
  uint8_t month = lv_roller_get_selected(ui_TimeSettingParamPanelMonthRoller) + 1;
  uint8_t day = lv_roller_get_selected(ui_TimeSettingParamPanelDayRoller) + 1;
  uint8_t hour = lv_roller_get_selected(ui_TimeSettingParamPanelHourRoller);
  uint8_t minute = lv_roller_get_selected(ui_TimeSettingParamPanelMinuteRoller);
  // lv_snprintf(text, sizeof(text), "%04d年%02d月%02d日  %02d:%02d", year, month,
  //             day, hour, minute);
  // if((GetHour1224() == 0) && hour > 12)
  //   {
  //     lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", year, month,
  //             day, hour - 12, minute);
  //   }
  //   else
  //   {
  //     lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", year, month,
  //             day, hour, minute);
  //   }
  //   _ui_label_set_property(ui_TimeSettingPanelChildPanelLabel,
  //                        _UI_LABEL_PROPERTY_TEXT, text);

  // system_time_t time = {
  //   .sec = 0,
  //   .min = minute,
  //   .hour = hour,
  //   .day = day,
  //   .month = month,
  //   .year = (uint8_t)(year-2000)
  // };
  //SetSystemTime(time);
  system_set_rtc_time(year, month, day, hour, minute, 0);
  programme_state_reset();
  prog_sch_state_reset();
  prog_sleep_state_reset();
}

/**
 * @brief Determine whether it is a leap year.
 *
 * @param year
 * @return unsigned char leap year return 1
 */
static uint8_t IsLeapYear(uint16_t year) {
  if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0))
    return 1;
  else
    return 0;
}

/**
 * @brief Calculate the number of days per month.
 *
 * @param year
 * @param month
 * @return uint8_t
 */
uint8_t CalcDayOfMonth(uint16_t year, uint8_t month) {
  const unsigned char MonthTable[12] = {31, 28, 31, 30, 31, 30,
                                        31, 31, 30, 31, 30, 31};

  if (month != 2)
    return MonthTable[month - 1];
  else
    /* 闰年二月增加一天 */
    return MonthTable[month - 1] + IsLeapYear(year);
}

/**
 * @brief Set the Month Param.
 * @param year
 * @param month
 * @param param_len
 * @return true
 * @return false
 */
bool SetMonthParam(uint16_t year, uint8_t month, uint8_t param_len) {
  bool state = false;
  uint8_t DayOfMonth = 31;
  if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
      (month == 8) || (month == 10) || (month == 12))
    DayOfMonth = 31;
  else if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
    DayOfMonth = 30;
  else if (month == 2)
    DayOfMonth = CalcDayOfMonth(year, month);
  if (param_len == DayOfMonth) goto End;
  char text[93] = {0x00};
  memset(text, 0, sizeof(text));
  uint8_t count = 01;
  /* 长度要算上结束符号 \0 */
  for (; count < DayOfMonth; count++)
    lv_snprintf(text + (count - 1) * strlen("01\n"), strlen("01\n") + 1,
                "%02d\n", count);
  lv_snprintf(text + (count - 1) * strlen("01\n"), strlen("01\n") + 1, "%02d",
              DayOfMonth);
  lv_roller_set_options(ui_TimeSettingParamPanelDayRoller, text,
                        LV_ROLLER_MODE_INFINITE);
  state = true;

End:
  return state;
}

void TimeSettingParamPanelYearRollerValueChangeCallback(lv_event_t * e)
{
  uint16_t year = lv_roller_get_selected(ui_TimeSettingParamPanelYearRoller) +
                  USER_SETTING_YEAR_OFFSET;
  uint8_t month = lv_roller_get_selected(ui_TimeSettingParamPanelMonthRoller) + 1;
  uint8_t DayOfMonthNumber =
      lv_roller_get_option_cnt(ui_TimeSettingParamPanelDayRoller);
  uint8_t day = lv_roller_get_selected(ui_TimeSettingParamPanelDayRoller) + 1;
  // if ((RollerTarget == ui_TimeSettingParamPanelYearRoller) ||
  //     (RollerTarget == ui_TimeSettingParamPanelMonthRoller)) {
    bool SetFlag = SetMonthParam(year, month, DayOfMonthNumber);
    if (SetFlag) {
      SetFlag = false;
      day = 1;
      _ui_roller_set_property(ui_TimeSettingParamPanelDayRoller,
                              _UI_ROLLER_PROPERTY_SELECTED, 0);
    }
}

void TimeSettingParamPanelMonthRollerValueChangeCallback(lv_event_t * e)
{
  uint16_t year = lv_roller_get_selected(ui_TimeSettingParamPanelYearRoller) +
                  USER_SETTING_YEAR_OFFSET;
  uint8_t month = lv_roller_get_selected(ui_TimeSettingParamPanelMonthRoller) + 1;
  uint8_t DayOfMonthNumber =
      lv_roller_get_option_cnt(ui_TimeSettingParamPanelDayRoller);
  uint8_t day = lv_roller_get_selected(ui_TimeSettingParamPanelDayRoller) + 1;
  // if ((RollerTarget == ui_TimeSettingParamPanelYearRoller) ||
  //     (RollerTarget == ui_TimeSettingParamPanelMonthRoller)) {
    bool SetFlag = SetMonthParam(year, month, DayOfMonthNumber);
    if (SetFlag) {
      SetFlag = false;
      day = 1;
      _ui_roller_set_property(ui_TimeSettingParamPanelDayRoller,
                              _UI_ROLLER_PROPERTY_SELECTED, 0);
    }
}

void TimeSettingParamPanelDayRollerValueChangeCallback(lv_event_t * e)
{
  
}

void TimeSettingParamPanelHourRollerValueChangeCallback(lv_event_t * e)
{

}

void TimeSettingParamPanelMinuteRollerValueChangeCallback(lv_event_t * e)
{

}

void EngineerPasswordBackPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_EngineerPasswordBackPanel, LV_OBJ_FLAG_HIDDEN,_UI_MODIFY_FLAG_ADD);
}

void EngineerPasswordReturnPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_EngineerPasswordBackPanel, LV_OBJ_FLAG_HIDDEN,_UI_MODIFY_FLAG_ADD);
}

void EngineerPasswordInputPanelKeyboardReadyCallback(lv_event_t * e)
{
  const uint32_t TextMaxLength =
      lv_textarea_get_max_length(ui_EngineerPasswordInputPanelDisArea);
  if ((TextMaxLength == 0) || (TextMaxLength > 4)) {
    USER_LOGE(TAG,
              "ConfirmUnitParamParam ui_UnitParamParamTextArea "
              "TextMaxLength error");
    goto End;
  }

  char text[5] = {0};
  memcpy(text,
    lv_textarea_get_text(ui_EngineerPasswordInputPanelDisArea),
    TextMaxLength);
  /* 删除已输入的密码 */
  
  lv_textarea_set_text(ui_EngineerPasswordInputPanelDisArea, "");
  uint8_t count = 0;
  for (; count < TextMaxLength; count++)
    if (!isdigit((int)text[count])) break;

  uint8_t CountVaild = TextMaxLength;
  uint16_t password_input = atoi(text);

  if(password_input == 1230)//密码正确
  {
    //_ui_flag_modify(ui_EngineerPasswordBackPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_EngineerPasswordInputPanelErrorLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    //_ui_flag_modify(ui_EngineerPasswordBackPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_screen_change(&ui_EngineerSettingScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_EngineerSettingScreen_screen_init);
  }
  else
  {
    _ui_flag_modify(ui_EngineerPasswordInputPanelErrorLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  }
  
End:
  return;
}

void EngineerPasswordInputPanelKeyboardClickedCallback(lv_event_t * e)
{
  if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_EngineerPasswordInputPanelDisArea)) == 0)
  {
    _ui_state_modify(ui_EngineerPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_EngineerPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_EngineerPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_EngineerPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_EngineerPasswordInputPanelDisArea)) == 1)
  {
    _ui_state_modify(ui_EngineerPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_EngineerPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_EngineerPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_EngineerPasswordInputPanelDisArea)) == 2)
  {
    _ui_state_modify(ui_EngineerPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_EngineerPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_EngineerPasswordInputPanelDisArea)) == 3)
  {
    _ui_state_modify(ui_EngineerPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_EngineerPasswordInputPanelDisArea)) == 4)
  {
    _ui_state_modify(ui_EngineerPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_EngineerPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
  }
}


void user_setting_screen_refresh(void)
{
  char text[50];
  if(lv_scr_act() == ui_UserSettingScreen)
  {
    /*亮度刷新*/
    if(GetBL_level() == 50)
    {
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    }
    else if(GetBL_level() == 75)
    {
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    }
    else if(GetBL_level() == 100)
    {
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
      _ui_state_modify(ui_UserSettingPanel1PanelChildPanelDis3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    }

    /*童锁开关刷新*/
    if(GetKey_Lock())
    {
      _ui_state_modify(ui_UserSettingPanel3PanelChildPanelSwitch, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    }
    else
    {
      //_ui_state_modify(ui_UserSettingPanel3PanelChildPanelSwitch, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    }

    /*屏幕超时时间刷新*/

    lv_snprintf(text, sizeof(text), "%dS", GetScreenOffTime());
    _ui_label_set_property(ui_UserSettingPanel2PanelChildPanelValueLable,_UI_LABEL_PROPERTY_TEXT, text);

    //_ui_label_text_set(ui_UserSettingPanel2PanelChildPanelValueLable, GetScreenOffTime());

    /*24小时制*/
    if(GetHour1224())
    {
      _ui_state_modify(ui_Hours24PanelChildPanelSwitch, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    }
    else
    {
      _ui_state_modify(ui_Hours24PanelChildPanelSwitch, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    }

    /*刷新时间*/
  rtc_time_cache_t rtc_time;
  if(system_get_rtc_time(&rtc_time) == ESP_OK)
    {
      if((GetHour1224() == 0) && rtc_time.hour > 12)
      {
        lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", rtc_time.year, rtc_time.month,
                rtc_time.day, rtc_time.hour - 12, rtc_time.min);
      }
      else
      {
        lv_snprintf(text, sizeof(text), "%04d/%02d/%02d   %02d:%02d", rtc_time.year, rtc_time.month,
                rtc_time.day, rtc_time.hour, rtc_time.min);
      }
      
      _ui_label_set_property(ui_TimeSettingPanelChildPanelLabel,
                          _UI_LABEL_PROPERTY_TEXT, text);
    }

    /*联网同步*/
    if(GetNetConnection())
    {
      _ui_state_modify(ui_NetworkSynchronizationPanelChildPanelSwitch, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    }
    else
    {
      _ui_state_modify(ui_NetworkSynchronizationPanelChildPanelSwitch, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    }
  }
}
