package com.cooleshow.teacher;

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

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.RCOnSnappingUpMessage;
import com.rong.io.live.message.RCPauseLiveMessage;
import com.rong.io.live.message.RCUserAddLikeMessage;
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 androidx.annotation.RequiresApi;

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.core.Scheduler;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.rong.imkit.IMCenter;
import io.rong.imlib.RongIMClient;
import io.rong.push.RongPushClient;
import io.rong.push.pushconfig.PushConfig;

/**
 * Author by pq, Date on 2022/5/6.
 */
public class App extends BaseApplication {
    public static boolean isRefresh = false;
    private String mRegistrationId;

    @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);
        }
        initBugly();
        //极光推送
        JPushInterface.setDebugMode(true);
        JCollectionAuth.setAuth(this, UserHelper.isAgreePrivacy());
        registrationId = JPushInterface.getRegistrationID(this);
        initRong();
        initUmeng();
        //二维码扫描
        ZXingLibrary.initDisplayOpinion(this);
    }

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

    private void initUmeng() {
        if (UserHelper.isAgreePrivacy()) {
            UMConfigure.init(this, "62a8394605844627b5b0554b", "app", UMConfigure.DEVICE_TYPE_PHONE, null);
        } else {
            UMConfigure.preInit(this, "62a8394605844627b5b0554b", "app");
        }

        String FileProvider = "com.cooleshow.teacher.fileprovider";
        PlatformConfig.setWeixin("wx149a928c415c137a", "d3f119b577ccacb262da153f4210174f");
        PlatformConfig.setWXFileProvider(FileProvider);
        PlatformConfig.setSinaWeibo("755965096", "8a4d2b76bbb2006e62e21ac9014435da", "http://sns.whalecloud.com");
        PlatformConfig.setSinaFileProvider(FileProvider);
        PlatformConfig.setQQZone("1110018403", "fY5gof6GIYnc8Gzx");
        PlatformConfig.setQQFileProvider(FileProvider);
    }

    private void initRong() {
        PushConfig config = new PushConfig.Builder()
                .enableMiPush("2882303761520172302", "5102017284302")
                .enableOppoPush("e2e0eff48d6145c0b9f87cd6a57636ad", "519726bf2dfc4a6c95a6e00d61e47f10")
                .enableHWPush(true)
                .enableVivoPush(true)
                .build();
        RongPushClient.setPushConfig(config);

        if (BuildConfig.DEBUG) {
            IMManager.init(this, "0vnjpoad0jbdz");
        } else {
            IMManager.init(this, "e5t4ouvpe42pa");
        }
        RongIMClient.registerMessageType(RCChatJoinRoomMessage.class);//加入直播房间消息
        RongIMClient.registerMessageType(RCChatModeMessage.class);//直播间聊天模式控制消息
        RongIMClient.registerMessageType(RCChatroomLocationMessage.class);//本地消息
        RongIMClient.registerMessageType(RCUserMicLeaveMessage.class);//观众下麦发送消息
        RongIMClient.registerMessageType(RCUserSeatApplyMessage.class);//申请连麦消息
        RongIMClient.registerMessageType(RCUserSeatResponseMessage.class);//连麦回复消息
        RongIMClient.registerMessageType(RCUserSeatsCtrlMessage.class);//连麦控制
        RongIMClient.registerMessageType(RCUserSyncAddLikeCountMessage.class);//连麦控制
        RongIMClient.registerMessageType(RCChatRoomCloseMessage.class);//直播间关闭消息
        RongIMClient.registerMessageType(RCOnSnappingUpMessage.class);//XXX正在抢购
        RongIMClient.registerMessageType(RCUserAddLikeMessage.class);//点赞数量消息
        RongIMClient.registerMessageType(RCChatRoomMemberNumMessage.class);//成员数量同步
        RongIMClient.registerMessageType(RCPauseLiveMessage.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 true;
    }
}
