#include "ui_himalaya_api.h"
#include "ui_himalaya_data.h"
#include "ui/ui_api.h"

#define LOG_TAG_CONST       UI
#define LOG_TAG     		"[HIMALAYA_UI]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"

static void h_msg_post(enum H_MSG status)
{
	UI_MSG_POST("H_msg_status:H_status=%4",status);
}

void h_post_ui_msg(int msg)
{
	h_msg_post(msg);
}


void h_msg_handler(const char *type,u32 arg)
{
	int play_status=1;
	if(type && (!strcmp(type,"H_status")))
	{
		switch(arg)
		{
			case MSG_HOMEPAGE_LIST:
				h_set_album_list_grid(__this->homepage.listen_total);
				ui_himalaya_set_album_title();
				h_ui_layout_switch(ALBUM_H);
				break;
			case MSG_TRACK_LIST:
				h_set_track_list_grid(__this->source_list.source_total);
				ui_himalaya_set_track_title();
				__this->ui.track_grid_row=__this->source_list.source_total;
				h_ui_layout_switch(TRACK_LIST_H);
				break;
			case MSG_TURN_PLAYER:
				h_net_music_task_create();
				ui_text_set_textu_by_id(H_P_TITLE,__this->ui.player_title,strlen(__this->ui.player_title),FONT_DEFAULT|FONT_SHOW_MULTI_LINE);
				__this->ui.play_status=MUSIC_PLAY;
				ui_pic_show_image_by_id(H_P_STATUS,__this->ui.play_status);
				os_taskq_post_msg("HIMALAYA_NET_MUSIC",2,0,__this->ui.url);
				h_ui_layout_switch(PLAYER_H);
				break;
			case MSG_USR_LOGIN:
				h_set_user_info();
				h_ui_layout_switch(ACCOUNT_INFO_H);
				break;
			case MSG_USR_UNLOGIN:
				qr_code_event_init();
				qr_code_set_title();
				h_ui_layout_switch(QRCODE_H);
				break;
			case MSG_USR_PERSONAL_CENTER:
				break;
			case MSG_USR_HISTORY_LIST:
				if(__this->history_list.source_total>=1)
				{
					ui_hide(H_HISTORY_EMPTY);
					h_set_history_list_grid(__this->history_list.source_total);
					ui_himalaya_set_history_title();
				}
				else
				{
					ui_hide(BASEFORM_2105);
					ui_show(H_HISTORY_EMPTY);
				}
				h_ui_layout_switch(HISTORY_H);
				break;
			case MSG_USR_ALBUM_LIST:
				h_set_track_list_grid(__this->favorite_list.source_total);
				h_ui_layout_switch(TRACK_LIST_H);
				__this->ui.track_grid_row=__this->favorite_list.source_total;
				ui_himalaya_set_subscribe_title(); 
				break;
			case MSG_REFRESH_TRACK:
				himalaya_ui_set_track_level(TRACK_LEVEL_II);
				h_set_track_list_grid(__this->track_list.source_total);
				ui_himalaya_refresh_track_title();
				__this->ui.track_grid_row=__this->track_list.source_total;
				h_ui_layout_switch(TRACK_LIST_H);
				break;
			case MSG_MUSIC_PAUSE:
				ui_pic_show_image_by_id(H_P_STATUS,MUSIC_PAUSE);
				break;
		}
	}
}






























void himalaya_info_memory_malloc()
{
	if(__this)return;
	__this=(himalaya_info *)malloc(INFO_SIZE);
}

void himalaya_info_memory_release()
{
	if(!__this)return;
	free(__this);
	__this=NULL;
}

void h_ui_quit_himalaya(void)
{
	UI_WINDOW_BACK_SHOW(PAGE_7);
	UI_WINDOW_BACK_CLEAN();
}

static int get_net_dhcp_status()
{
	if(!net_get_dhcp_flag())
	{
		h_ui_layout_switch(LAYOUT_QUIT_H);
		return 0;
	}
	return 1;
}

void h_ui_initial_animation(void)
{
	if(net_get_dhcp_flag())
	{
		printf(">>> %%s %%d %%s\n", __func__, __LINE__, __FILE__);
		h_ui_layout_switch(PRIVARY_H);
	}
	else
	{
		printf(">>> %%s %%d %%s\n", __func__, __LINE__, __FILE__);
		h_ui_layout_switch(LAYOUT_QUIT_H);
	}
}

void ask_himalaya_homepage_info()
{
	if(!get_net_dhcp_status())return;
	xy_http_get_homepage_info();
}

void ask_himalaya_popup_screen_info()
{
	if(!get_net_dhcp_status())return;
	xy_http_get_popup_screen_info();
}

void ask_himalaya_account_login_info(void)
{
	if(!get_net_dhcp_status())return;
	if(xy_common_user_is_login())
	{
		xy_api_http_user_vip_info();
	}
	else if(!xy_common_user_is_login())
	{
		xy_http_get_login_qrcode();
	}
}

void ask_himalaya_clear_info_personal_center(void)
{
	if(!get_net_dhcp_status())return;
	xy_api_http_user_logout(1);
	xy_common_clear_user_info();
	h_ui_layout_switch(PERSONAL_H);
	/* h_usr_info_destory(); */
}

void ask_himalaya_get_user_history_list(void)
{
	if(xy_common_user_is_login())
	{
		xy_http_get_user_history_list();
	}
	else if(!xy_common_user_is_login())
	{
		xy_http_get_login_qrcode();
	}
}

void ask_himalaya_wait_user_listen_list(void)
{
	if(xy_common_user_is_login())
	{
		xy_http_get_user_subscribe_list();
	}
	else if(!xy_common_user_is_login())
	{
		xy_http_get_login_qrcode();
	}
}

void himalaya_ui_cmd_server(u8 cmd)
{
	switch(cmd)
	{
		case CMD_INIT:
			himalaya_info_memory_malloc();
			himalaya_qrcode_init();
			himalaya_usr_info_malloc();
			break;
		case CMD_RELEASE:
			h_net_music_task_quit();
			himalaya_usr_info_destory();
			himalaya_qrcode_destory();
			himalaya_info_memory_release();
			break;
		case CMD_NET_CHECK:
			h_ui_initial_animation();
			break;
		case CMD_GET_HOMEPAGE:
			ask_himalaya_homepage_info();//获取首页播单数据
			break;
		case CMD_GET_PRIVACY:
			ask_himalaya_popup_screen_info();//获取隐私二维码数据
			break;
		case CMD_CLEAR:
			ask_himalaya_clear_info_personal_center();//退出登录清理用户登录数据
			break;
		case CMD_HOMEPAGE_ALBUM_BY_LISTENID:
			himalaya_ui_set_track_level(TRACK_LEVEL_I);
			himalaya_ui_set_track_mode(TRACK_HOMEPAGE);
			xy_http_get_track_list_by_listen_id(__this->homepage.listen_array[__this->homepage.sel_item].listen_id);
			break;
		case CMD_USR_ACCOUNT:
			ask_himalaya_account_login_info();// 获取用户登录二维码数据
			break;
		case CMD_USR_HISTORY:
			himalaya_ui_reset_history_info();
			ask_himalaya_get_user_history_list();
			break;
		case CMD_USR_SUBSCRIBE:
			himalaya_ui_reset_subscribe_info();
			himalaya_ui_set_track_level(TRACK_LEVEL_I);
			himalaya_ui_set_track_mode(TRACK_PERSONAL);
			ask_himalaya_wait_user_listen_list();
			break;
		case CMD_HISTORY_TRACK_BY_LISTENID:
			himalaya_ui_set_track_level(TRACK_LEVEL_II);
			himalaya_ui_set_track_mode(TRACK_HISTORY);
			xy_http_get_track_list_by_album_id(__this->history_list.source_array[__this->history_list.sel_item].album_id);
			break;
	}
}

void himalaya_homepage_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * id)
{
	__this->homepage.listen_total=total_num;
	__this->homepage.listen_array[index].pay_type=pay_type;
	__this->homepage.listen_array[index].index=index;
	strncpy(__this->homepage.listen_array[index].listen_name,title,strlen(title));
	__this->homepage.listen_array[index].listen_name[strlen(title)]='\0';
	strncpy(__this->homepage.listen_array[index].listen_id,id,strlen(id));
	__this->homepage.listen_array[index].listen_id[strlen(id)]='\0';
	log_info(">>>>>>>func %s line %d total_num %d pay_type %d index %d ,title %s id %s \n",__func__,__LINE__,__this->homepage.listen_total,__this->homepage.listen_array[index].pay_type,__this->homepage.listen_array[index].index,__this->homepage.listen_array[index].listen_name,__this->homepage.listen_array[index].listen_id);
}

static void ui_himalaya_set_album_title()
{
	int id[]={H_ALBUM_TITLE0,H_ALBUM_TITLE1,H_ALBUM_TITLE2};	
	for(int i=0;i<(sizeof(id)/sizeof(id[0]));i++)
	{
		ui_text_set_textu_by_id(id[i],__this->homepage.listen_array[i].listen_name,strlen(__this->homepage.listen_array[i].listen_name),FONT_DEFAULT|FONT_SHOW_SCROLL);
	}
}

void himalaya_get_sys_time(struct sys_time *time)
{
    void *fd = dev_open("rtc", NULL);
    if (!fd) {
        memset(time, 0, sizeof(*time));
        return;
    }
    dev_ioctl(fd, IOCTL_GET_SYS_TIME, (u32)time);
    dev_close(fd);
}


//[listen_id]
void himalaya_homepage_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id)
{
	__this->source_list.source_total=total_num+1;
	__this->source_list.source_array[index].pay_type=pay_type;
	strncpy(__this->source_list.source_array[index].title,title,strlen(title));
	__this->source_list.source_array[index].title[strlen(title)]='\0';
	strncpy(__this->source_list.source_array[index].album_id,album_id,strlen(album_id));
	__this->source_list.source_array[index].album_id[strlen(album_id)]='\0';
	strncpy(__this->source_list.source_array[index].track_id,track_id,strlen(track_id));
	__this->source_list.source_array[index].track_id[strlen(track_id)]='\0';
	log_info(">>>>>func %s line %d total_num %d pay_type %d index %d title %s album_id %s track_id %s \n",__func__,__LINE__,__this->source_list.source_total,__this->source_list.source_array[index].pay_type,index,__this->source_list.source_array[index].title,__this->source_list.source_array[index].album_id,__this->source_list.source_array[index].track_id);
}

//[album_id]
void himalaya_track_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id)
{
	__this->track_list.source_total=total_num;
	__this->track_list.source_array[index].pay_type=pay_type;
	memset(__this->track_list.source_array[index].title,0,MAX_NAME_LEN);
	strncpy(__this->track_list.source_array[index].title,title,strlen(title));
	log_info(">>>>>>func %s line %d title %s \n",__func__,__LINE__,__this->track_list.source_array[index].title);
	__this->track_list.source_array[index].title[strlen(title)]='\0';
	memset(__this->track_list.source_array[index].album_id,0,MAX_ID_LEN);
	strncpy(__this->track_list.source_array[index].album_id,album_id,strlen(album_id));
	__this->track_list.source_array[index].album_id[strlen(album_id)]='\0';
	memset(__this->track_list.source_array[index].track_id,0,MAX_ID_LEN);
	strncpy(__this->track_list.source_array[index].track_id,track_id,strlen(track_id));
	__this->track_list.source_array[index].track_id[strlen(track_id)]='\0';
	log_info(">>>>>func %s line %d total_num %d pay_type %d index %d title %s album_id %s track_id %s \n",__func__,__LINE__,__this->track_list.source_total,__this->track_list.source_array[index].pay_type,index,__this->track_list.source_array[index].title,__this->track_list.source_array[index].album_id,__this->track_list.source_array[index].track_id);
}

void himalaya_ui_reset_subscribe_info()
{
	memset(&(__this->favorite_list),0,sizeof(source_list_info));
} 
//[favoriye_list]
void himalaya_favorite_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id)
{
	__this->favorite_list.source_total=total_num+1;
	__this->favorite_list.source_array[index].pay_type=pay_type;
	strncpy(__this->favorite_list.source_array[index].title,title,strlen(title));
	__this->favorite_list.source_array[index].title[strlen(title)]='\0';
	strncpy(__this->favorite_list.source_array[index].album_id,album_id,strlen(album_id));
	__this->favorite_list.source_array[index].album_id[strlen(album_id)]='\0';
	log_info(">>>>>func %s line %d total_num %d pay_type %d index %d title %s album_id %s\n",__func__,__LINE__,__this->favorite_list.source_total,__this->favorite_list.source_array[index].pay_type,index,__this->favorite_list.source_array[index].title,__this->favorite_list.source_array[index].album_id);
}

void himalaya_ui_reset_history_info()
{
	memset(&(__this->history_list),0,sizeof(source_list_info));
}
//[history_list]
void himalaya_history_list_info_cpy(u8 total_num,u8 pay_type,u8 index,u8 *title,u8 * album_id,u8 *track_id)
{
	__this->history_list.source_total=total_num;
	__this->history_list.source_array[index].pay_type=pay_type;
	strncpy(__this->history_list.source_array[index].title,title,strlen(title));
	__this->history_list.source_array[index].title[strlen(title)]='\0';
	strncpy(__this->history_list.source_array[index].album_id,album_id,strlen(album_id));
	__this->history_list.source_array[index].album_id[strlen(album_id)]='\0';
	strncpy(__this->history_list.source_array[index].track_id,track_id,strlen(track_id));
	__this->history_list.source_array[index].track_id[strlen(track_id)]='\0';
	log_info(">>>>>func %s line %d total_num %d pay_type %d index %d title %s album_id %s track_id %s \n",__func__,__LINE__,__this->history_list.source_total,__this->history_list.source_array[index].pay_type,index,__this->history_list.source_array[index].title,__this->history_list.source_array[index].album_id,__this->history_list.source_array[index].track_id);
}

static void ui_himalaya_set_history_title()
{
	int id[]={H_HISTORY_TITLE0,H_HISTORY_TITLE1,H_HISTORY_TITLE2};	
	for(int i=0;i<(sizeof(id)/sizeof(id[0]));i++)
	{
		ui_text_set_textu_by_id(id[i],__this->history_list.source_array[i].title,strlen(__this->history_list.source_array[i].title),FONT_DEFAULT|FONT_SHOW_MULTI_LINE);
	}
}

static void h_set_user_info()
{
	ui_text_set_textu_by_id(H_ACCOUNT_NAME,__usr_name,strlen(__usr_name),FONT_DEFAULT);
	/* ui_text_show_index_by_id(H_ACCOUNT_VIP_INFO,ui.usr.vip); */
}

void himalaya_usr_info_malloc()
{
	if(__usr_name)return;
	__usr_name=malloc(USR_NAME);
}

void himalaya_usr_info_destory()
{
	if(!__usr_name)return;
	free(__usr_name);
	__usr_name=NULL;
}

void h_cpy_usr_info(u8 * name)
{
	memset(__usr_name,0,USR_NAME);
	strncpy(__usr_name,name,USR_NAME);
	log_info(">>>>>name %s \n",__usr_name);
}

void h_cpy_usr_vip(int vip)
{
	/* ui.usr.vip=0; */
	/* ui.usr.vip=vip; */
}

void himalaya_http_audio_url(u8 *url)
{
	memset(__this->ui.url,0,sizeof(__this->ui.url));
	strncpy(__this->ui.url,url,512);
}

void h_cpy_qr_mess(char * buf,char * name)
{
	strncpy((__qr->mess),buf,QRCODE_SIZE);
	strncpy((__qr->title),name,QRCODE_NAME);
}

int h_ui_voice_to_percent(s8 volume)
{
	return volume * 100 / get_max_sys_vol();
}


static void ui_himalaya_set_track_title()
{
	int id[]={H_TRACK_TITLE0,H_TRACK_TITLE1,H_TRACK_TITLE2};	
	int pic[]={H_TRACK_PAY0,H_TRACK_PAY1,H_TRACK_PAY2};
	for(int i=0;i<(sizeof(id)/sizeof(id[0]));i++)
	{
		ui_text_set_textu_by_id(id[i],__this->source_list.source_array[i].title,strlen(__this->source_list.source_array[i].title),FONT_DEFAULT|FONT_SHOW_MULTI_LINE);
	}
	for(int i=0;i<(sizeof(pic)/sizeof(pic[0]));i++)
	{
		ui_pic_show_image_by_id(pic[i], __this->source_list.source_array[i].pay_type);
	}
	return;
}

static void ui_himalaya_set_subscribe_title()
{
	int id[]={H_TRACK_TITLE0,H_TRACK_TITLE1,H_TRACK_TITLE2};	
	int pic[]={H_TRACK_PAY0,H_TRACK_PAY1,H_TRACK_PAY2};
	for(int i=0;i<(sizeof(id)/sizeof(id[0]));i++)
	{
		log_info(">>>>>>>381 title  %s \n",__this->favorite_list.source_array[i].title);
		ui_text_set_textu_by_id(id[i],__this->favorite_list.source_array[i].title,strlen(__this->favorite_list.source_array[i].title),FONT_DEFAULT|FONT_SHOW_MULTI_LINE);
	}
	for(int i=0;i<(sizeof(pic)/sizeof(pic[0]));i++)
	{
		ui_pic_show_image_by_id(pic[i], __this->favorite_list.source_array[i].pay_type);
	}
	return;
}

static void ui_himalaya_refresh_track_title()
{
	int id[]={H_TRACK_TITLE0,H_TRACK_TITLE1,H_TRACK_TITLE2};	
	int pic[]={H_TRACK_PAY0,H_TRACK_PAY1,H_TRACK_PAY2};
	for(int i=0;i<(sizeof(id)/sizeof(id[0]));i++)
	{
		ui_text_set_textu_by_id(id[i],__this->track_list.source_array[i].title,strlen(__this->source_list.source_array[i].title),FONT_DEFAULT|FONT_SHOW_MULTI_LINE);
	}
	for(int i=0;i<(sizeof(pic)/sizeof(pic[0]));i++)
	{
		ui_pic_show_image_by_id(pic[i], __this->source_list.source_array[i].pay_type);
	}
	return;
}

void himalaya_ui_set_track_mode(u8 mode)
{
	__this->ui.track_page_mode=mode;
}

int himalaya_ui_return_track_mode()
{
	return __this->ui.track_page_mode;
}

void himlaya_ui_reset_track_mode()
{
	__this->ui.track_page_mode=TRACK_RESET;
}

void himalaya_ui_set_track_level(u8 level)
{
	__this->ui.track_page_level=level;
}

int himalaya_ui_return_track_level()
{
	return __this->ui.track_page_level;
}

void himalaya_ui_reset_track_level()
{
	__this->ui.track_page_level=TRACK_LEVEL;
}

void himalaya_ui_refresh_by_listen_id(u8 num)
{
	if(himalaya_ui_return_track_level()==TRACK_LEVEL_I)
	{
		switch(__this->ui.track_page_mode)
		{
			case TRACK_HOMEPAGE:
				__this->source_list.sel_item=num;
				himalaya_ui_set_track_level(TRACK_LEVEL_II);
				xy_http_get_track_list_by_album_id(__this->source_list.source_array[num].album_id);//自己更新一遍数据
				break;
			case TRACK_PERSONAL:
				__this->favorite_list.sel_item=num;
				himalaya_ui_set_track_level(TRACK_LEVEL_II);
				xy_http_get_track_list_by_album_id(__this->favorite_list.source_array[num].album_id);//自己更新一遍数据
				break;
		}
	}
	else if(himalaya_ui_return_track_level()==TRACK_LEVEL_II)
	{
		switch(__this->ui.track_page_mode)
		{
			case TRACK_HOMEPAGE:
			case TRACK_PERSONAL:
			case TRACK_HISTORY:
				h_net_get_play_track_item(num);
				h_net_music_task_quit();
				xy_http_get_play_url_by_track_id(__this->track_list.source_array[num].track_id,__this->track_list.source_array[num].pay_type,__this->track_list.source_array[num].album_id,__this->track_list.source_array[num].pay_type);
				memset(__this->ui.player_title,0,128);
				strncpy(__this->ui.player_title,__this->track_list.source_array[num].title,strlen(__this->track_list.source_array[num].title));
				log_info(">>>>>func %s line %d title %s \n",__func__,__LINE__,__this->ui.player_title);
				break;
		}

	}
}


void himalaya_ui_track_page_return()
{
	if(himalaya_ui_return_track_level()==TRACK_LEVEL_I)
	{
		switch(__this->ui.track_page_mode)
		{
			case TRACK_HOMEPAGE:
				himalaya_ui_cmd_server(CMD_GET_HOMEPAGE);
				break;
			case TRACK_PERSONAL:
				h_ui_layout_switch(PERSONAL_H);//返回个人中心
				break;
		}
	}
	else if(himalaya_ui_return_track_level()==TRACK_LEVEL_II)
	{
		switch(__this->ui.track_page_mode)
		{
			case TRACK_HOMEPAGE:
				himalaya_ui_set_track_level(TRACK_LEVEL_I);
				himalaya_ui_cmd_server(CMD_HOMEPAGE_ALBUM_BY_LISTENID);
				break;
			case TRACK_PERSONAL:
				himalaya_ui_set_track_level(TRACK_LEVEL_I);
				himalaya_ui_cmd_server(CMD_USR_SUBSCRIBE);
				break;
			case TRACK_HISTORY:
				himalaya_ui_set_track_level(TRACK_LEVEL_I);
				himalaya_ui_cmd_server(CMD_USR_HISTORY);
				break;
		}

	}
}


void ask_himalaya_track_list_next_url()
{
	extern int h_net_return_play_track_item();
	int num = h_net_return_play_track_item();
	log_info(">>>>>>func %s line %d num  %d \n",__func__,__LINE__,num);
	if(num>__this->track_list.source_total)
	{
		return;	
	}
	extern int xy_http_get_play_url_by_track_id(u8* p_trackid, u8 pay_type, u8* album_id, u8 album_type);
	xy_http_get_play_url_by_track_id(__this->track_list.source_array[num].track_id,__this->track_list.source_array[num].pay_type,__this->track_list.source_array[num].album_id,__this->track_list.source_array[num].pay_type);
	memset(__this->ui.player_title,0,128);
	strncpy(__this->ui.player_title,__this->track_list.source_array[num].title,strlen(__this->track_list.source_array[num].title));
}







void himalaya_set_music_mode(u8 mode)
{
	mode+=6;
	switch(mode)
	{
		case HIMALAYA_REPEAT:
			os_taskq_post_msg("HIMALAYA_NET_MUSIC",1,HIMALAYA_REPEAT);//[repeat]
			break;
		case HIMALAYA_LOOP:
			os_taskq_post_msg("HIMALAYA_NET_MUSIC",1,HIMALAYA_LOOP);//[loop]
			break;
		case HIMALAYA_RAND:
			os_taskq_post_msg("HIMALAYA_NET_MUSIC",1,HIMALAYA_RAND);//[rand]
			break;
	}
}












































static int himalaya_qrcode_init(void)
{
	if(__qr)return -1;
	__qr=malloc(sizeof(himalaya_qr));
	__qr->title=malloc(QRCODE_NAME);
	__qr->mess=malloc(QRCODE_SIZE);
    __qr->data.code128_mode = 60;
    __qr->data.qr_version = 3;
    __qr->data.qr_max_version = 8;
    __qr->data.qr_ecc_level = 2;
    __qr->data.qr_code_max_input_len = 384;
    __qr->data.qr_buf_size = 4096;
    __qr->data.img_w = 260;
	return 0;
}

static void himalaya_qrcode_destory()	
{
	if(__qr->mess)
	{
		free(__qr->mess);
		__qr->mess=NULL;
	}
	if(__qr->title)
	{
		free(__qr->title);
		__qr->title=NULL;
	}
	if(__qr)
	{
		free(__qr);
		__qr=NULL;
	}
}

static void qr_code_event_init()
{
	h_qr_code_process(__qr->mess, strlen(__qr->mess), &__qr->data);
}



void fill_rect(void *_dc, struct rect *rectangle, u16 color);
static void qr_fill_rect(void *_dc, int x, int y, int width, int height, int color)
{
    struct rect rectangle;
    rectangle.left = x;
    rectangle.top = y;
    rectangle.width = width;
    rectangle.height = height;
    fill_rect(_dc, &rectangle, color);
}

void h_draw_qrcode(int id, u8 *dst_buf, struct rect *dst_r, struct rect *src_r, u8 bytes_per_pixel, void *priv)
{
    int w, h;
    struct rect r;
    struct rect rect = {0};
    int dst_stride = (dst_r->width * bytes_per_pixel + 3) / 4 * 4;
    int src_stride = (src_r->width * bytes_per_pixel + 3) / 4 * 4;
    u8 step_size;
    u8 remain_size;
    struct _H_QRCODE *qr_code = (struct _H_QRCODE *)priv;
    struct draw_context dc = {0};
    dc.buf = dst_buf;
    memcpy(&dc.disp, dst_r, sizeof(struct rect));
    memcpy(&dc.draw, dst_r, sizeof(struct rect));
    memcpy(&rect, src_r, sizeof(struct rect));
    //添加白底画图区域
    qr_fill_rect(&dc, rect.left, rect.top, rect.width, rect.height, 0xffff);


    if ((qr_code->out_size == 0) || (__qr->param.l_size == 0)) {
        step_size = 0;
        remain_size = 0;
    } else {
        step_size = rect.width / (qr_code->out_size * __qr->param.l_size);
        remain_size = (rect.width % (qr_code->out_size * __qr->param.l_size)) / 2;
    }
    for (int i = 0; i < qr_code->out_size * __qr->param.l_size; i++) {
        for (int j = 0; j < qr_code->out_size * __qr->param.l_size; j++) {
            if (__qr->copy_row_img[i * qr_code->out_size * __qr->param.l_size + j] == 0xff) {
                qr_fill_rect(&dc, rect.left + 0 + remain_size + step_size * j, rect.top + 0 + remain_size + step_size * i, step_size, step_size, 0xffff);
            }
            if (__qr->copy_row_img[i * qr_code->out_size * __qr->param.l_size + j] == 0x00) {
                qr_fill_rect(&dc, rect.left + 0 + remain_size + step_size * j, rect.top + 0 + remain_size + step_size * i, step_size, step_size, 0x0000);
            }
        }
    }
}

void h_qr_code_process(u8 *qr_code_str, int qr_code_len, struct _H_QRCODE *qr_code)
{
    int ret;
    u8 num = 0;
    log_info("before init:");
    log_info("qr:%s \n", qr_code_str);
    jl_code_init(qr_code->code128_mode, qr_code->qr_version, qr_code->qr_max_version, qr_code->qr_ecc_level,  qr_code->qr_code_max_input_len, qr_code->qr_buf_size, qr_code->img_w);
    ret = jl_code_process(JL_CODE_MODE_QR_CODE, qr_code_str, qr_code_len, &qr_code->out_size, &qr_code->line_size);  //返回值为 1，说明编码成功。
    log_info("out_size:%d line_size:%d", qr_code->out_size, qr_code->line_size);
    if (ret == 1) {                                   //out_size为基础数据大小，也就是原始数据大小
        __qr->param.l_size = 1;
        if (__qr->param.l_size > qr_code->line_size) {
            __qr->param.l_size = qr_code->line_size;
        }
        jl_code_set_info(&__qr->param);  //设置一个输出数据占用几个像素点，这个地方同比放大了数据
        __qr->row_img = br28_zalloc(qr_code->out_size * __qr->param.l_size);
        if (__qr->row_img == 0) {
            jl_code_deinit();
            log_info("after deinit:");
            return;
        }
        __qr->copy_row_img = br28_zalloc(qr_code->out_size * __qr->param.l_size * qr_code->out_size * __qr->param.l_size);
        if (__qr->copy_row_img == 0) {
            jl_code_deinit();
            log_info("after deinit:");
            return;
        }
        log_info("remain heap size:");
        for (unsigned char j = 0; j < qr_code->out_size; j++) {
            jl_code_get_data(qr_code->out_size, j, __qr->row_img);
            for (int k = 0; k < __qr->param.l_size; k++) {
                memcpy(__qr->copy_row_img + (j * __qr->param.l_size + k) * qr_code->out_size * __qr->param.l_size, __qr->row_img, qr_code->out_size * __qr->param.l_size);
            }
        }
    }
    jl_code_deinit();
    log_info("after deinit:");
}

static void qr_code_set_title()
{
	ui_text_set_textu_by_id(QR_CODE_TITLE,__qr->title,strlen(__qr->title),FONT_DEFAULT|FONT_SHOW_MULTI_LINE);
}

void ask_himalaya_qrcode_status()
{
	extern int xy_http_get_qrcode_status(void);
	xy_http_get_qrcode_status();
}

