#include "app_config.h"
#include "system/includes.h"
#include "net_download.h"
#include "http/http_cli.h"
#include <stdlib.h>
#include "asm/includes.h"
#include "himalaya_net_music.h"
#include "os/os_api.h"

#if  TCFG_HIMALAYA_ENABLE
#define MUSIC_URL_LEN					512
#define MUSIC_BUFF_SIZE  				4*1024
#define MUSIC_BUFF_OFFSET  				4*1024
#define MUSIC_LIMIT_SIZE  				2*1024
#define MUSIC_CBUF_SIZE   				16*1024
#define MUSIC_TIMEOUT_MILLSET			10000
#define MUSIC_THRESHOLD   				100*1024
#define MUSIC_ROW_RANGE   				0
#define MUSIC_NET_NAME 			"HIMALAYA_NET_MUSIC"
static struct net_download_parm parm;
struct h_music_info h_music;
int timeout_id=NULL;

typedef enum {
    PLAYER_URL         = 0,
    PLAYER_PAUSE       = 1,
    PLAYER_RESUME      = 2,
    PLAYER_KILL        = 3,
    PLAYER_PREV_TRACK  = 4,
    PLAYER_BACKWARD    = 5,
    PLAYER_REPEAT      = 6,
    PLAYER_LOOP        = 7,
    PLAYER_RAND        = 8,
} playercommand;

enum
{
	MUSIC_TMP,
	MUSIC_REPEAT,	
	MUSIC_LOOP,	
	MUSIC_RAND,	
};
/* 状态处理函数前置声明 */
static void handle_url(void * param);
static void handle_pause(void* param);
static void handle_resume(void* param);
static void handle_kill(void* param);
static void handle_prev_track(void* param);
static void handle_backward(void* param);
static int mode;
/* 具体状态处理实现 */
static void handle_url(void * param)
{
	strncpy(h_music.hstream.url,param,MUSIC_URL_LEN);
	parm.url                = h_music.hstream.url;
	parm.cbuf_size          = MUSIC_CBUF_SIZE;
	parm.timeout_millsec    = MUSIC_TIMEOUT_MILLSET;
	parm.seek_threshold     = MUSIC_THRESHOLD;
	parm.seek_low_range    	= MUSIC_ROW_RANGE;
	if(!h_music.hstream.dec_buf)	
	{
		h_music.hstream.dec_buf=malloc(MUSIC_BUFF_SIZE);
	}
	cbuf_init(&(h_music.hstream.dec_cbuf),h_music.hstream.dec_buf,MUSIC_BUFF_SIZE);
	net_download_open(&h_music.hstream.net_file, &parm);
}
static void handle_pause(void* param)
{
}
static void handle_resume(void* param) 
{
	net_tts_dec_resume(NULL);
	h_music.ex_status=0;
}
static void handle_kill(void* param) 
{
	int msg[16];
	os_taskq_pend(NULL, msg, ARRAY_SIZE(msg));
}
static void handle_prev_track(void* param) 
{
	h_music.play_status=1;
	h_music.ex_status=0;
}
static void handle_backward(void* param) 
{
	h_music.play_status=2;
	h_music.ex_status=0;
}
static void handle_repeat(void* param) 
{
	mode=MUSIC_REPEAT;
}
static void handle_loop(void* param) 
{
	mode=MUSIC_LOOP;
}
static void handle_rand(void* param) 
{
	mode=MUSIC_RAND;
}

static void h_net_music_task_init(void)
{
	h_music.hstatus.run=1;
	h_music.first_flag = 1;
	h_music.ex_status=0;
    set_continuous_trans(1);
}
static int h_get_first_flag()
{
	return h_music.first_flag;	
}


void clear_music_info(struct h_music_info *info) {
    memset(info, 0, sizeof(struct h_music_info));
}

static int h_music_cb(void *priv, void *buf, u32 len, u8 tmp_flag, int tmp_offset)
{
	int rlen = 0;
	if(h_music.play_status==1)
	{
		rlen = cbuf_read(&(h_music.hstream.dec_cbuf), buf+MUSIC_BUFF_OFFSET, len);
		h_music.play_status=0;
	}
	else if(h_music.play_status==2)
	{
		rlen = cbuf_read(&(h_music.hstream.dec_cbuf), buf-MUSIC_BUFF_OFFSET, len);
		h_music.play_status=0;
	}
	else
	{
		rlen = cbuf_read(&(h_music.hstream.dec_cbuf), buf, len);
	}
	if (rlen != len) {
		rlen = cbuf_get_data_len(&(h_music.hstream.dec_cbuf));
		rlen = cbuf_read(&(h_music.hstream.dec_cbuf), buf, rlen);
	}
	if (rlen == 0) {
		rlen = -1; // 没结束时返回-1挂起解码，不结束解码
	}
	return rlen;
}

static void h_net_music_task_main(void)
{
	int msg[16];
	int res;
	int pend=1;
	static int command;
	h_net_music_task_init();
	while(1)
	{
		putchar('O');
        if (pend) {
            res = os_taskq_pend(NULL, msg, ARRAY_SIZE(msg));
        } else {
            res = os_taskq_accept(ARRAY_SIZE(msg), msg);
        }
		if (res == OS_TASKQ)
		{
			command = msg[1];
			switch(command)
			{
				case PLAYER_URL:
					pend=0;
					handle_url(msg[2]);
					break;
				case PLAYER_PAUSE:
					pend=1;
					handle_pause(NULL);
					break;
				case PLAYER_RESUME:
					pend=0;
					handle_resume(NULL);
					break;
				case PLAYER_KILL:
					pend=1;
					handle_kill(NULL);
					break;
				case PLAYER_PREV_TRACK:
					pend=0;
					handle_prev_track(NULL);
					break;
				case PLAYER_BACKWARD:
					pend=0;
					handle_backward(NULL);
					break;
				case PLAYER_REPEAT:
					pend=0;
					handle_repeat(NULL);
					break; 
				case PLAYER_LOOP:
					pend=0;
					handle_loop(NULL);
					break;
				case PLAYER_RAND:
					pend=0;
					handle_rand(NULL);
					break;
			}
		}
		net_download_get_status(h_music.hstream.net_file,&h_music.hstatus.down_status,&h_music.hstatus.err_status);
		if(h_music.hstatus.down_status==2)//播放结束流程,他杀
		{
			h_music.hstream.f_len= net_download_get_tmp_data_size(h_music.hstream.net_file);
			h_net_music_task_kill();
		}
		h_music.hstream.f_len= net_download_get_tmp_data_size(h_music.hstream.net_file);
		h_music.hstream.music_buf=cbuf_write_alloc(&(h_music.hstream.dec_cbuf),&h_music.hstream.cbuf_size);
		if(h_music.hstream.f_len>h_music.hstream.cbuf_size)
		{
			h_music.hstream.r_len=	net_download_read(h_music.hstream.net_file, h_music.hstream.music_buf,h_music.hstream.cbuf_size);
			cbuf_write_updata(&(h_music.hstream.dec_cbuf),h_music.hstream.cbuf_size);
		}
		h_music.hstream.cbuf_size = cbuf_get_data_size(&(h_music.hstream.dec_cbuf));

		if (h_get_first_flag()) {
			if (h_music.hstream.cbuf_size >= MUSIC_LIMIT_SIZE) {
				net_tts_dec_open(NULL, h_music_cb);
				h_music.first_flag = 0;
			}
		}
		os_time_dly(1);
	}
}



static int sel_item;
void h_net_get_play_track_item(int num)
{
	sel_item=num;
}

int h_net_return_play_track_item()
{
	return sel_item;
}

static void h_net_play_track_clear_flag()
{
	sel_item=0;
}


static void h_net_next_music_audio()
{
	if(mode<PLAYER_REPEAT)
	{
		mode=PLAYER_REPEAT;	
	}
	switch(mode)
	{
		case MUSIC_REPEAT:
			sel_item+=1;	
			break;
		case MUSIC_LOOP:
			break;
		case MUSIC_RAND:
			sel_item=rand32()%6+1;
			break;
		default:
			sel_item+=1;	
			break;
	}
	extern void ask_himalaya_track_list_next_url();
	ask_himalaya_track_list_next_url();
	sys_timeout_del(timeout_id);
	timeout_id=NULL;
}

void h_net_music_task_create(void)
{
	if(h_music.hstatus.run==1)
	{
		return;
	}
    os_task_create(h_net_music_task_main, NULL, 31, 512, 128,MUSIC_NET_NAME);
}


static void h_net_music_task_del(void)
{
	while(!h_music.hstatus.run)
	{
		os_time_dly(1);	
	}
	h_music.hstatus.run=0;
	net_download_close(h_music.hstream.net_file);
	net_tts_dec_close();
	int ret=task_kill(MUSIC_NET_NAME);
	set_continuous_trans(0);
	if(ret!=OS_NO_ERR)
	{
		g_printf("task del err :%s %d \n",__FUNCTION__,ret);	
	}
	cbuf_clear(&(h_music.hstream.dec_cbuf));
	free(h_music.hstream.dec_buf);
	clear_music_info(&h_music);
	h_music.first_flag = 1;
}


void h_net_music_task_quit(void)
{
	if(h_music.hstatus.run==0)return;	
	os_taskq_post_msg("HIMALAYA_NET_MUSIC",1,PLAYER_PAUSE);
	os_time_dly(10);
	h_net_music_task_del();
	h_net_play_track_clear_flag();
}
static void h_net_music_task_kill_cb(void)
{
	while(!h_music.hstatus.run)
	{
		os_time_dly(1);	
	}
	h_music.hstatus.run=0;
	net_download_close(h_music.hstream.net_file);
	net_tts_dec_close();
	int ret=task_kill(MUSIC_NET_NAME);
	set_continuous_trans(0);
	if(ret!=OS_NO_ERR)
	{
		g_printf("task del err :%s %d \n",__FUNCTION__,ret);	
	}
	cbuf_clear(&(h_music.hstream.dec_cbuf));
	free(h_music.hstream.dec_buf);
	clear_music_info(&h_music);
	timeout_id=sys_timeout_add(NULL,h_net_next_music_audio,100);
}

void h_net_music_task_kill(void)
{
	if(h_music.hstatus.run==0)return;	
	os_taskq_post_msg("HIMALAYA_NET_MUSIC",1,PLAYER_KILL);
	os_time_dly(10);
	int msg[3];
	msg[0]=(int)h_net_music_task_kill_cb;
	msg[1]=ARRAY_SIZE(msg)-2;
	os_taskq_post_type("app_core",Q_CALLBACK,ARRAY_SIZE(msg),msg);
}


int h_net_music_return_status()
{
	return h_music.hstatus.run;
}

#endif
