package com.bokecc.dwlivedemo;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.bokecc.chatroom.ChatRoomInteractSDK;
import com.bokecc.commerce.CommerceInteractSDK;
import com.bokecc.doc.docsdk.DocSDK;
import com.bokecc.gift.GiftInteractSDK;
import com.bokecc.interact.InteractSDK;
import com.bokecc.invitationcard.InvitationCardInteractSDK;
import com.bokecc.like.LikeInteractSDK;
import com.bokecc.livemodule.utils.SPUtil;
import com.bokecc.questionnaire.QuestionnaireInteractSDK;
import com.bokecc.redpacket.RedPacketInteractSDK;
import com.bokecc.sdk.mobile.live.DWLiveEngine;
import com.bokecc.vote.VoteInteractSDK;

/**
 * 应用的 Application
 */
public class DWApplication {

    private static final String TAG = "DWApplication";

    private static Application context;

    private OnChannelMessageListener channelMessageListener;

    private DWApplication() {}

    public static DWApplication getInstance() {
        return DWApplication.SingletonHolder.instance;
    }

    private static class SingletonHolder {
        public static DWApplication instance = new DWApplication();
    }

    /**
     * 初始化
     *
     * @param app
     */
    public void init(Application app) {
        context = app;

        if (!getPrivacy()){
            Log.e("DWApplication","wait privacy");
            return;
        }

        initWithoutPrivacy(app);
    }/**
     * 初始化，不需要授权，提供给 uniapp
     *
     * @param app
     */
    public void initWithoutPrivacy(Application app) {
        context = app;

        // 只需要初始化一次
        if (DWLiveEngine.getInstance() == null) {
            //在业务入口页面设置是否启用x5以及是否使用docsdk作为文档组件，docsdk为单独文档sdk，依赖dwlivesdk
            boolean isUseX5 = SPUtil.getInstance().getBoolean(Constants.SP_WEB_CORE_KEY,true);

            InteractSDK.getInstance().init(app);
            GiftInteractSDK.getInstance().init(app);
            LikeInteractSDK.getInstance().init(app);
            VoteInteractSDK.getInstance().init(app);
            RedPacketInteractSDK.getInstance().init(app);
            QuestionnaireInteractSDK.getInstance().init(app);
            InvitationCardInteractSDK.getInstance().init(app);
            CommerceInteractSDK.getInstance().init(app);
            ChatRoomInteractSDK.getInstance().init(app);

            // enableLog:是否开启日志
            // enableX5: 是否开启x5内核，建议启用x5内核，提升文档稳定性
            DWLiveEngine.init(app, BuildConfig.DEBUG, isUseX5);

            //参数1：context；参数2：文档sdk支持x5内核初始化，如果在外部进行x5初始化或者不想启用x5内核，最后一个参数isEnableX5请赋值false
            DocSDK.init(app,isUseX5);
        } else {
            Log.i(TAG, "DWLiveEngine has init");
        }
    }

    public boolean getPrivacy(){
        return SPUtil.getInstance().getBoolean("cc_live_privacy");
    }

    public void setPrivacy(boolean privacy){
        SPUtil.getInstance().put("cc_live_privacy",privacy);
    }

    /**
     * 销毁
     */
    public void onTerminate() {
        DWLiveEngine.getInstance().onTerminate();
    }

    /**
     * 获取上下文
     * @return context
     */
    public static Context getContext() {
        return context;
    }


    /**
     * 设置公共消息通道，给Flutter扩展使用
     * @param channelMessageListener 监听
     */
    public void setMessageChannel(OnChannelMessageListener channelMessageListener){
        this.channelMessageListener = channelMessageListener;
    }

    public OnChannelMessageListener  getChannelMessageListener(){
        return channelMessageListener;
    }

    public void onMessage(String msg){
        if(channelMessageListener != null){
            channelMessageListener.onMessage(msg);
        }
    }

    public interface OnChannelMessageListener{
        void onMessage(String msg);
    }
}

