#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_product_test_screen_event.h"
#include "system_control.h"
#include "i2c_mcu.h"
#include "parameter.h"

void ProductTestTimer_Callback(lv_timer_t * timer);

lv_timer_t * ProductTestTimer = NULL;
static uint8_t test_time_count = 0;
void InProductTest(lv_event_t * e)
{
  if(e->code == LV_EVENT_SCREEN_LOAD_START)
  {
    set_production_test_running_flag(1);
    
    /*关闭所有继电器*/
    i2c_mcu_ao1(0);
    i2c_mcu_ao2(0);
    i2c_mcu_relay_K3(0);
    i2c_mcu_relay_K4(0);
    i2c_mcu_relay_K5(0);
    if(ProductTestTimer == NULL)
    {
      test_time_count = 0;
      ProductTestTimer = lv_timer_create(ProductTestTimer_Callback, 1000, &test_time_count);
    }
  }
}

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

  lvgl_obj_delete(&ui_ProductTestPanelPromptLabel);
  lvgl_obj_delete(&ui_ProductTestPanel);
  lvgl_obj_delete(&ui_ProductTestFirmwareVersionLabel);
  lvgl_obj_delete(&ui_ProductTestPromptLabel);
  lvgl_obj_delete(&ui_ProductTestWifiSwitch);
  lvgl_obj_delete(&ui_ProductTestWifiRssiLabel);
  lvgl_obj_delete(&ui_ProductTestTemperatureSwitch);
  lvgl_obj_delete(&ui_ProductTestNtcTemperatureLabel);
  lvgl_obj_delete(&ui_ProductTestCurrentTemperatureLabel);
  lvgl_obj_delete(&ui_ProductTestDateTimeWeekSwitch);
  lvgl_obj_delete(&ui_ProductTestWeekLabel);
  lvgl_obj_delete(&ui_ProductTestDateTimeLabel);
  lvgl_obj_delete(&ui_ProductTestRelay4PlusButtonPanel);
  lvgl_obj_delete(&ui_ProductTestRelay4MinusButtonPanel);
  lvgl_obj_delete(&ui_ProductTestRelay4LevelPanel3);
  lvgl_obj_delete(&ui_ProductTestRelay4LevelPanel2);
  lvgl_obj_delete(&ui_ProductTestRelay4LevelPanel1);
  lvgl_obj_delete(&ui_ProductTestRelayLabel5);
  lvgl_obj_delete(&ui_ProductTestRelaySwitch5);
  lvgl_obj_delete(&ui_ProductTestRelayLabel4);
  lvgl_obj_delete(&ui_ProductTestRelaySwitch4);
  lvgl_obj_delete(&ui_ProductTestRelayLabel3);
  lvgl_obj_delete(&ui_ProductTestRelaySwitch3);
  lvgl_obj_delete(&ui_ProductTestRelayLabel2);
  lvgl_obj_delete(&ui_ProductTestRelaySwitch2);
  lvgl_obj_delete(&ui_ProductTestRelayLabel1);
  lvgl_obj_delete(&ui_ProductTestRelaySwitch1);
  lvgl_obj_delete(&ui_ProductTestTopBarPromptLabel);
  lvgl_obj_delete(&ui_ProductTestTopBarPanel);
  lvgl_obj_delete(&ui_ProductTestScreen);

  if(ProductTestTimer != NULL)
  { 
    lv_timer_del(ProductTestTimer);
    ProductTestTimer = NULL;
  }

  set_production_test_running_flag(0);
  i2c_mcu_ao1(0);
  i2c_mcu_ao2(0);
  i2c_mcu_relay_K3(0);
  i2c_mcu_relay_K4(0);
  i2c_mcu_relay_K5(0);

  End:
  return;
}

void TurnProductTest(lv_event_t * e)
{
  if(e->target == ui_ProductTestRelaySwitch1)
  {
    if(lv_obj_has_state(ui_ProductTestRelaySwitch1, LV_STATE_CHECKED))
    {
      /*打开继电器*/
      i2c_mcu_ao1(1);
    }
    else
    {
      /*关闭继电器*/
      i2c_mcu_ao1(0);
    }
  }
  else if(e->target == ui_ProductTestRelaySwitch2)
  {
    if(lv_obj_has_state(ui_ProductTestRelaySwitch2, LV_STATE_CHECKED))
    {
      /*打开继电器*/
      i2c_mcu_ao2(1);
    }
    else
    {
      /*关闭继电器*/
      i2c_mcu_ao2(0);
    }
  }
  else if(e->target == ui_ProductTestRelaySwitch3)
  {
    if(lv_obj_has_state(ui_ProductTestRelaySwitch3, LV_STATE_CHECKED))
    {
      /*打开继电器*/
      i2c_mcu_relay_K3(1);
    }
    else
    {
      /*关闭继电器*/
      i2c_mcu_relay_K3(0);
    }
  }
  else if(e->target == ui_ProductTestRelaySwitch4)
  {
    if(lv_obj_has_state(ui_ProductTestRelaySwitch4, LV_STATE_CHECKED))
    {
      /*打开继电器*/
      i2c_mcu_relay_K4(1);
    }
    else
    {
      /*关闭继电器*/
      i2c_mcu_relay_K4(0);
    }
  }
  else if(e->target == ui_ProductTestRelaySwitch5)
  {
    if(lv_obj_has_state(ui_ProductTestRelaySwitch5, LV_STATE_CHECKED))
    {
      /*打开继电器*/
      i2c_mcu_relay_K5(1);
    }
    else
    {
      /*关闭继电器*/
      i2c_mcu_relay_K5(0);
    }
  }
}

void RegulateProductTest(lv_event_t * e)
{

}

void TurnProductTestBackground(lv_event_t * e)
{

}

void ProductTestScreen_Refresh(void)
{
  char text[50];
  if(lv_scr_act() == ui_ProductTestScreen)
  {
    /*当前温度刷新*/
    int fault = 0;
    float current_temp = i2c_mcu_get_ntc1_temp(&fault);
    if(fault == 0)
    {
      snprintf(text, sizeof(text), "当前温度：%.1f℃", current_temp);
      _ui_label_set_property(ui_ProductTestCurrentTemperatureLabel, _UI_LABEL_PROPERTY_TEXT, text);
    }
    else
    {
      _ui_label_set_property(ui_ProductTestCurrentTemperatureLabel, _UI_LABEL_PROPERTY_TEXT, "当前温度：- -");
    }

    /*时间刷新*/
    rtc_time_cache_t rtc_time;
    if(system_get_rtc_time(&rtc_time) == ESP_OK)
    {
      lv_snprintf(text, sizeof(text), "%04d-%02d-%02d %02d:%02d:%02d", 
      rtc_time.year, rtc_time.month, rtc_time.day,
      rtc_time.hour, rtc_time.min, rtc_time.sec);
      _ui_label_set_property(ui_ProductTestDateTimeLabel, _UI_LABEL_PROPERTY_TEXT, text);
    }
    /*星期刷新*/
    switch(rtc_time.week)
    {
      case 1:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期一");
        break;
      case 2:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期二");
        break;
      case 3:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期三");
        break;
      case 4:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期四");
        break;
      case 5:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期五");
        break;
      case 6:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期六");
        break;
      case 7:
        _ui_label_set_property(ui_ProductTestWeekLabel, _UI_LABEL_PROPERTY_TEXT, "星期日");
        break;
      default:
        break;
    }

    /*NTC温度刷新*/
    int fault2 = 0;
    static int fault2_count = 0;
    float NTC_temp = i2c_mcu_get_ntc2_temp(&fault2);
    if(fault2 == 0)
    {
      fault2_count = 0;
      snprintf(text, sizeof(text), "NTC温度：%.1f℃", NTC_temp);
      _ui_label_set_property(ui_ProductTestNtcTemperatureLabel, _UI_LABEL_PROPERTY_TEXT, text);
    }
    else
    {
      fault2_count++;
      if(fault2_count >= 3)
      {
        fault2_count = 0;
        _ui_label_set_property(ui_ProductTestNtcTemperatureLabel, _UI_LABEL_PROPERTY_TEXT, "NTC温度：- -");
      }
    }

    /*WIFI RSSI*/
    lv_snprintf(text, sizeof(text), "WIFI RSSI：%d", get_wifi_rssi_value());
    _ui_label_set_property(ui_ProductTestWifiRssiLabel, _UI_LABEL_PROPERTY_TEXT, text);

    /*程序版本*/
    lv_snprintf(text, sizeof(text), "软件版本：%s", "0.0.5");
    _ui_label_set_property(ui_ProductTestFirmwareVersionLabel, _UI_LABEL_PROPERTY_TEXT, text);
  }
}

void ProductTestTimer_Callback(lv_timer_t * timer)
{
  (*(uint8_t *)timer->user_data)++;
  
  if((*(uint8_t *)timer->user_data) >= 180)
  {
    /*测试时间到，自动退出生产测试界面*/
    (*(uint8_t *)timer->user_data) = 0;
    _ui_screen_change(&ui_MainScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_MainScreen_screen_init);
  }

  if(lv_scr_act() == ui_ProductTestScreen)
  {
    if(lv_obj_get_style_bg_color(ui_ProductTestPanel, LV_PART_MAIN | LV_STATE_DEFAULT).full == lv_color_hex(0xFF0000).full)
    {
      lv_obj_set_style_bg_color(ui_ProductTestPanel, lv_color_hex(0x00FF00), LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_text_color(ui_ProductTestPanelPromptLabel, lv_color_hex(0x0000FF), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
    else if(lv_obj_get_style_bg_color(ui_ProductTestPanel, LV_PART_MAIN | LV_STATE_DEFAULT).full == lv_color_hex(0x00FF00).full)
    {
      lv_obj_set_style_bg_color(ui_ProductTestPanel, lv_color_hex(0x0000FF), LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_text_color(ui_ProductTestPanelPromptLabel, lv_color_hex(0xFF0000), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
    else 
    {
      lv_obj_set_style_bg_color(ui_ProductTestPanel, lv_color_hex(0xFF0000), LV_PART_MAIN | LV_STATE_DEFAULT);
      lv_obj_set_style_text_color(ui_ProductTestPanelPromptLabel, lv_color_hex(0x00FF00), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
  }
}