#include "system/includes.h"
#include "server/audio_server.h"
#include "syscfg_id.h"
#include <stdlib.h>
#include "app_sdk_localdata_audio_play.h"
#include "app_config.h"
#include "brtc_wrapper.h"


bool rtc_player_tts = true;

//#if defined BRTC_SDK_ENABLE
static app_sdk_play_feed_cb g_play_feed_cb = NULL;
static app_sdk_play_end_cb g_play_end_cb = NULL;
static struct server *s_dec_server = NULL;
static volatile bool g_has_play = false;
static play_state_t s_play_state = AUDIO_PLAY_STOP;
static OS_MUTEX s_play_mutex;
static __g_rtc_player_volume = 50;
bool g_player_pause = false;
bool g_palyer_pause_interval = 20;

static int __play_mutex_init()
{
    return os_mutex_create(&s_play_mutex);
}
late_initcall(__play_mutex_init);

bool g_mute = false;

void setTTSMute(bool mute) {
    g_mute = mute;
}

static int app_sdk_audio_play_volume_step(int step)
{
    union audio_req req = {0};

    int volume = __g_rtc_player_volume + step;
    if (volume < MIN_VOLUME_VALUE) {
        volume = MIN_VOLUME_VALUE;
    } else if (volume > MAX_VOLUME_VALUE) {
        volume = MAX_VOLUME_VALUE;
    }
    if (volume == __g_rtc_player_volume) {
        return -EINVAL;
    }
    __g_rtc_player_volume = volume;

#ifdef CONFIG_UI_ENABLE
    os_taskq_post(POST_TASK_NAME, 2, UI_MSG_SET_VOLUME, volume);
#endif

    printf("set_dec_volume agent step: %d\n", volume);
/*
#ifdef CONFIG_DEC_DIGITAL_VOLUME_ENABLE
#if TCFG_USER_EMITTER_ENABLE
    if (get_bt_emitter_audio_server() && __this->digital_vol_hdl && __this->dec_server) {
        return user_audio_digital_volume_set(__this->digital_vol_hdl, volume, 1);
    }
#endif
#if TCFG_USER_VIRTUAL_PLAY_ENABLE
    if (check_user_virtual_audio_if_running() && __this->digital_vol_hdl && __this->dec_server) {
        return user_audio_digital_volume_set(__this->digital_vol_hdl, volume, 1);
    }
#endif
#endif

#ifdef CONFIG_VOICE_PROMPT_FILE_PATH
    if (volume == MAX_VOLUME_VALUE) {
        play_file(CONFIG_VOICE_PROMPT_FILE_PATH"VolumeFull.mp3");
    } else {
        play_file(CONFIG_VOICE_PROMPT_FILE_PATH"Volume.mp3");
    }
#endif
*/
    if (!s_dec_server) {
        return -EFAULT;
    }

    req.dec.cmd     = AUDIO_DEC_SET_VOLUME;
    req.dec.volume  = volume * ceofVolume;
    os_mutex_pend(&s_play_mutex, 0);
    if( 0 == server_request(s_dec_server, AUDIO_REQ_DEC, &req)) {
        os_mutex_post(&s_play_mutex);

        syscfg_write(CFG_MUSIC_VOL, &volume, sizeof(volume));
        printf("step server_request  volume:%d \r\n", volume);
        return -EFAULT;
    }
    os_mutex_post(&s_play_mutex);

    syscfg_write(CFG_MUSIC_VOL, &volume, sizeof(volume));
    printf("step server_request  volume:%d \r\n", volume);

    return 1;
}
static int app_sdk_audio_play_volume_lite() {

    union audio_req req = {0};

    if (!s_dec_server) {
        return -EFAULT;
    }
    req.dec.cmd     = AUDIO_DEC_SET_VOLUME;
    req.dec.volume  = __g_rtc_player_volume * ceofVolume;
    int ret  = 0 ;

    os_mutex_pend(&s_play_mutex, 0);
    if (ret = server_request(s_dec_server, AUDIO_REQ_DEC, &req) != 0) {
        os_mutex_post(&s_play_mutex);
        printf("server_request failed  :%d\r\n", ret);

        return - EFAULT;
    }
    os_mutex_post(&s_play_mutex);
    printf("syscfg_write finished:%d\n", __g_rtc_player_volume);

    return 1;
}

static int app_sdk_audio_play_volume(int volume) {

    union audio_req req = {0};

    if (volume < MIN_VOLUME_VALUE) {
        volume = MIN_VOLUME_VALUE;
    } else if (volume > MAX_VOLUME_VALUE) {
#ifdef CONFIG_VOICE_PROMPT_FILE_PATH
        //play_file(CONFIG_VOICE_PROMPT_FILE_PATH"VolumeFull.mp3");
#endif
        volume = MAX_VOLUME_VALUE;
    }
    if (volume == __g_rtc_player_volume) {
        return -EINVAL;
    }
    __g_rtc_player_volume = volume;

#ifdef CONFIG_UI_ENABLE
    os_taskq_post(POST_TASK_NAME, 2, UI_MSG_SET_VOLUME, __g_rtc_player_volume);
#endif

#ifdef CONFIG_DEC_DIGITAL_VOLUME_ENABLE
#if TCFG_USER_EMITTER_ENABLE
    if (get_bt_emitter_audio_server() && __this->digital_vol_hdl && __this->dec_server) {
        return user_audio_digital_volume_set(__this->digital_vol_hdl, __g_rtc_player_volume, 1);
    }
#endif
#if TCFG_USER_VIRTUAL_PLAY_ENABLE
    if (check_user_virtual_audio_if_running() && __this->digital_vol_hdl && __this->dec_server) {
        return user_audio_digital_volume_set(__this->digital_vol_hdl, __g_rtc_player_volume, 1);
    }
#endif
#endif

    if (!s_dec_server) {
        return -EFAULT;
    }
    printf("set_dec_volume: %d\n", __g_rtc_player_volume);
    req.dec.cmd     = AUDIO_DEC_SET_VOLUME;
    req.dec.volume  = __g_rtc_player_volume * ceofVolume;
    int ret  = 0 ;

    os_mutex_pend(&s_play_mutex, 0);
    if (ret = server_request(s_dec_server, AUDIO_REQ_DEC, &req) != 0) {
        os_mutex_post(&s_play_mutex);
        syscfg_write(CFG_MUSIC_VOL, &__g_rtc_player_volume, sizeof(__g_rtc_player_volume));

        printf("server_request failed  :%d\r\n", ret);

        return - EFAULT;
    }
    os_mutex_post(&s_play_mutex);
    syscfg_write(CFG_MUSIC_VOL, &__g_rtc_player_volume, sizeof(__g_rtc_player_volume));


    printf("syscfg_write finished:%d\n", __g_rtc_player_volume);

    return 1;
}

int app_adk_play_pause() {
    g_player_pause = true;
    g_palyer_pause_interval = 20;
}

static int vfs_fread(void *file, void *data, u32 len)
{
    // printf("%s, start,len:%d\n", __func__, len);
    if (s_play_state != AUDIO_PLAY_START)
    {
        return len;
    }
    if (g_mute) {
        memset(data, 0, len);
    } else {
        if(g_play_feed_cb)
        {
            g_play_feed_cb(data,len);
        }
    }

    int32_t cur_time =  timer_get_ms();
    if (g_player_pause) { //调用时间间隔
        if (g_palyer_pause_interval-- >0) {
            memset(data, 0, len);
        }else {
            g_player_pause  = false;
        }
    }
    //printf("%s, done,len:%d, cur_time = %d\n", __func__, len, cur_time);
    return len;

}

static int vfs_fclose(void *file)
{
    return 0;
}

static const struct audio_vfs_ops audioplay_vfs_ops =
{
    .fread = vfs_fread,
    .fclose = vfs_fclose,
};

static void play_dec_server_event_handler(void *priv, int argc, int *argv)
{
    printf("[play_dec_server_event_handler] %s event %d", __func__, argv[0]);
    switch (argv[0]) {
    case AUDIO_SERVER_EVENT_ERR:
    case AUDIO_SERVER_EVENT_END:
        printf("AUDIO_SERVER_EVENT_END\n");
        if(g_play_end_cb)
            g_play_end_cb();
        break;
    case AUDIO_SERVER_EVENT_CURR_TIME:
        break;
    case AUDIO_SERVER_EVENT_SPEAK_START:
        printf("+++vad start++++\n");
        break;
    case AUDIO_SERVER_EVENT_SPEAK_STOP:
        printf("+++vad stop++++\n");
        if(g_play_end_cb)
            g_play_end_cb();
        break;
    default:
        break;
    }
}

int app_sdk_audio_play_start(app_sdk_play_feed_cb cb_feed, app_sdk_play_end_cb cb_end)
{
    os_mutex_pend(&s_play_mutex, 0);
    if (!g_has_play)
    {
        g_has_play = true;
        int err;
        union audio_req req = {0};
        g_play_feed_cb = cb_feed;
        g_play_end_cb = cb_end;

        if (!s_dec_server) {
            s_dec_server = server_open("audio_server", "dec");
            if (!s_dec_server) {
                printf("app_sdk_audio_play_start s_dec_server null\n");
                os_mutex_post(&s_play_mutex);
                return -1;
            }
            server_register_event_handler_to_task(s_dec_server, NULL, play_dec_server_event_handler, "app_core");
        } else {
            os_mutex_post(&s_play_mutex);
            return -1;
        }

        syscfg_read(CFG_MUSIC_VOL, &__g_rtc_player_volume, sizeof(__g_rtc_player_volume));

        req.dec.cmd             = AUDIO_DEC_OPEN;
        req.dec.volume          = __g_rtc_player_volume * ceofVolume;
        req.dec.output_buf      = NULL;
        req.dec.output_buf_len  = 640;
        req.dec.channel         = 1;
        req.dec.sample_rate     = 8000;
        req.dec.priority        = 1;
        req.dec.vfs_ops         = &audioplay_vfs_ops;
        req.dec.dec_type        = "pcm";
        req.dec.sample_source   = "dac";

#ifdef BRTC_ENABLE_G722
        req.dec.output_buf_len  = 640;
        req.dec.channel         = 1;
        req.dec.sample_rate     = 16000;
#endif // 0
        printf("app_sdk_audio_play_start AUDIO_REQ_DEC task\n");
        //打开解码器
        if (server_request(s_dec_server, AUDIO_REQ_DEC, &req) != 0) {
            os_mutex_post(&s_play_mutex);
            return -1;
        }

        printf("app_sdk_audio_play_start AUDIO_DEC_START task\n");
        //开始解码
        s_play_state = AUDIO_PLAY_START;
        req.dec.cmd = AUDIO_DEC_START;
        if (server_request(s_dec_server, AUDIO_REQ_DEC, &req) != 0) {
            os_mutex_post(&s_play_mutex);
            return -1;
        }
        os_mutex_post(&s_play_mutex);
        return 0;
    }
    os_mutex_post(&s_play_mutex);
    return -1;
}

int app_sdk_audio_play_stop(void)
{
    os_mutex_pend(&s_play_mutex, 0);
    printf("app_sdk_audio_play_stop = %d\n", g_has_play);
    if (g_has_play) {
        g_has_play = false;

        int err;
        union audio_req req;
        if (!s_dec_server || s_play_state == AUDIO_PLAY_STOP) {
            os_mutex_post(&s_play_mutex);
            return 0;
        }
        s_play_state = AUDIO_PLAY_STOP;
        req.dec.cmd = AUDIO_DEC_STOP;
        err = server_request(s_dec_server, AUDIO_REQ_DEC, &req);

        server_close(s_dec_server);
        s_dec_server = NULL;
    }
    os_mutex_post(&s_play_mutex);
    return 0;
}

extern int mp3_get_dec_volume();

int app_sdk_audio_play_pause(bool pause) {

    os_mutex_pend(&s_play_mutex, 0);
    if (g_has_play) {
        int err;
        union audio_req req;
        if (!s_dec_server || s_play_state == AUDIO_PLAY_STOP) {
            os_mutex_post(&s_play_mutex);
            return 0;
        }
        if (pause) {

            s_play_state = AUDIO_PLAY_PAUSE;
            req.dec.cmd = AUDIO_DEC_PAUSE;
        } else {
            s_play_state = AUDIO_PLAY_START;
            req.dec.cmd = AUDIO_DEC_START ;
        }
        err = server_request(s_dec_server, AUDIO_REQ_DEC, &req);

    }
    os_mutex_post(&s_play_mutex);

    /*if (!pause) { //丢弃5帧数据 160*2*4*5
            char buf[1024]= {0, };
        for(int i = 0; i < 5; i++){
            if(g_play_feed_cb){
                g_play_feed_cb(buf, 960);
            }
        }
    }*/

    /*
    if(mp3_get_dec_volume() != __g_rtc_player_volume){
        app_sdk_audio_play_volume_lite(__g_rtc_player_volume);
    }*/
    return 0;
}



