package com.cooleshow.student;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

import androidx.annotation.RequiresApi;

import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.data.net.CommonParamsHelper;
import com.cooleshow.base.utils.FileUtils;
import com.cooleshow.base.utils.ProcessUtils;
import com.cooleshow.base.utils.Utils;
import com.cooleshow.usercenter.helper.UserHelper;
import com.daya.live_teaching.im.IMManager;
import com.rong.io.live.message.RCChatJoinRoomMessage;
import com.rong.io.live.message.RCChatModeMessage;
import com.rong.io.live.message.RCChatRoomCloseMessage;
import com.rong.io.live.message.RCChatRoomMemberNumMessage;
import com.rong.io.live.message.RCChatroomLocationMessage;
import com.rong.io.live.message.RCLiveBlackUserBlockMessage;
import com.rong.io.live.message.RCLiveBlackUserUnBlockMessage;
import com.rong.io.live.message.RCLiveGoodsChangeMessage;
import com.rong.io.live.message.RCOnSnappingUpMessage;
import com.rong.io.live.message.RCPauseLiveMessage;
import com.rong.io.live.message.RCUserAddLikeMessage;
import com.rong.io.live.message.RCUserKickOutMessage;
import com.rong.io.live.message.RCUserLeaveRoomMessage;
import com.rong.io.live.message.RCUserLogOutUnusualMessage;
import com.rong.io.live.message.RCUserMicLeaveMessage;
import com.rong.io.live.message.RCUserSeatApplyMessage;
import com.rong.io.live.message.RCUserSeatResponseMessage;
import com.rong.io.live.message.RCUserSeatsCtrlMessage;
import com.rong.io.live.message.RCUserSyncAddLikeCountMessage;
import com.tencent.bugly.crashreport.CrashReport;
import com.rong.io.live.helper.LiveRTCEngineInitHelper;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.vanniktech.emoji.EmojiManager;
import com.vanniktech.emoji.ios.IosEmojiProvider;

import cn.jiguang.api.utils.JCollectionAuth;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.rong.imlib.RongIMClient;
import io.rong.push.RongPushClient;
import io.rong.push.pushconfig.PushConfig;

/**
 * 创建日期：2022/5/25 17:16
 *
 * @author Ryan
 * 类说明：
 */
public class App extends BaseApplication {
    public static boolean isRefresh = false;

    @Override
    public void onCreate() {
        super.onCreate();
        ARouter.init(this);
        Utils.init(this);
        initCommonParams();
        initSDK();
    }

    private void initSDK() {
        //设置webview配置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            webviewSetPath(this);
        }
        //极光推送
        //用户同意之后才初始化极光推送
        JPushInterface.setDebugMode(true);
        JCollectionAuth.setAuth(this, UserHelper.isAgreePrivacy());
        registrationId = JPushInterface.getRegistrationID(this);
        initBugly();
        initRong();
        initUmeng();
        //二维码扫描
        ZXingLibrary.initDisplayOpinion(this);
    }

    private void initBugly() {
        if (UserHelper.isAgreePrivacy()) {
            CrashReport.initCrashReport(getApplicationContext(), "b51fd73164", false);
        }
    }

    private void initUmeng() {
        if (UserHelper.isAgreePrivacy()) {
            UMConfigure.init(this, "5f2b6f03b4b08b653e91a49e", "app", UMConfigure.DEVICE_TYPE_PHONE, null);
        } else {
            UMConfigure.preInit(this, "5f2b6f03b4b08b653e91a49e", "app");
        }
        String FileProvider = "com.cooleshow.student.fileprovider";
        PlatformConfig.setWeixin("wx97408cd22c879ff7", "665a1608a16631dee40d737b4d1a0ad1");
        PlatformConfig.setWXFileProvider(FileProvider);
        PlatformConfig.setSinaWeibo("3712190405", "e77924e0125dbd751bab61063d8338fb", "http://sns.whalecloud.com");
        PlatformConfig.setSinaFileProvider(FileProvider);
        PlatformConfig.setQQZone("1110018403", "fY5gof6GIYnc8Gzx");
        PlatformConfig.setQQFileProvider(FileProvider);
    }

    private void initRong() {
        PushConfig config = new PushConfig.Builder()
                .enableMiPush("2882303761520172242", "5692017221242")
                .enableOppoPush("5b2b716e774c45f2bd9a90b242b295e9", "3f1d849d8bce4273be48ca08a2ce320b")
                .enableHWPush(true)
                .enableVivoPush(true)
                .build();
        RongPushClient.setPushConfig(config);

        if (BuildConfig.DEBUG) {
            IMManager.init(this, "0vnjpoad0jbdz");
        } else {
            IMManager.init(this, "e5t4ouvpe42pa");
        }
        // RTCLib 初始化
        RongIMClient.registerMessageType(RCChatJoinRoomMessage.class);//加入直播房间消息
        RongIMClient.registerMessageType(RCChatModeMessage.class);//直播间聊天模式控制消息
        RongIMClient.registerMessageType(RCChatRoomCloseMessage.class);//直播间关闭消息
        RongIMClient.registerMessageType(RCChatroomLocationMessage.class);//本地消息
        RongIMClient.registerMessageType(RCChatRoomMemberNumMessage.class);//观众数量
        RongIMClient.registerMessageType(RCUserAddLikeMessage.class);//用户点赞消息
        RongIMClient.registerMessageType(RCUserKickOutMessage.class);//用户被踢出房间消息
        RongIMClient.registerMessageType(RCUserLeaveRoomMessage.class);//退出直播间消息
        RongIMClient.registerMessageType(RCUserLogOutUnusualMessage.class);//用户异常退出消息
        RongIMClient.registerMessageType(RCUserMicLeaveMessage.class);//观众下麦发送消息
        RongIMClient.registerMessageType(RCUserSeatApplyMessage.class);//申请连麦消息
        RongIMClient.registerMessageType(RCUserSeatResponseMessage.class);//连麦回复消息
        RongIMClient.registerMessageType(RCUserSeatsCtrlMessage.class);//连麦控制
        RongIMClient.registerMessageType(RCUserSyncAddLikeCountMessage.class);//连麦控制
        RongIMClient.registerMessageType(RCLiveGoodsChangeMessage.class);//直播间商品变化
        RongIMClient.registerMessageType(RCPauseLiveMessage.class);//直播间商品变化
        RongIMClient.registerMessageType(RCOnSnappingUpMessage.class);//XXX正在抢购
        RongIMClient.registerMessageType(RCLiveBlackUserBlockMessage.class);//添加黑名单
        RongIMClient.registerMessageType(RCLiveBlackUserUnBlockMessage.class);//解除黑名单

        LiveRTCEngineInitHelper.initRTC();
        EmojiManager.install(new IosEmojiProvider());
        /**
         * liveClass 相关开始
         */
        /*
         * 以上部分在所有进程中会执行
         */
        if (!getApplicationInfo().packageName.equals(ProcessUtils.getCurrentProcessName())) {
            return;
        }
    }


    private void initCommonParams() {
        String userToken = UserHelper.getUserToken();
        if (!TextUtils.isEmpty(userToken)) {
            //添加公共参数
            CommonParamsHelper.getInstance()
                    .addCommonHeaderParams(CommonParamsHelper.USER_TOKEN, userToken);
        }
    }


    @RequiresApi(api = 28)
    public void webviewSetPath(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(context);
            if (processName != null && !getApplicationContext().getPackageName().equals(processName)) {//判断不等于默认进程名称
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    public String getProcessName(Context context) {
        if (context == null)
            return null;
        if (!UserHelper.isAgreePrivacy()) {
            return null;
        }
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }

    @Override
    public boolean isTeacherClient() {
        return false;
    }
}

