#include "btn_mgr.h"

#include "rtthread.h"
#include "bf0_hal.h"
#include "drv_io.h"
#include "stdio.h"
#include "string.h"
#include <drv_gpio.h>
#include <stdbool.h>

#include "drv_touch.h"

#include "drv_flash.h"
#include "mem_section.h"

#include "drv_io.h"
#include "stdio.h"
#include "string.h"
#include <drv_gpio.h>
#include "stdlib.h"

#include "drv_btn.h"


#define BUTTON_MGR_PLAY_PAUSE_ON_OFF_PIN    (19)    /**< 开/关机键（长按），播放/暂停键（短按） */
#define BUTTON_MGR_NEXT_VOLUME_UP_PIN       (24)     /**< 音量增加键（长按），下一曲键（短按） */
#define BUTTON_MGR_PREV_VOLUME_DOWN_PIN     (16)     /**< 音量减少键（长按），上一曲键（短按） */
#define BUTTON_MGR_LED_CTRL_PIN             (13)     /**< LED 开/关机键（长按），LED 状态切换键（短按） */
#define BUTTON_MGR_RGB_CTRL_PIN             (14)     /**< RGB 开/关机键（长按），RGB 状态切换键（短按） */


static button_manager_t g_button_mgr;

/*
 * @brief: GPIO 按钮驱动对象    
 * @details: 该结构体定义了 GPIO 按钮驱动的对象，包含按钮驱动基类、GPIO 引脚号和 GPIO 激活电平。    
*/
typedef struct {
    btn_drv_interface_t base;        /**< 按钮驱动基类 */
    int32_t gpio_num;               /**< GPIO 引脚号 */
    uint8_t active_level;           /**< GPIO 激活电平 */
} gpio_button_object_t;


static uint8_t button_mgr_get_key_level(btn_drv_interface_t *button_driver)
{
    gpio_button_object_t *gpio_btn_obj = __containerof(button_driver, gpio_button_object_t, base);

    return rt_pin_read(gpio_btn_obj->gpio_num) == gpio_btn_obj->active_level ? 1 : 0;
}

void button_mgr_timer_timeout(void *args)
{
    btn_drv_event_callback(NULL);
}

/*
 * @brief: 初始化按钮管理器
 * @return: RT_EOK 成功，-RT_ERROR 失败 
*/
rt_err_t button_mgr_init(void)
{
    button_manager_t *mgr = &g_button_mgr;

    if (mgr->is_initlized) {
        rt_kprintf("func: %s, button manager is initialized.\r\n", __func__);
        return -RT_ERROR;
    }

    const btn_drv_params_t btn_cfg = {0};
    btn_drv_handle_t btn_handle = NULL;

    gpio_button_object_t *gpio_btn_obj_0 = (gpio_button_object_t *)calloc(1, sizeof(gpio_button_object_t));
    RT_ASSERT(gpio_btn_obj_0 != RT_NULL);
    gpio_btn_obj_0->active_level = BTN_DRV_LEVEL_INACTIVE;
    gpio_btn_obj_0->gpio_num = BUTTON_MGR_PLAY_PAUSE_ON_OFF_PIN;
    gpio_btn_obj_0->base.get_key_level = button_mgr_get_key_level;
    gpio_btn_obj_0->base.del = NULL;
    btn_drv_create(&btn_cfg, &gpio_btn_obj_0->base, &btn_handle);

    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_SINGLE_CLICK, button_mgr_short_press_play_pause);
    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_LONG_PRESS_START, button_mgr_long_press_entry_hibernate);

    gpio_button_object_t *gpio_btn_obj_1 = (gpio_button_object_t *)calloc(1, sizeof(gpio_button_object_t));
    RT_ASSERT(gpio_btn_obj_1 != RT_NULL);
    gpio_btn_obj_1->active_level = BTN_DRV_LEVEL_INACTIVE;
    gpio_btn_obj_1->gpio_num = BUTTON_MGR_NEXT_VOLUME_UP_PIN;
    gpio_btn_obj_1->base.get_key_level = button_mgr_get_key_level;
    gpio_btn_obj_1->base.del = NULL;
    btn_drv_create(&btn_cfg, &gpio_btn_obj_1->base, &btn_handle);

    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_SINGLE_CLICK, button_mgr_short_press_play_next);
    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_LONG_PRESS_HOLD, button_mgr_long_press_volume_up);

    gpio_button_object_t *gpio_btn_obj_2 = (gpio_button_object_t *)calloc(1, sizeof(gpio_button_object_t));
    RT_ASSERT(gpio_btn_obj_2 != RT_NULL);
    gpio_btn_obj_2->active_level = BTN_DRV_LEVEL_INACTIVE;
    gpio_btn_obj_2->gpio_num = BUTTON_MGR_PREV_VOLUME_DOWN_PIN;
    gpio_btn_obj_2->base.get_key_level = button_mgr_get_key_level;
    gpio_btn_obj_2->base.del = NULL;
    btn_drv_create(&btn_cfg, &gpio_btn_obj_2->base, &btn_handle);

    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_SINGLE_CLICK, button_mgr_short_press_play_prev);
    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_LONG_PRESS_HOLD, button_mgr_long_press_volume_down);

    gpio_button_object_t *gpio_btn_obj_3 = (gpio_button_object_t *)calloc(1, sizeof(gpio_button_object_t));
    RT_ASSERT(gpio_btn_obj_3 != RT_NULL);
    gpio_btn_obj_3->active_level = BTN_DRV_LEVEL_INACTIVE;
    gpio_btn_obj_3->gpio_num = BUTTON_MGR_LED_CTRL_PIN;
    gpio_btn_obj_3->base.get_key_level = button_mgr_get_key_level;
    gpio_btn_obj_3->base.del = NULL;
    btn_drv_create(&btn_cfg, &gpio_btn_obj_3->base, &btn_handle);

    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_SINGLE_CLICK, button_mgr_short_press_led_change);
    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_LONG_PRESS_START, button_mgr_long_press_led_on_off);

    gpio_button_object_t *gpio_btn_obj_4 = (gpio_button_object_t *)calloc(1, sizeof(gpio_button_object_t));
    RT_ASSERT(gpio_btn_obj_3 != RT_NULL);
    gpio_btn_obj_4->active_level = BTN_DRV_LEVEL_INACTIVE;
    gpio_btn_obj_4->gpio_num = BUTTON_MGR_RGB_CTRL_PIN;
    gpio_btn_obj_4->base.get_key_level = button_mgr_get_key_level;
    gpio_btn_obj_4->base.del = NULL;
    btn_drv_create(&btn_cfg, &gpio_btn_obj_4->base, &btn_handle);

    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_SINGLE_CLICK, button_mgr_short_press_rgb_change);
    btn_drv_event_register(btn_handle, BTN_DRV_EVENT_LONG_PRESS_START, button_mgr_long_press_rgb_on_off);


    mgr->timer = rt_timer_create("button_mgr_timer", 
                                button_mgr_timer_timeout, 
                                NULL, 
                                rt_tick_from_millisecond(10), 
                                RT_TIMER_FLAG_PERIODIC);
    RT_ASSERT(mgr->timer != RT_NULL);

    rt_timer_start(mgr->timer);

    mgr->is_initlized = true;

    rt_kprintf("func: %s, button manager init success.\r\n", __func__);

    return RT_EOK;
}


/**
 * @brief 按钮管理器短按播放下一首
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_short_press_play_next(void *args)
{
    //bt_mgr_avrcp_next();
    rt_kprintf("func: %s, short press play next.\r\n", __func__);
}

/**
 * @brief 按钮管理器长按音量增加
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_long_press_volume_up(void *args)
{
    //bt_mgr_avrcp_volume_up();
    rt_kprintf("func: %s, long press volume up.\r\n", __func__);
}

/**
 * @brief 按钮管理器短按播放上一首
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_short_press_play_prev(void *args)
{
    //bt_mgr_avrcp_prev();
    rt_kprintf("func: %s, short press play prev.\r\n", __func__);
}

/**
 * @brief 按钮管理器长按音量减少
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_long_press_volume_down(void *args)
{
    //bt_mgr_avrcp_volume_down();
    rt_kprintf("func: %s, long press volume down.\r\n", __func__);
}

/**
 * @brief 按钮管理器短按切换RGB颜色
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_short_press_rgb_change(void *args)
{
    // if (bt_mgr_is_connected()) {
    //     bt_interface_close_bt();
    //     rt_kprintf("func: %s, request close bt driver.\r\n", __func__);
    // } else {
    //     rt_kprintf("func: %s, bt not connected, skip close.\r\n", __func__);
    // }
    rt_kprintf("func: %s, short press rgb change.\r\n", __func__);
}

/**
 * @brief 按钮管理器长按RGB开关
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_long_press_rgb_on_off(void *args)
{
    rt_kprintf("func: %s, long press rgb on/off.\r\n", __func__);
}

/**
 * @brief 按钮管理器短按切换LED颜色
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_short_press_led_change(void *args)
{
   // power_mgr_send_msg(POWER_MGR_POWER_ON_EVENT_LONG_PRESS, NULL);

    //app_mgr_send_msg(APP_MGR_MSG_TYPE_LED, APP_MGR_EVENT_LED_ADD, NULL);
    rt_kprintf("func: %s, short press led change.\r\n", __func__);
}

/**
 * @brief 按钮管理器长按LED开关
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_long_press_led_on_off(void *args)
{
    //app_mgr_send_msg(APP_MGR_MSG_TYPE_LED, APP_MGR_EVENT_LED_ONOFF, NULL);
    rt_kprintf("func: %s, long press led on/off.\r\n", __func__);
}

/**
 * @brief 按钮管理器短按播放/暂停
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/  
static void button_mgr_short_press_play_pause(void *args)
{
    //power_mgr_send_msg(POWER_MGR_POWER_OFF_EVENT_LONG_PRESS, NULL);
    rt_kprintf("func: %s, short press play/pause.\r\n", __func__);
}

/*
 * @brief 按钮管理器长按进入休眠状态
 * @param button_mgr 按钮管理器实例指针
 * @param args 额外参数（未使用）
*/
static void button_mgr_long_press_entry_hibernate(void *args)
{
    //power_mgr_send_msg(POWER_MGR_POWER_OFF_EVENT_LONG_PRESS, NULL);

    rt_kprintf("func: %s, long press entry hibernate.\r\n", __func__);
}








