#include <rtthread.h>
#include "flexible_button.h"
#include "bsp_button.h"

#include "bsp_led.h"
#include "bsp_beep.h"
#include "bsp_relay.h"
#include "bsp_emv.h"

#include "user_sys.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_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);
            // return rt_pin_read(BUTTON_0);
            break;

        default:
            RT_ASSERT(0);
    }

    return value;
}

#if 0
static uint8_t led_cnt = 1;
static uint8_t beep_cnt = 1;
static uint8_t relay_cnt = 1;
// static uint8_t emv_cnt = 0;
#endif

static void _CommonBtnEvtCb(void *arg)
{
    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_LONG_HOLD)
    {
        Send_Laser_Alarm_Event(kSelfCheckEvent);
    }
}

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_memset(&user_button[0], 0x0, sizeof(user_button));

    rt_pin_mode(BUTTON_0, 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
