#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_main_screen_event.h"
#include "esp_timer.h"

#include "parameter.h"
#include "modbus_register_map.h"
#include "rtc_aip8563.h"
#include "i2c_mcu.h"
#include "system_control.h"
#include "output_control.h"

static const char *TAG = "ui_main_screen_event";

// /*定时器回调*/
// static void main_screen_timer_callback(void);


void MainScreenLoadStartCallback(lv_event_t * e)
{
  MainScreenRefresh();
}

void MainScreenLoadedcallback(lv_event_t * e)
{
  
}

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

  lvgl_obj_delete(&ui_MainScreenWeekUpPanel);
  lvgl_obj_delete(&ui_ChildLockPasswordDot4Panel);
  lvgl_obj_delete(&ui_ChildLockPasswordDot3Panel);
  lvgl_obj_delete(&ui_ChildLockPasswordDot2Panel);
  lvgl_obj_delete(&ui_ChildLockPasswordDot1Panel);
  lvgl_obj_delete(&ui_ChildLockPasswordInputPanelKeyboard);
  lvgl_obj_delete(&ui_ChildLockPasswordInputPanelErrorLable);
  lvgl_obj_delete(&ui_ChildLockPasswordInputPanelDisArea);
  lvgl_obj_delete(&ui_ChildLockPasswordReturnPanel);
  lvgl_obj_delete(&ui_ChildLockPasswordInputPanelLable);
  lvgl_obj_delete(&ui_ChildLockPasswordInputPanel);
  lvgl_obj_delete(&ui_MainScreenChildLockPasswordBackPanel);
  lvgl_obj_delete(&ui_MainScreenChildLockTouchPanel);
  lvgl_obj_delete(&ui_MainScreenOffCurrentTempLable);
  lvgl_obj_delete(&ui_MainScreenOffCurrentTempTextLable);
  lvgl_obj_delete(&ui_MainScreenOffClickedPanel);
  lvgl_obj_delete(&ui_MainScreenOffIconPanel);
  lvgl_obj_delete(&ui_SaswellIconPanel);
  lvgl_obj_delete(&ui_MainScreenOffPanel);
  lvgl_obj_delete(&ui_TurnOffClickedConfirmLabel);
  lvgl_obj_delete(&ui_TurnOffClickedConfirmPanel);
  lvgl_obj_delete(&ui_TurnOffClickedCancelLabel);
  lvgl_obj_delete(&ui_TurnOffClickedCancelPanel);
  lvgl_obj_delete(&ui_TurnOffBackgroundLabel);
  lvgl_obj_delete(&ui_TurnOffBackgroundPanel);
  lvgl_obj_delete(&ui_MainScreenTurnOffPanel);
  lvgl_obj_delete(&ui_MainScreenMuneButtonPanel);
  lvgl_obj_delete(&ui_ModeButtonTextLabel);
  lvgl_obj_delete(&ui_ModeButtonIconPanel);
  lvgl_obj_delete(&ui_MainScreenModeButtonPanel);
  lvgl_obj_delete(&ui_MainScreenOnOffButtonPanel);
  lvgl_obj_delete(&ui_SetTempMainSetTempValueLable);
  lvgl_obj_delete(&ui_SetTempMainSetTempLable);
  lvgl_obj_delete(&ui_SetTempMainMinusIconPanel);
  lvgl_obj_delete(&ui_SetTempMainAddIconPanel);
  lvgl_obj_delete(&ui_SetTempMainPanel);
  lvgl_obj_delete(&ui_ThirdGear1GearImage);
  lvgl_obj_delete(&ui_ThirdGearTextLable);
  lvgl_obj_delete(&ui_ThirdGearClickFanIconImage);
  lvgl_obj_delete(&ui_ThirdGearClickTextLable);
  lvgl_obj_delete(&ui_ThirdGearClickPanel);
  lvgl_obj_delete(&ui_ThirdGearMainPanel);
  lvgl_obj_delete(&ui_MainScreenModeLabel);
  lvgl_obj_delete(&ui_CurrentTempPointNumbLable);
  lvgl_obj_delete(&ui_MainScreenTempUnitLable);
  lvgl_obj_delete(&ui_MainScreenTempUnitPanel);
  lvgl_obj_delete(&ui_CurrentTempNumbLable);
  lvgl_obj_delete(&ui_CurrentTemphintLabel);
  lvgl_obj_delete(&ui_ModebackgroundPanel);
  lvgl_obj_delete(&ui_MainScreenProgramModeLabel);
  lvgl_obj_delete(&ui_MainScreenOpenLabel);
  lvgl_obj_delete(&ui_MainScreenValveOpenDegreePanel);
  lvgl_obj_delete(&ui_MainScreenError2Label);
  lvgl_obj_delete(&ui_MainScreenError1Label);
  lvgl_obj_delete(&ui_MainScreenTimeLable);
  lvgl_obj_delete(&ui_ECOIconPanel);
  lvgl_obj_delete(&ui_WifiIconPanel);
  lvgl_obj_delete(&ui_ChildLockIconPanel);
  lvgl_obj_delete(&ui_SaswellPanel);
  lvgl_obj_delete(&ui_MainScreenOnPanel);
  lvgl_obj_delete(&ui_MainScreen);

End:
  return;
}

void MainThirdGearClickPanelClickedCallback(lv_event_t * e)
{
  if(GetWork_mode() != 3)
  {
    if(GetFan_speed() == 3)
    {
      SetFan_speed(0);
    }
    else if(GetFan_speed() < 3)
    {
      SetFan_speed(GetFan_speed() + 1);
    }
  }
  /*通风模式不允许设置自动风*/
  else if(GetWork_mode() == 3)
  {
    if(GetFan_speed() == 3)
    {
      SetFan_speed(1);
    }
    else if(GetFan_speed() < 3)
    {
      SetFan_speed(GetFan_speed() + 1);
    }
  }
  
  switch (GetFan_speed())
  {
    /*自动*/
    case 0: 
      _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "AUTO");
      break;

    /*低风*/
    case 1: 
      _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "LOW");
      lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan1_png);

      break;

    /*中风*/  
    case 2: 
      _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "MID");
      lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan2_png);
      break;

    /*高风*/
    case 3: 
      _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "HIGH");
      lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan3_png);
      break;
  }
}

void MainSetTempMainAddIconPanelClickedcallback(lv_event_t * e)
{
  char text[10];
  if(e->code != LV_EVENT_CLICKED)
  {
    _ui_state_modify(ui_SetTempMainPanel, LV_STATE_EDITED, _UI_MODIFY_STATE_ADD);
    if(GetSetPoint() >= GetSet_upper_temperature_limit())
    {
      SetSetPoint(GetSet_upper_temperature_limit());
    }
    else
    {
      if(Gettemperature_unit())
      {
        SetSetPoint(GetSetPoint() + 1);
      }
      else
      {
        SetSetPoint(GetSetPoint() + 0.5);
      }
    }
  }
  else
  {
    _ui_state_modify(ui_SetTempMainPanel, LV_STATE_EDITED, _UI_MODIFY_STATE_REMOVE);
  }
  /*设定温度整数部分*/
  if(Gettemperature_unit())
  {
    snprintf(text, sizeof(text), "%.0f", GetSetPoint());
  }
  else
  {
    snprintf(text, sizeof(text), "%.1f", GetSetPoint());
  }
  _ui_label_set_property(ui_SetTempMainSetTempValueLable, _UI_LABEL_PROPERTY_TEXT, text);
}

void MainSetTempMainMinusIconPanelClickedCallback(lv_event_t * e)
{
  char text[10];
  if(e->code != LV_EVENT_CLICKED)
  {
    _ui_state_modify(ui_SetTempMainPanel, LV_STATE_EDITED, _UI_MODIFY_STATE_ADD);
    if(GetSetPoint() <= GetSet_lower_temperature_limit())
    {
      SetSetPoint(GetSet_lower_temperature_limit());
    }
    else
    {
      if(Gettemperature_unit())
      {
        SetSetPoint(GetSetPoint() - 1);
      }
      else
      {
        SetSetPoint(GetSetPoint() - 0.5);
      }
    }
  }
  else
  {
    _ui_state_modify(ui_SetTempMainPanel, LV_STATE_EDITED, _UI_MODIFY_STATE_REMOVE);
  }
  /*设定温度整数部分*/
  if(Gettemperature_unit())
  {
    snprintf(text, sizeof(text), "%.0f", GetSetPoint());
  }
  else
  {
    snprintf(text, sizeof(text), "%.1f", GetSetPoint());
  }
  _ui_label_set_property(ui_SetTempMainSetTempValueLable, _UI_LABEL_PROPERTY_TEXT, text);
}

void MainScreenOnOffButtonPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_MainScreenTurnOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
}

void MainScreenModeButtonPanelCallback(lv_event_t * e)
{
  /*仅支持制冷和通风模式*/
  if(GetSystem_mode() == 0)
  {
    if(GetWork_mode() == 1)
    {
      SetWork_mode(3);
    }
    else
    {
      SetWork_mode(1);
    }
  }
  else if(GetSystem_mode() == 1)
  {
    if(GetWork_mode() == 1)
    {
      SetWork_mode(2);
    }
    else if(GetWork_mode() == 2)
    {
      SetWork_mode(3);
    }
    else
    {
      SetWork_mode(1);
    }
    SetFan_speed(1);
  }
  
  uint8_t fan_state, valve_opening = 0;
  get_oc_fan_valve_state(&fan_state, &valve_opening);

  switch (GetWork_mode())
  {
    /*自动*/
    case 0: 
      lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_auto_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_mode_auto_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_label_set_text(ui_ModeButtonTextLabel, "Auto");
      _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
      
      break;

    /*制冷*/
    case 1: 
      lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_cool_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_mode_cool_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_label_set_text(ui_ModeButtonTextLabel, "Cooling");

      if(valve_opening != 0)
      {
        _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        lv_label_set_text(ui_MainScreenModeLabel, "Cooling");
        lv_obj_set_style_text_color(ui_MainScreenModeLabel, lv_color_hex(0x34D5DB), LV_PART_MAIN | LV_STATE_DEFAULT);
      }
      else 
      {
        _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
      }
      break;

    /*制热*/
    case 2: 
      lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_heat_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_mode_heat_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_label_set_text(ui_ModeButtonTextLabel, "Heating");
      
      if(valve_opening != 0)
      {
        _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        lv_label_set_text(ui_MainScreenModeLabel, "Heating");
        lv_obj_set_style_text_color(ui_MainScreenModeLabel, lv_color_hex(0xFFB11A), LV_PART_MAIN | LV_STATE_DEFAULT);
      }
      else 
      {
        _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
      }
      break;

    /*通风*/
    case 3: 
      lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_ventilate_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_ventilate_png, LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_label_set_text(ui_ModeButtonTextLabel, "Vent");
      _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);          
      break;

    default:
      break;
  }
}

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

void MainTurnOffClickedCancelPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_MainScreenTurnOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
}

void MainTurnOffClickedConfirmPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_MainScreenTurnOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  _ui_flag_modify(ui_MainScreenOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  SetOnOffState(0);
}

void MainScreenOffClickedPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_MainScreenOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  SetOnOffState(1);
}


void MainScreenChildLockPasswordBackPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_MainScreenChildLockPasswordBackPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);

  /*清除密码界面所有信息*/
  lv_textarea_set_text(ui_ChildLockPasswordInputPanelDisArea, "");
  _ui_state_modify(ui_ChildLockPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  _ui_state_modify(ui_ChildLockPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  _ui_state_modify(ui_ChildLockPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  _ui_state_modify(ui_ChildLockPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);

  _ui_flag_modify(ui_ChildLockPasswordInputPanelErrorLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
}

void ChildLockPasswordReturnPanelClickedCallback(lv_event_t * e)
{
  _ui_flag_modify(ui_MainScreenChildLockPasswordBackPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
}

void ChildLockPasswordInputPanelKeyboardReadyCallback(lv_event_t * e)
{
  const uint32_t TextMaxLength =
      lv_textarea_get_max_length(ui_ChildLockPasswordInputPanelDisArea);
  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_ChildLockPasswordInputPanelDisArea),
    TextMaxLength);
  /* 删除已输入的密码 */
  // for (uint8_t i = 0; i < TextMaxLength; i++)
  //   lv_textarea_del_char(ui_HomeInqurePasswordInputPanelDisArea);
  lv_textarea_set_text(ui_ChildLockPasswordInputPanelDisArea, "");
  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 == 2301)//密码正确
  {
    _ui_flag_modify(ui_MainScreenChildLockPasswordBackPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_ChildLockPasswordInputPanelErrorLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    _ui_flag_modify(ui_MainScreenChildLockTouchPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  }
  else
  {
    _ui_flag_modify(ui_ChildLockPasswordInputPanelErrorLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  }
  
End:
  return;
}

void ChildLockPasswordInputPanelKeyboardKeyCallback(lv_event_t * e)
{
  // const uint32_t TextMaxLength =
  //     lv_textarea_get_length(ui_ChildLockPasswordInputPanelDisArea);

  if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_ChildLockPasswordInputPanelDisArea)) == 0)
  {
    _ui_state_modify(ui_ChildLockPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_ChildLockPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_ChildLockPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_ChildLockPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_ChildLockPasswordInputPanelDisArea)) == 1)
  {
    _ui_state_modify(ui_ChildLockPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_ChildLockPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_ChildLockPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_ChildLockPasswordInputPanelDisArea)) == 2)
  {
    _ui_state_modify(ui_ChildLockPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
    _ui_state_modify(ui_ChildLockPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_ChildLockPasswordInputPanelDisArea)) == 3)
  {
    _ui_state_modify(ui_ChildLockPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
  }
  else if(_lv_txt_get_encoded_length(lv_textarea_get_text(ui_ChildLockPasswordInputPanelDisArea)) == 4)
  {
    _ui_state_modify(ui_ChildLockPasswordDot1Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot2Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot3Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
    _ui_state_modify(ui_ChildLockPasswordDot4Panel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
  }
}

void MainScreenRefresh(void)
{
  char text[50];
  static uint16_t Fan_speed_record = 100;
  static uint16_t Work_mode_record = 100;
  static uint16_t On_Off_record = 100;
  static float Set_Temp_record = 1000;
  static float Room_Temp_record = 1000;

  if(GetOnOffState() == 0)
  {
    if(lv_scr_act() != ui_MainScreen && lv_scr_act() != ui_ProductTestScreen)
    {
      _ui_screen_change(&ui_MainScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_MainScreen_screen_init);   
    }
  }

  if(lv_scr_act() == ui_MainScreen)
  {
    /*设定温度整数部分*/
    //if(Set_Temp_record != GetSetPoint())
    {
      Set_Temp_record = GetSetPoint();
      if(Gettemperature_unit())
      {
        snprintf(text, sizeof(text), "%.0f", GetSetPoint());
      }
      else
      {
        snprintf(text, sizeof(text), "%.1f", GetSetPoint());
      }
      _ui_label_set_property(ui_SetTempMainSetTempValueLable, _UI_LABEL_PROPERTY_TEXT, text);
    }

    /*探头报错*/
    if(get_fault_code() == 0)
    {
      _ui_flag_modify(ui_MainScreenError1Label, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
      _ui_flag_modify(ui_MainScreenError2Label, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    }
    else if(get_fault_code() == 1)
    {
      _ui_flag_modify(ui_MainScreenError1Label, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
      _ui_flag_modify(ui_MainScreenError2Label, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
    }
    else if(get_fault_code() == 2)
    { 
      _ui_flag_modify(ui_MainScreenError1Label, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
      _ui_flag_modify(ui_MainScreenError2Label, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
    }

    /*环境温度*/
    //if(Room_Temp_record != get_temp_disp())
    {
      Room_Temp_record = get_temp_disp();
      if(Room_Temp_record != 999.9f)
      {
        if(get_temp_disp() < 100.0)
        {
          lv_snprintf(text, sizeof(text), "%02d", (uint16_t)(get_temp_disp()*10)/10);
          _ui_label_set_property(ui_CurrentTempNumbLable, _UI_LABEL_PROPERTY_TEXT, text);
          lv_snprintf(text, sizeof(text), ".%01d", (uint16_t)(get_temp_disp()*10)%10);
          _ui_label_set_property(ui_CurrentTempPointNumbLable, _UI_LABEL_PROPERTY_TEXT, text);
        }
        else
        {
          lv_snprintf(text, sizeof(text), "%02d", (uint16_t)(get_temp_disp())/10);
          _ui_label_set_property(ui_CurrentTempNumbLable, _UI_LABEL_PROPERTY_TEXT, text);
          lv_snprintf(text, sizeof(text), "%01d", (uint16_t)(get_temp_disp())%10);
          _ui_label_set_property(ui_CurrentTempPointNumbLable, _UI_LABEL_PROPERTY_TEXT, text);
        }
        //printf("get_temp_disp() = %.1f\r\n", get_temp_disp());
      }
      else
      {
        _ui_label_set_property(ui_CurrentTempNumbLable, _UI_LABEL_PROPERTY_TEXT, "- -");
        _ui_label_set_property(ui_CurrentTempPointNumbLable, _UI_LABEL_PROPERTY_TEXT, "-");
      }
    }

    /*首页摄氏度/华氏度切换*/
    /*摄氏度*/
    if(Gettemperature_unit() == 0)
    {
      lv_label_set_text(ui_MainScreenTempUnitLable, "℃");
      _ui_flag_modify(ui_CurrentTempPointNumbLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);

      /*关机界面显示温度*/
      if(get_temp_disp() == 999.9f)
      {
        lv_snprintf(text, sizeof(text), "Current temp - -℃");
        _ui_label_set_property(ui_MainScreenOffCurrentTempLable, _UI_LABEL_PROPERTY_TEXT, text);
      }
      else
      {
        lv_snprintf(text, sizeof(text), "Current temp %02d.%01d℃", (uint16_t)(get_temp_disp()*10)/10, (uint16_t)(get_temp_disp()*10)%10);
        _ui_label_set_property(ui_MainScreenOffCurrentTempLable, _UI_LABEL_PROPERTY_TEXT, text);
      }
    }
    /*华氏度*/
    else
    {
      lv_label_set_text(ui_MainScreenTempUnitLable, "℉");
      if(get_temp_disp() < 100.0)
      {
        _ui_flag_modify(ui_CurrentTempPointNumbLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
      }
      else
      {
        _ui_flag_modify(ui_CurrentTempPointNumbLable, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
      }

      /*关机界面显示温度*/
      if(get_temp_disp() == 999.9f)
      {
        lv_snprintf(text, sizeof(text), "Current temp - -℉");
        _ui_label_set_property(ui_MainScreenOffCurrentTempLable, _UI_LABEL_PROPERTY_TEXT, text);
      }
      else
      {
        lv_snprintf(text, sizeof(text), "Current temp %d℉", (uint16_t)(get_temp_disp()));
        _ui_label_set_property(ui_MainScreenOffCurrentTempLable, _UI_LABEL_PROPERTY_TEXT, text);
      }
    }

    /*需要一直判断童锁状态*/
    static uint8_t child_lock_record = 0xff;
    if(child_lock_record != GetKey_Lock())
    {
      child_lock_record = GetKey_Lock();
      if(GetKey_Lock())
      {
        if(lv_obj_has_flag(ui_MainScreenChildLockTouchPanel, LV_OBJ_FLAG_HIDDEN))
        {
          _ui_flag_modify(ui_MainScreenChildLockTouchPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        }
      }
      else
      {
        if(!lv_obj_has_flag(ui_MainScreenChildLockTouchPanel, LV_OBJ_FLAG_HIDDEN))
        {
          _ui_flag_modify(ui_MainScreenChildLockTouchPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
      }
    }
 

    /*刷新编程模式显示*/
    switch(GetProgram_option())
    {
      case 0:
        if(programme_state_get(0))
        {
          lv_label_set_text(ui_MainScreenProgramModeLabel, "Sleep Mode");
          _ui_flag_modify(ui_MainScreenProgramModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        }
        else
        {
          _ui_flag_modify(ui_MainScreenProgramModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
        break;

      case 1: 
        if(programme_state_get(1))
        {
          lv_label_set_text(ui_MainScreenProgramModeLabel, "Timing");
          _ui_flag_modify(ui_MainScreenProgramModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        }
        else
        {
          _ui_flag_modify(ui_MainScreenProgramModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
        break;

      case 2: 
        if(programme_state_get(2))
        {
          lv_label_set_text(ui_MainScreenProgramModeLabel, "Programming");
          _ui_flag_modify(ui_MainScreenProgramModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        }
        else
        {
          _ui_flag_modify(ui_MainScreenProgramModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
        break;

      default:
        break;
    }

    //uint16_t year, month, day, hour, minute, second, week = 0;
    rtc_time_cache_t rtc_time;
    if(system_get_rtc_time(&rtc_time) == ESP_OK)
    {
      //printf("%04d-%02d-%02d %02d:%02d:%02d %d\r\n", rtc_time.year, rtc_time.month, rtc_time.day, rtc_time.hour, rtc_time.min, rtc_time.sec, rtc_time.week);
      if(GetHour1224())
      {
        lv_snprintf(text, sizeof(text), "%02d:%02d", rtc_time.hour, rtc_time.min);
        _ui_label_set_property(ui_MainScreenTimeLable, _UI_LABEL_PROPERTY_TEXT, text);
      }
      else
      {
        if(rtc_time.hour > 12)
        {
          lv_snprintf(text, sizeof(text), "%02d:%02d", rtc_time.hour - 12, rtc_time.min);
          _ui_label_set_property(ui_MainScreenTimeLable, _UI_LABEL_PROPERTY_TEXT, text);
        }
        else
        {
          lv_snprintf(text, sizeof(text), "%02d:%02d", rtc_time.hour, rtc_time.min);
          _ui_label_set_property(ui_MainScreenTimeLable, _UI_LABEL_PROPERTY_TEXT, text);
        }
      }
    }

    /*阀门开度*/
    uint8_t fan_state, valve_opening = 0;
    get_oc_fan_valve_state(&fan_state, &valve_opening);

    lv_snprintf(text, sizeof(text), "%02d%%", valve_opening);
    _ui_label_set_property(ui_MainScreenOpenLabel, _UI_LABEL_PROPERTY_TEXT, text);
    
    
    /*风挡需要单独处理*/
    /*风速，0：自动 ，1:低风，2：中风，3：高风*/
//    if(Fan_speed_record != GetFan_speed())
    {
      Fan_speed_record = GetFan_speed();
      switch (GetFan_speed())
      {
        /*自动*/
        case 0: 
          _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "AUTO");
          break;

        /*低风*/
        case 1: 
          _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "LOW");
          lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan1_png);

          break;

        /*中风*/  
        case 2: 
          _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "MID");
          lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan2_png);
          break;

        /*高风*/
        case 3: 
          _ui_label_set_property(ui_ThirdGearTextLable, _UI_LABEL_PROPERTY_TEXT, "HIGH");
          lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan3_png);
          break;
      }
    }
    
    /*模式，0：自动 ，1：制冷  ，2：制热 ，3：通风*/
//    if(Work_mode_record != GetWork_mode())
    {
      Work_mode_record = GetWork_mode();

      /*通风模式设置温度面板需要特殊处理,或者运行ECO温度*/
      if((Work_mode_record == 3) || ((GetExternal_Input() == 2) && (get_roomcard_detect() == 0)) || (get_fault_code()))
      {
        _ui_state_modify(ui_SetTempMainPanel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
        _ui_state_modify(ui_SetTempMainAddIconPanel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
        _ui_state_modify(ui_SetTempMainMinusIconPanel, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
        _ui_state_modify(ui_SetTempMainSetTempValueLable, LV_STATE_CHECKED, _UI_MODIFY_STATE_ADD);
        _ui_flag_modify(ui_SetTempMainAddIconPanel, LV_OBJ_FLAG_CLICKABLE, _UI_MODIFY_FLAG_REMOVE);
        _ui_flag_modify(ui_SetTempMainMinusIconPanel, LV_OBJ_FLAG_CLICKABLE, _UI_MODIFY_FLAG_REMOVE);
        //lv_label_set_text(ui_SetTempMainSetTempValueLable, "0.0");
      }
      else
      {
        _ui_state_modify(ui_SetTempMainPanel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
        _ui_state_modify(ui_SetTempMainAddIconPanel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
        _ui_state_modify(ui_SetTempMainMinusIconPanel, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
        _ui_state_modify(ui_SetTempMainSetTempValueLable, LV_STATE_CHECKED, _UI_MODIFY_STATE_REMOVE);
        _ui_flag_modify(ui_SetTempMainAddIconPanel, LV_OBJ_FLAG_CLICKABLE, _UI_MODIFY_FLAG_ADD);
        _ui_flag_modify(ui_SetTempMainMinusIconPanel, LV_OBJ_FLAG_CLICKABLE, _UI_MODIFY_FLAG_ADD);
      }

      uint8_t fan_state, valve_opening = 0;
      get_oc_fan_valve_state(&fan_state, &valve_opening);

      switch (GetWork_mode())
      {
        /*自动*/
        case 0: 
          lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_auto_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_mode_auto_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_label_set_text(ui_ModeButtonTextLabel, "Auto");
          _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
          
          break;

        /*制冷*/
        case 1: 
          lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_cool_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_mode_cool_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_label_set_text(ui_ModeButtonTextLabel, "Cooling");

          if(valve_opening != 0)
          {
            _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
            lv_label_set_text(ui_MainScreenModeLabel, "Cooling");
            lv_obj_set_style_text_color(ui_MainScreenModeLabel, lv_color_hex(0x34D5DB), LV_PART_MAIN | LV_STATE_DEFAULT);
          }
          else 
          {
            _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
          }
          break;

        /*制热*/
        case 2: 
          lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_heat_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_mode_heat_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_label_set_text(ui_ModeButtonTextLabel, "Heating");
          
          if(valve_opening != 0)
          {
            _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
            lv_label_set_text(ui_MainScreenModeLabel, "Heating");
            lv_obj_set_style_text_color(ui_MainScreenModeLabel, lv_color_hex(0xFFB11A), LV_PART_MAIN | LV_STATE_DEFAULT);
          }
          else 
          {
            _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
          }
          break;

        /*通风*/
        case 3: 
          lv_obj_set_style_bg_img_src(ui_ModebackgroundPanel, &ui_img_mode_ventilate_background_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_obj_set_style_bg_img_src(ui_ModeButtonIconPanel, &ui_img_20_click_ventilate_png, LV_PART_MAIN | LV_STATE_DEFAULT);
          lv_label_set_text(ui_ModeButtonTextLabel, "Vent");
          _ui_flag_modify(ui_MainScreenModeLabel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);          
          break;

        default:
          break;
      }
    }


//    if(On_Off_record != GetOnOff())
    {
      if(GetOnOffState())
      {
        //if(lv_obj_has_flag(ui_MainScreenOffPanel, LV_OBJ_FLAG_HIDDEN))
        {
          _ui_flag_modify(ui_MainScreenOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
      }
      else
      { 
        //if(!lv_obj_has_flag(ui_MainScreenOffPanel, LV_OBJ_FLAG_HIDDEN))
        {
          _ui_flag_modify(ui_MainScreenOffPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        }
      }
    }

    /*icon刷新*/
    mainscreen_icon_refresh();

  }
}


void main_screen_timer_callback(void)
{
  static uint8_t count = 0;
  count++;
  if(count > 2)
  {
    count = 0;
  }

  uint8_t fan_state, valve_opening = 0;
  get_oc_fan_valve_state(&fan_state, &valve_opening);

  if(lv_scr_act() == ui_MainScreen)
  {
      switch(count)
      {
        // case 0: 
        //     lv_obj_set_style_bg_img_src(ui_ThirdGear2GearPanel, &ui_img_1_fan_default_png, LV_PART_MAIN | LV_STATE_DEFAULT);

        //     lv_img_set_src(ui_ThirdGearClickFanIconImage, &ui_img_20_fan_speed_icon_png);
        //   break;

        case 0: 
          if(fan_state)
          {
            lv_img_set_src(ui_ThirdGearClickFanIconImage, &ui_img_20_fan_speed_icon_png);
          }
            
          if(GetFan_speed() == 0)
          {
            lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan1_png);
          }
          break;

        case 1: 
          if(fan_state)
          {
            lv_img_set_src(ui_ThirdGearClickFanIconImage, &ui_img_20_fan_speed1_icon_png);
          }

          if(GetFan_speed() == 0)
          {
            lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan2_png);
          }
          break;

        case 2: 
          if(fan_state)
          {
            lv_img_set_src(ui_ThirdGearClickFanIconImage, &ui_img_20_fan_speed2_icon_png);
          }
          if(GetFan_speed() == 0)
          {
            lv_img_set_src(ui_ThirdGear1GearImage, &ui_img_1_fan3_png);
          }
          break;
      }
  }
}


void mainscreen_icon_refresh(void)
{
  uint8_t count = 0;
  if(GetKey_Lock())
  {
    count++;
    lv_obj_set_x(ui_ChildLockIconPanel, 107);
    _ui_flag_modify(ui_ChildLockIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  }
  else
  {
    _ui_flag_modify(ui_ChildLockIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  } 

  // if(GetWifiWorkState() == 4)
  // {
  //   count++;
  //   if(count == 1)
  //   {
  //     lv_obj_set_x(ui_WifiIconPanel, 107);
  //   }
  //   else if(count == 2)
  //   {
  //     lv_obj_set_x(ui_WifiIconPanel, 66);
  //   }
  //   _ui_flag_modify(ui_WifiIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  // }
  // else
  // {
  //   _ui_flag_modify(ui_WifiIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  // }

  if((GetExternal_Input() == 2) && (get_roomcard_detect() == 0))
  {
    count++;
    if(count == 1)
    {
      lv_obj_set_x(ui_ECOIconPanel, 90);
    }
    else if(count == 2)
    {
      lv_obj_set_x(ui_ECOIconPanel, 50);
    }
    else if(count == 3)
    {
      lv_obj_set_x(ui_ECOIconPanel, 6);
    }
    _ui_flag_modify(ui_ECOIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
  }
  else
  {
    _ui_flag_modify(ui_ECOIconPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
  }
}


void ProductTestTouchPanelClickedCallback(lv_event_t * e)
{
  static uint8_t count = 0;
  count++;
  if(count >= 5 && count < 6)
  {
    count = 10;
    _ui_screen_change(&ui_ProductTestScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_ProductTestScreen_screen_init);    
  }
}


