#include <stdint.h>

//#include "os_task.h"
//#include "os_msg_q.h"

//#include "co_printf.h"
#include "user_task.h"
//#include "app_at.h"
#include "button.h"
//#include "driver_system.h"
//#include "driver_gpio.h"
#include "lvgl.h"
#include "hal_key_handle.h"


static uint16_t user_task_id;

extern lv_obj_t * get_root_win(void);
extern void hal_keypad_msg_handle(uint8_t button_index, uint8_t button_type);


static void handle_key_msg_input(uint8_t *param)
{
	//key input
	struct button_msg_t *button_msg;
	const char *button_type_str[] = {
                                                "BUTTON_PRESSED",
                                                "BUTTON_RELEASED",
                                                "BUTTON_SHORT_PRESSED",
                                                "BUTTON_MULTI_PRESSED",
                                                "BUTTON_LONG_PRESSED",
                                                "BUTTON_LONG_PRESSING",
                                                "BUTTON_LONG_RELEASED",
                                                "BUTTON_LONG_LONG_PRESSED",
                                                "BUTTON_LONG_LONG_RELEASED",
                                                "BUTTON_COMB_PRESSED",
                                                "BUTTON_COMB_RELEASED",
                                                "BUTTON_COMB_SHORT_PRESSED",
                                                "BUTTON_COMB_LONG_PRESSED",
                                                "BUTTON_COMB_LONG_PRESSING",
                                                "BUTTON_COMB_LONG_RELEASED",
                                                "BUTTON_COMB_LONG_LONG_PRESSED",
                                                "BUTTON_COMB_LONG_LONG_RELEASED",
                                            };

	button_msg = (struct button_msg_t *)param;

	co_printf("KEY 0x%08x, TYPE %s.\r\n", button_msg->button_index, button_type_str[button_msg->button_type]);

	switch(button_msg->button_index)
	{
#ifdef BL01_V10
				case GPIO_PA3:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key1 Pressed %d\r\n",button_msg->button_type);
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA4:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key2 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA5:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key3 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA6:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key4 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA7:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key5 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA3|GPIO_PA4:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key1/2 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA3|GPIO_PA5:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key1/3 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA3|GPIO_PA6:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key1/4 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA3|GPIO_PA7:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key1/5 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA4|GPIO_PA5:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key2/3 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA4|GPIO_PA6:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key2/4 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA4|GPIO_PA7:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key2/5 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA5|GPIO_PA6:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key3/4 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PA5|GPIO_PA7:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key3/5 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;

				case GPIO_PA6|GPIO_PA7:
					switch(button_msg->button_type)
					{
						case BUTTON_COMB_SHORT_PRESSED:
						case BUTTON_COMB_LONG_PRESSED:
							co_printf("key4/5 COMB Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
#else
				case GPIO_PC6:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key1 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PC7:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key2 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
				case GPIO_PD4:
					switch(button_msg->button_type)
					{
						case BUTTON_SHORT_PRESSED:
						case BUTTON_MULTI_PRESSED:
						case BUTTON_LONG_PRESSED:
							co_printf("key3 Pressed\r\n");
							hal_keypad_msg_handle(button_msg->button_index,button_msg->button_type);
							break;
					}
					break;
#endif
	}
}

static int user_task_func(os_event_t *param)
{
	co_printf("user_task_func %d\r\n", param->event_id);
    switch(param->event_id)
    {
        case USER_EVT_AT_COMMAND:
            app_at_cmd_recv_handler(param->param, param->param_len);
            break;
        case USER_EVT_BUTTON:
            handle_key_msg_input(param->param);
		break;
		default:
			break;
    }
    return EVT_CONSUMED;
}

void user_task_init(void)
{
	co_printf("user_task_init %d\r\n", user_task_id);
	user_task_id = os_task_create(user_task_func);
}


uint16_t get_user_task_id(void)
{

	co_printf("get_user_task_id %d\r\n", user_task_id);
	return user_task_id;
}

