#include <rtthread.h>
#include "flexible_button.h"
#include "bsp_button.h"
#include "bsp_flash.h"
#include "bsp_led.h"
#include "bsp_beep.h"
#include "bsp_bt.h"
#include "user_sys.h"
#include "bsp_mq.h"

#define LOG_TAG "bsp_button"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>

#ifdef BUTTON_USE_THREAD
ALIGN(RT_ALIGN_SIZE)
static char button_thread_stack[BUTTON_THREAD_STACK_SIZE];
static struct rt_thread button_thread;
#else
static rt_timer_t button_timer;
#endif // !BUTTON_USE_THREAD

typedef enum
{
    USER_BUTTON_0 = 0,
    USER_BUTTON_1,
    USER_BUTTON_MAX
} user_button_t;

static flex_button_t user_button[USER_BUTTON_MAX];

static uint8_t _CommonBtnRead(void *arg)
{
    uint8_t value      = 0;
    flex_button_t *btn = (flex_button_t *)arg;

    switch (btn->id)
    {
        case USER_BUTTON_0:
            value = rt_pin_read(BUTTON_0);
            break;
        case USER_BUTTON_1:
            value = rt_pin_read(BUTTON_1);
            break;
        default:
            RT_ASSERT(0);
    }

    return value;
}

static void _CommonBtnEvtCb(void *arg)
{
    // static rt_uint8_t button_cnt = 0;

    if (flex_button_event_read(&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_DOUBLE_CLICK)
    {
        if ( SysControl.status > kPreheatingEvent)
        {
            Send_Laser_Alarm_Event(kSelfCheckEvent);
        }
    }
    else if (flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_CLICK)
    {
        if (SysControl.status == kAlarmEvent)
        {
            Send_Laser_Alarm_Event(KMuteEvent);
        }
    }
    // else if ((flex_button_event_read(&user_button[USER_BUTTON_0]) == FLEX_BTN_PRESS_REPEAT_CLICK) && (flex_button_event_read(&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_LONG_HOLD))
     else if (flex_button_event_read(&user_button[USER_BUTTON_1]) == FLEX_BTN_PRESS_LONG_HOLD)
    {
        LOG_I("本地清除阀门信息");
        rt_uint8_t mac_addr[6] = {0};
        if(Flash_Get_Valve_Num() == 0)
        {
            LOG_I("本地没有阀门信息");
        }
        else
        {
            //清空本地阀门信息
            if (Flash_Set_Mac_Addr(mac_addr, 1) != RESET)
            {
                LOG_E("本地阀门信息清空完成");
                Flash_Set_Valve_Num(0);
            }
        }
        Bt_Valve_Handler(kValveCmdRemAll, RT_NULL, RT_NULL);
        if (rt_sem_take(&bt_rem_sem, 10000) == RT_EOK) // 如果没刷掉数据，则黄灯会亮一秒，如果刷成功了，则红灯闪一下黄灯不会亮
        {
            LED_OFF(g);
            LED_OFF(r);
            LED_OFF(y);
            LED_ON(r);
            rt_thread_mdelay(100);
            LED_OFF(r);
            LOG_I("本地清除阀门信息成功");
        }
    }
}

static void _BUTTON_Process(void *param)
{
#ifdef BUTTON_USE_THREAD
    LOG_D("BSP_BUTTON_thread");
    while (1)
    {
        flex_button_scan();
        rt_thread_mdelay(20);
    }
#else
    flex_button_scan();
    // LOG_D("BSP_BUTTON_timer");
#endif // !BUTTON_USE_THREAD
}

int BSP_BUTTON_Init(void)
{
    rt_uint8_t ret = 0;
    rt_memset(&user_button[0], 0x0, sizeof(user_button));

    rt_pin_mode(BUTTON_0, PIN_MODE_INPUT_PULLUP); /* set KEY pin mode to input */
    rt_pin_mode(BUTTON_1, PIN_MODE_INPUT_PULLUP); /* set KEY pin mode to input */

    for (uint8_t i = 0; i < USER_BUTTON_MAX; i++)
    {
        user_button[i].id                     = i;
        user_button[i].usr_button_read        = _CommonBtnRead;
        user_button[i].cb                     = _CommonBtnEvtCb;
        user_button[i].pressed_logic_level    = PIN_LOW;
        user_button[i].short_press_start_tick = FLEX_MS_TO_SCAN_CNT(1000);
        user_button[i].long_press_start_tick  = FLEX_MS_TO_SCAN_CNT(3000);
        user_button[i].long_hold_start_tick   = FLEX_MS_TO_SCAN_CNT(5000);

        flex_button_register(&user_button[i]);
    }
#ifdef BUTTON_USE_THREAD
    ret = rt_thread_init(&button_thread,
                         "button_thread",
                         _BUTTON_Process,
                         RT_NULL,
                         &button_thread_stack[0],
                         sizeof(button_thread_stack),
                         BUTTON_THREAD_PRIORITY,
                         BUTTON_THREAD_TICKS);

    if (ret == RT_EOK)
    {
        rt_thread_startup(&button_thread);
        return RT_EOK;
    }
    else
    {
        LOG_E("button_thread create failed");
        return -RT_ERROR;
    }
#else
    button_timer = rt_timer_create("button_timer", _BUTTON_Process,
                                   RT_NULL, 20,
                                   RT_TIMER_FLAG_PERIODIC);
    if (button_timer != RT_NULL)
    {
        rt_timer_start(button_timer);
    }
    else
    {
        LOG_E("create button_timer fail");
        return -RT_ERROR;
    }
#endif // !BUTTON_USE_THREAD
    LOG_I("BSP_BUTTON_Init!");

    return -RT_ERROR;
}
#ifdef FINSH_USING_MSH
// INIT_DEVICE_EXPORT(BSP_BUTTON_Init);
#endif
