#include "driver/gptimer.h"
#include "driver/gpio.h"
#include "log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "string.h"
#include "system_freertos_define.h"
#include "rtos_drv.h"
#include "ui_app.h"
#include "lcd_display_drv.h"
#include "i2c_mcu.h"
#include "esp_timer.h"

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

#define STACK_SIZE 1024*6

static const char *TAG = "ui_app";

StaticTask_t ui_taskBuffer;
StackType_t ui_xStack[STACK_SIZE];

static esp_timer_handle_t ui_taskHandle = NULL;


/**
 * @brief UI任务
 * @param arg 
 */
void UITask(void *arg)
{
    while(1)
    {
        if (lvgl_mutex_semaphore_handler != NULL)
        /* 获取互斥锁, 其他任务将阻塞直到互斥锁被释放 */
        xSemaphoreTake(lvgl_mutex_semaphore_handler, portMAX_DELAY);

        MainScreenRefresh();
        EngineerSettingInterfaceRefresh(1);
        user_setting_screen_refresh();
        WifiScreenRefresh();
        menu_screen_interface_refresh(GetProgram_option());
        programme_data_C_F();
        ProductTestScreen_Refresh();
        if (lvgl_mutex_semaphore_handler != NULL)
        /* 释放互斥锁, 以允许其他任务访问LVGL资源 */
        xSemaphoreGive(lvgl_mutex_semaphore_handler);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}




/*0:息屏， 1:亮屏*/
static bool breathing_plate_flag = 1;
void breathing_plate_flag_state_set(bool state)
{
    breathing_plate_flag = state;
}

bool breathing_plate_flag_state_get(void)
{
    return breathing_plate_flag;
}


/*息屏计数器*/
static uint8_t breathing_plate_counter = 0;

void breathing_plate_counter_set(uint8_t value)
{
    breathing_plate_counter = value;
}

void IRAM_ATTR rtos_timer_callback(void)
{
    if(lv_scr_act() == ui_ProductTestScreen)
    {
        return;
    }

    if (lvgl_mutex_semaphore_handler != NULL)
    /* 获取互斥锁, 其他任务将阻塞直到互斥锁被释放 */
    xSemaphoreTake(lvgl_mutex_semaphore_handler, portMAX_DELAY);

    breathing_plate_counter++;
    if(breathing_plate_counter >= 60)
    {
        if(lv_scr_act() != ui_MainScreen)
        {
            /*不在首页回到首页*/
            breathing_plate_counter = 0;
            _ui_screen_change(&ui_MainScreen, LV_SCR_LOAD_ANIM_NONE, 0, 50, &ui_MainScreen_screen_init);               
        }
        else
        {
            /*在首页隐藏弹窗*/
            _ui_flag_modify(ui_MainScreenChildLockPasswordBackPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
    }

    if(breathing_plate_counter >= GetScreenOffTime())
    {
        breathing_plate_counter = 0;
        if(breathing_plate_flag_state_get())
        {
            system_backlight_onoff(0);
            _ui_flag_modify(ui_MainScreenWeekUpPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);

            if(GetKey_Lock())
            {
                _ui_flag_modify(ui_MainScreenChildLockTouchPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
            }
            lcd_display_rgb_driver_sleep();
            /*息屏标志*/
            breathing_plate_flag_state_set(0);
        }
    }

    if (lvgl_mutex_semaphore_handler != NULL)
    /* 释放互斥锁, 以允许其他任务访问LVGL资源 */
    xSemaphoreGive(lvgl_mutex_semaphore_handler);
}


/*首页动画效果*/
void lv_timer_callback(void)
{
    main_screen_timer_callback();
}

/*进入产品测试定时器*/
void product_test_timer_callback(void)
{
    static uint16_t count = 0;
    count++;
    printf("count = %d\n", count);
    if(count >= 5*10)
    {
        count = 5*10;
        if(lv_scr_act() == ui_MainScreen)
        {
            if(!lv_obj_has_flag(ui_ProductTestTouchPanel, LV_OBJ_FLAG_HIDDEN))
            _ui_flag_modify(ui_ProductTestTouchPanel, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_ADD);
        }
    }
}

/**
 * @brief UI任务创建
 */
void UITaskCreat(void)
{
    if (ui_taskHandle == NULL)
    ui_taskHandle = xTaskCreateStatic(UITask, "UITask", STACK_SIZE, NULL, UI_TASK_PRIORITY, ui_xStack, &ui_taskBuffer);

    esp_timer_create_args_t timer_args = {
    .callback = &rtos_timer_callback,
    .arg = NULL,                      // 可选参数
    .name = "my_timer"
    };

    ESP_ERROR_CHECK(esp_timer_create(&timer_args, &ui_taskHandle));

     // 启动周期性定时器，每1000ms触发一次
    ESP_ERROR_CHECK(esp_timer_start_periodic(ui_taskHandle, 1000000));

    lv_timer_create(lv_timer_callback, 500, NULL);

    lv_timer_create(product_test_timer_callback, 200, NULL);
}



