
/*************************************************************
   此文件函数主要是lp模式按键处理和事件处理

    void app_lp_task()
   lp模式主函数

    static int lp_sys_event_handler(struct sys_event *event)
   lp模式系统事件所有处理入口

    static void lp_task_close(void)
    lp模式退出

**************************************************************/

#include "system/app_core.h"
#include "system/includes.h"
#include "server/server_core.h"

#include "app_config.h"
#include "app_task.h"

#include "media/includes.h"
#include "tone_player.h"
#include "audio_dec_linein.h"

#include "asm/audio_linein.h"
#include "asm/pwm_led.h"
#include "asm/charge.h"

#include "app_charge.h"
#include "app_main.h"
#include "app_online_cfg.h"
#include "app_power_manage.h"

#include "app_chargestore.h"
#include "gSensor/gSensor_manage.h"
#include "ui_manage.h"
#include "vm.h"

#include "lp/lp_dev.h"
#include "lp/lp.h"

#include "key_event_deal.h"
#include "user_cfg.h"
#include "ui/ui_api.h"
#include "fm_emitter/fm_emitter_manage.h"
#include "clock_cfg.h"

#include "bt.h"
#include "bt_tws.h"

#include "kt.h"

#if TCFG_APP_LP_EN

#if (SMART_BOX_EN)
__attribute__((weak)) void smartbox_lp_msg_deal(int msg, u8 ret)
{
    (void)msg;
    (void)ret;
}
#endif

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

static u8 lp_last_onoff = (u8)-1;
static u8 lp_bt_back_flag = 0;
static u8 lp_idle_flag = 1;

///*----------------------------------------------------------------------------*/
/**@brief   当前出于非lp 模式时候lp的在common消息处理
   @param    无
   @return   1:需要切换回lp 模式 0：无
   @note
*/
/*----------------------------------------------------------------------------*/
int lp_device_event_handler(struct sys_event *event)
{
    if ((u32)event->arg == DEVICE_EVENT_FROM_LP)
    {
        if (event->u.dev.event == DEVICE_EVENT_IN)
        {
            printf("lp online \n");
            return true;
        }
        else if (event->u.dev.event == DEVICE_EVENT_OUT)
        {
            printf("lp offline \n");
        }
    }
    return false;
}

static int lp_key_event_filter_before()
{
#if TCFG_UI_ENABLE
#if (TCFG_APP_FM_EMITTER_EN == ENABLE_THIS_MOUDLE)
    if (ui_get_app_menu(GRT_CUR_MENU) == MENU_FM_SET_FRE)
    {
        return false;
    }
#endif
#endif
    return TRUE;
}

//*----------------------------------------------------------------------------*/
/**@brief    lp 按键消息入口
   @param    无
   @return   1、消息已经处理，不需要发送到common  0、消息发送到common处理
   @note
*/
/*----------------------------------------------------------------------------*/
static int lp_key_msg_deal(struct sys_event *event)
{
    int ret = true;
    int err = 0;
    u8 vol;
    int key_event = event->u.key.event;
    int key_value = event->u.key.value; //

    if (lp_key_event_filter_before() == false)
    {
        return false;
    }

    log_info("key_event:%d \n", key_event);
#if (SMART_BOX_EN)
    extern bool smartbox_key_event_filter_before(int key_event);
    if (smartbox_key_event_filter_before(key_event))
    {
        return true;
    }
#endif

    switch (key_event)
    {
    case KEY_LP_START:
        printf("KEY_LP_START\n");
        lp_start();
        lp_last_onoff = 1;
        UI_REFLASH_WINDOW(true); // 刷新主页并且支持打断显示
        break;
    case KEY_TEST_DEMO_0:
        log_info("KEY_TEST_DEMO_0 = %d \n", key_value);
        app_task_put_key_msg(KEY_TEST_DEMO_1, 5678); // test demo
        break;
    case KEY_TEST_DEMO_1:
        log_info("KEY_TEST_DEMO_1 = %d \n", key_value); // test demo
        break;
    /* case KEY_USER_CHANGE_MODE:
        printf("  KEY_USER_CHANGE_MODE \n");
        if (linein_app_check())
        {
            app_task_switch_to(APP_LINEIN_TASK);            
        }
        else if (music_app_check())
        {
            app_task_switch_to(APP_MUSIC_TASK);
        }
        else
        {
            app_task_switch_to(APP_BT_TASK);           
        }        
        break; */

    case KEY_MUSIC_PP:
        log_info("KEY_MUSIC_PP\n");
        /* app_task_put_key_msg(KEY_TEST_DEMO_0,1234);  //test demo// */
        lp_last_onoff = lp_volume_pp();
        //lp_last_onoff ? ui_update_status(STATUS_LP_PLAY)
        //              : ui_update_status(STATUS_LP_PAUSE);
        break;
    case KEY_VOL_UP:
        log_info(" KEY_VOL_UP\n");
        lp_key_vol_up();
        break;

    case KEY_VOL_DOWN:
        log_info(" KEY_VOL_DOWN\n");
        lp_key_vol_down();
        break;

    default:
        ret = false;
        break;
    }
#if (SMART_BOX_EN)
    smartbox_lp_msg_deal(key_event, ret);
#endif

    return ret;
}

//*----------------------------------------------------------------------------*/
/**@brief    音乐播放结束回调函数
   @param    无
   @return
   @note
*/
/*----------------------------------------------------------------------------*/
static void line_tone_play_end_callback(void *priv, int flag)
{
    u32 index = (u32)priv;

    if (APP_LP_TASK != app_get_curr_task())
    {
        log_error("tone callback task out \n");
        return;
    }

    switch (index)
    {
    //case IDEX_TONE_LP:
        /// 提示音播放结束， 启动播放器播放
    //    app_task_put_key_msg(KEY_LP_START, 0);
    //    break;
    default:
        break;
    }
}

//*----------------------------------------------------------------------------*/
/**@brief    lp 入口
   @param    无
   @return
   @note
*/
/*----------------------------------------------------------------------------*/
static void lp_app_init(void)
{
    lp_idle_flag = 0;
    UI_SHOW_WINDOW(ID_WINDOW_LINEIN); // LP 与 Linein 共用 AUX 界面
    UI_SHOW_MENU(MENU_AUX, 0, 0, NULL);
    sys_key_event_enable(); // 开启按键
    //ui_update_status(STATUS_LP_MODE);
    clock_idle(LINEIN_IDLE_CLOCK);
}

//*----------------------------------------------------------------------------*/
/**@brief    lp 退出
   @param    无
   @return
   @note
*/
/*----------------------------------------------------------------------------*/

static void lp_task_close(void)
{
    UI_HIDE_CURR_WINDOW();
    lp_stop();
    /* tone_play_stop(); */
    //tone_play_stop_by_path(tone_table[IDEX_TONE_LP]);
    lp_idle_flag = 1;
}

//*----------------------------------------------------------------------------*/
/**@brief    lp 模式活跃状态 所有消息入口
   @param    无
   @return   1、当前消息已经处理，不需要发送comomon 0、当前消息不是lp处理的，发送到common统一处理
   @note
*/
/*----------------------------------------------------------------------------*/
static int lp_sys_event_handler(struct sys_event *event)
{
    int ret = TRUE;
    switch (event->type)
    {
    case SYS_KEY_EVENT:
        return lp_key_msg_deal(event);
        break;
    case SYS_DEVICE_EVENT:
        if ((u32)event->arg == DEVICE_EVENT_FROM_LP)
        {
            if (event->u.dev.event == DEVICE_EVENT_IN)
            {
                log_info("lp online \n");
            }
            else if (event->u.dev.event == DEVICE_EVENT_OUT)
            {
                log_info("lp offline \n");
                if (music_app_check())
                {
                    app_task_switch_to(APP_MUSIC_TASK);
                }
                else
                {
                    app_task_switch_next();
                }
                
            }
            return true;
        }
        return false;
        break;
    default:
        return false;
    }
    return false;
}

//*----------------------------------------------------------------------------*/
/**@brief    lp 在线检测  切换模式判断使用
   @param    无
   @return   1 lp设备在线 0 设备不在线
   @note
*/
/*----------------------------------------------------------------------------*/
int lp_app_check(void)
{
    if (lp_is_online())
    {
        return true;
    }
    return false;
}

//*----------------------------------------------------------------------------*/
/**@brief    lp 主任务
   @param    无
   @return   无
   @note
*/
/*----------------------------------------------------------------------------*/
void app_lp_task()
{
    int res;
    int err = 0;
    int msg[32];

#if TCFG_APP_BT_EN
    lp_bt_back_flag = get_bt_back_flag(); // 从蓝牙后台返回标志
    set_bt_back_flag(0);
#else
    lp_bt_back_flag = 0;
#endif
    log_info("lp_bt_back_flag == %d lp_last_onoff = %d\n",
             lp_bt_back_flag, lp_last_onoff);

    lp_app_init(); // 初始化时钟和开启ui

    kt_eq_switch_to(KT_EQ_IDX_LP);

#if TCFG_DEC2TWS_ENABLE
    extern void set_tws_background_connected_flag(u8 flag);
    extern u8 get_tws_background_connected_flag();
    if (get_tws_background_connected_flag())
    { // 不播放提示音
        app_task_put_key_msg(KEY_LP_START, 0);
        set_tws_background_connected_flag(0);
    }
    else
#endif
    {
        //err = tone_play_with_callback_by_name(tone_table[IDEX_TONE_LP], 1,
        //                                      line_tone_play_end_callback, (void *)IDEX_TONE_LP);
        
    }
    //    if (err) { //
    //        ///提示音播放失败，直接推送KEY_MUSIC_PLAYER_START启动播放
    //        app_task_put_key_msg(KEY_LP_START, 0);
    //    }
    app_task_put_key_msg(KEY_LP_START, 0);

    while (1)
    {
        app_task_get_msg(msg, ARRAY_SIZE(msg), 1);

        switch (msg[0])
        {
        case APP_MSG_SYS_EVENT:
            if (lp_sys_event_handler((struct sys_event *)(&msg[1])) == false)
            {
                app_default_event_deal((struct sys_event *)(&msg[1])); // 由common统一处理
            }
            break;
        default:
            break;
        }

        if (app_task_exitting())
        {
            lp_task_close();
            return;
        }
    }
}

static u8 lp_idle_query(void)
{
    return lp_idle_flag;
}
REGISTER_LP_TARGET(lp_lp_target) = {
    .name = "lp",
    .is_idle = lp_idle_query,
};

#else

int lp_app_check(void)
{
    return false;
}

void app_lp_task()
{
}

#endif
