package com.cooleshow.student.ui.live;

import android.Manifest;
import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.airbnb.lottie.LottieAnimationView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.cooleshow.base.common.WebConstants;
import com.cooleshow.base.data.net.ApiException;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.AppUtils;
import com.cooleshow.base.utils.PermissionUtils;
import com.cooleshow.base.utils.SizeUtils;
import com.cooleshow.base.utils.SoftKeyboardUtil;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.UiUtils;
import com.cooleshow.base.utils.Utils;
import com.cooleshow.base.widgets.InputBar;
import com.cooleshow.base.widgets.dialog.CommonConfirmDialog;
import com.cooleshow.student.bean.FriendInfoBean;
import com.cooleshow.student.ui.live.floatPop.FloatWindowHelper;
import com.cooleshow.student.ui.live.floatPop.FloatingWindowService;
import com.cooleshow.student.widgets.dialog.LiveRoomCloseTipDialog;
import com.cooleshow.student.widgets.dialog.OpenOverlayPermissionTipDialog;
import com.cooleshow.student.widgets.helper.LiveRoomAddLikeHelper;
import com.cooleshow.base.widgets.dialog.InputBarDialog;
import com.cooleshow.student.R;
import com.cooleshow.student.adapter.MessageAdapter;
import com.cooleshow.student.bean.LiveRoomInfoBean;
import com.cooleshow.student.contract.LiveRoomContract;
import com.cooleshow.student.databinding.ActivityLiveroomLayoutBinding;
import com.cooleshow.student.presenter.live.LiveRoomPresenter;
import com.rong.io.live.helper.LiveMemberHelper;
import com.rong.io.live.helper.LiveMessageHelper;
import com.rong.io.live.helper.LiveRTCEngineInitHelper;
import com.cooleshow.student.widgets.dialog.LiveRoomCloseMicTipDialog;
import com.cooleshow.student.widgets.dialog.LiveRoomClosePageOnMicTipDialog;
import com.cooleshow.student.widgets.dialog.LiveRoomInviteSeatMicTipDialog;
import com.cooleshow.student.widgets.dialog.LiveRoomShopCarDialog;
import com.cooleshow.student.widgets.helper.LiveRoomAnimatorHelper;
import com.cooleshow.usercenter.helper.UserHelper;
import com.rong.io.live.LiveRoomMsgConstants;
import com.rong.io.live.config.LiveConfig;
import com.rong.io.live.helper.LiveEventHelper;
import com.rong.io.live.message.RCChatJoinRoomMessage;
import com.rong.io.live.message.RCOnSnappingUpMessage;
import com.rong.io.live.message.RCUserKickOutMessage;
import com.rong.io.live.message.RCUserLogOutUnusualMessage;
import com.rong.io.live.message.RCUserSeatApplyMessage;
import com.rong.io.live.message.RCUserSeatResponseMessage;
import com.rong.io.live.message.RCUserSyncAddLikeCountMessage;
import com.rong.io.live.widget.LiveRoomMicIconView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.rongcloud.rtc.api.RCRTCAudioRouteManager;
import cn.rongcloud.rtc.api.RCRTCRemoteUser;
import cn.rongcloud.rtc.api.RCRTCRoom;
import cn.rongcloud.rtc.api.RCRTCVideoStream;
import cn.rongcloud.rtc.api.stream.RCRTCAudioInputStream;
import cn.rongcloud.rtc.api.stream.RCRTCAudioOutputStream;
import cn.rongcloud.rtc.api.stream.RCRTCInputStream;
import cn.rongcloud.rtc.api.stream.RCRTCVideoInputStream;
import cn.rongcloud.rtc.api.stream.RCRTCVideoOutputStream;
import cn.rongcloud.rtc.api.stream.RCRTCVideoView;
import cn.rongcloud.rtc.base.RCRTCLiveRole;
import cn.rongcloud.rtc.base.RCRTCMediaType;
import cn.rongcloud.rtc.base.RCRTCResourceState;
import cn.rongcloud.rtc.core.RendererCommon;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.rxjava3.disposables.Disposable;
import io.rong.imkit.IMCenter;
import io.rong.imlib.IRongCoreCallback;
import io.rong.imlib.IRongCoreEnum;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;
import io.rong.message.TextMessage;

import com.cooleshow.student.widgets.helper.VideoViewManager;
import com.tbruyelle.rxpermissions3.RxPermissions;

/**
 * Author by pq, Date on 2022/3/29.
 */

@Route(path = RouterPath.LiveCenter.ACTIVITY_LIVE_ROOM)
public class LiveRoomActivity extends BaseMVPActivity<ActivityLiveroomLayoutBinding, LiveRoomPresenter> implements View.OnClickListener, LiveRoomContract.view, SoftKeyboardUtil.OnSoftInputChangedListener {
    public static final int SEND_SHOW_FLOAT_WINDOW_TIME = 1000;//退到后台以后检查是否显示悬浮窗等待时长
    public static final int SEND_APP_BACKGROUND_MSG = 1001;//退到后台消息
    public static final int SEND_JUMP_OTHER_PAGE_MSG = 1002;//页面切换消息(购物车详情页等等)
    public static final int OPEN_SHOP_CAR_DIALOG = 1003;//打开购物车
    public static final int LIVE_STATUS_IS_REST = 1;//休息一下,马上回来
    public static final int LIVE_STATUS_IS_CLOSE_VIDEO = 2;//主持人关闭画面
    public static final int LIVE_STATUS_IS_OFFLINE = 3;//直播已结束
    ConstraintLayout content_view;
    RelativeLayout mFlLiveView;
    LinearLayout ll_like;
    RecyclerView mRecyclerMsg;
    TextView mTvNumPeople;
    ImageView iv_mic;
    LiveRoomMicIconView mLlMicContainer;
    TextView mTvAddLikeCount;
    CircleImageView mIvAvatar;
    TextView mTvRoomCreateName;
    Group mGroupViews;
    ConstraintLayout mViewLiveStatus;
    ImageView mIvLiveStatusCenterIcon;
    TextView mTvLiveStatusTipText;
    ImageView mIvSwitchVideoOrientation;
    ImageView mIvSwitchVideoOrientationFull;
    ConstraintLayout cs_header_info;
    LottieAnimationView mViewShopCarAnim;
    ImageView mIvShopCar;
    ImageView mIvClose;
    TextView tv_input;
    FrameLayout mFlJoinBarrage;
    TextView mTvJoinBarrage;
    FrameLayout mFlSnapUpBarrage;
    TextView mTvSnapUpBarrage;

    private LiveRoomInfoBean mRoomInfoBean;
    private String mRoomId;
    private MessageAdapter mMessageAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private boolean isEnableChat = false;//默认不禁止
    private boolean isEnableMic = false;//默认不禁止
    private boolean isEnableAll = false;//默认不禁止 黑名单状态
    private InputBarDialog mInputBarDialog;
    private boolean isPcClientLive = true;//默认为PC端直播
    private int currentSeatStatus = LiveRoomMsgConstants.MIC_STATUS_NORMAL;//1未连麦,2连麦中,3连麦成功
    private LiveRoomCloseMicTipDialog mRoomCloseMicTipDialog;
    private LiveRoomInviteSeatMicTipDialog mInviteSeatMicTipDialog;
    private int currentAddLikeCount = 0;//当前点赞数量
    private boolean creatorIsCloseAudioStream = false;
    private boolean creatorIsCloseVideoStream = false;
    private String createRoomId = "";
    private static final String ROOMID_KEY = "roomid_key";
    private String mUserId;
    private boolean connectStatusIsPaused = false;
    private boolean isNeedRefresh = false;//是否需要重新刷新房间信息
    private boolean liveIsFinish = false;//直播间是否已结束
    private ServiceConnection serviceConnection;
    private ObjectAnimator hideHeaderInfoAnim;
    private ObjectAnimator showHeaderInfoAnim;
    private boolean isOnResume = true;
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            if (msg.what == SEND_APP_BACKGROUND_MSG) {
                //APP切换后台
                //开启悬浮窗
                if (!isOnResume && AppUtils.isApplicationInBackground(LiveRoomActivity.this)) {
                    startFloatWindowService();
                }
                return;
            }
            if (msg.what == SEND_JUMP_OTHER_PAGE_MSG) {
                //跳转其他页面时候
                //开启悬浮窗
                if (isOnResume) {
                    //如果页面onResume了,就不开启service
                    return;
                }
                startFloatWindowService();
                return;
            }

            if (msg.what == OPEN_SHOP_CAR_DIALOG) {
                showShopCarDialog();
                return;
            }
        }
    };
    private RongIMClient.ConnectionStatusListener connectStatusListener = new RongIMClient.ConnectionStatusListener() {
        @Override
        public void onChanged(ConnectionStatus status) {
            Log.i("pq", "LiveRoomActivity receive ConnectionStatus:" + status);
            if (status == ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT
                    || status == ConnectionStatus.SIGN_OUT || status == ConnectionStatus.TIMEOUT) {
                finish();
            }
            if (status == ConnectionStatus.CONNECTED) {
                //IM连接成功
                if (presenter != null && isNeedRefresh) {
                    connectStatusIsPaused = false;
                    presenter.init(mRoomId, false);
                }
            } else {
                //其他状态的时候需要重新刷新房间信息
                isNeedRefresh = true;
            }
            if (status == ConnectionStatus.SUSPEND) {
                connectStatusIsPaused = true;
            }
        }
    };
    private LiveRoomClosePageOnMicTipDialog mLiveRoomClosePageOnMicTipDialog;
    private VideoViewManager mVideoViewManager;
    private LiveRoomShopCarDialog mShopCarDialog;

    public static void startLiveRoomActivity(Context context, String roomid) {
        Intent intent = new Intent(context, LiveRoomActivity.class);
        intent.putExtra(ROOMID_KEY, roomid);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        content_view = viewBinding.contentView;
        mFlLiveView = viewBinding.flLiveView;
        ll_like = viewBinding.llLike;
        mRecyclerMsg = viewBinding.recyclerMsg;
        mTvNumPeople = viewBinding.tvNumPeople;
        iv_mic = viewBinding.ivMic;
        mLlMicContainer = viewBinding.llMicContainer;
        mTvAddLikeCount = viewBinding.tvAddLikeCount;
        mIvAvatar = viewBinding.ivAvatar;
        mTvRoomCreateName = viewBinding.tvRoomAuthorName;
        mGroupViews = viewBinding.groupViews;
        mViewLiveStatus = viewBinding.viewLiveStatus.csRootLiveStatus;
        mIvLiveStatusCenterIcon = viewBinding.viewLiveStatus.ivCenterIcon;
        mTvLiveStatusTipText = viewBinding.viewLiveStatus.tvLiveStatusTipText;
        mIvSwitchVideoOrientation = viewBinding.ivSwitchVideoOrientation;
        mIvSwitchVideoOrientationFull = viewBinding.ivSwitchVideoOrientationFull;
        cs_header_info = viewBinding.csHeaderInfo;
        mViewShopCarAnim = viewBinding.viewShopCarAnim;
        mIvShopCar = viewBinding.ivShopCar;
        mIvClose = viewBinding.ivClose;
        tv_input = viewBinding.tvInput;
        mFlJoinBarrage = viewBinding.flJoinBarrage;
        mTvJoinBarrage = viewBinding.tvJoinBarrage;
        mFlSnapUpBarrage = viewBinding.flSnapUpBarrage;
        mTvSnapUpBarrage = viewBinding.tvSnapUpBarrage;
        initListener();
    }

    private void initListener() {
        mIvClose.setOnClickListener(this);
        mIvSwitchVideoOrientation.setOnClickListener(this);
        mIvSwitchVideoOrientationFull.setOnClickListener(this);
        tv_input.setOnClickListener(this);
        iv_mic.setOnClickListener(this);
        viewBinding.iconAddLike.setOnClickListener(this);
        mViewLiveStatus.setOnClickListener(this);
        mViewShopCarAnim.setOnClickListener(this);
        mFlLiveView.setOnClickListener(this);
        viewBinding.tvAppointmentCourse.setOnClickListener(this);
        viewBinding.tvLiveCourse.setOnClickListener(this);
        viewBinding.tvVideoCourse.setOnClickListener(this);
        viewBinding.tvMusicSheet.setOnClickListener(this);
    }

    @Override
    protected ActivityLiveroomLayoutBinding getLayoutView() {
        return ActivityLiveroomLayoutBinding.inflate(getLayoutInflater());
    }


    @Override
    public void initData() {
        super.initData();
        mRoomId = getIntent().getStringExtra(ROOMID_KEY);
        if (TextUtils.isEmpty(mRoomId)) {
            ToastUtil.getInstance().show(this, "房间id不可为空");
            finish();
            return;
        }
        String userToken = UserHelper.getUserToken();
        if (TextUtils.isEmpty(userToken)) {
            finish();
            return;
        }
        if (LiveConfig.isNeedReInitRTC) {
            //检查是否需要重新初始化RTC
            LiveRTCEngineInitHelper.initRTC();
        }
        // 初始化音频路由管理类
        RCRTCAudioRouteManager.getInstance().init(Utils.getApp());
        mFlLiveView.post(new Runnable() {
            @Override
            public void run() {
                checkVideoViewManager();
            }
        });
        mUserId = UserHelper.getUserId();
        mMessageAdapter = new MessageAdapter(this);
        mLinearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        mRecyclerMsg.setLayoutManager(mLinearLayoutManager);
        mRecyclerMsg.setAdapter(mMessageAdapter);
        IMCenter.getInstance().addConnectionStatusListener(connectStatusListener);
        SoftKeyboardUtil.registerSoftInputChangedListener(getWindow(), this);
        mLlMicContainer.setOnEventListener(new LiveRoomMicIconView.OnEventListener() {
            @Override
            public void getUserInfoByUserId(String userId) {
                if (presenter != null) {
                    presenter.getUserInfoByUserId(userId);
                }
            }
        });
        checkNeedPermission();
    }

    private void checkNeedPermission() {
        Disposable disposable = new RxPermissions(LiveRoomActivity.this)
                .request(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE
                        , Manifest.permission.CAMERA)
                .subscribe(permission -> {
                    if (permission) {
                        prepareInitRoom();
                    } else {
                        CommonConfirmDialog confirmDialog = new CommonConfirmDialog(LiveRoomActivity.this);
                        confirmDialog.show();
                        confirmDialog.setContent("直播需要麦克风、摄像头、储存权限，去设置？");
                        confirmDialog.setConfirmText("去设置");
                        confirmDialog.setOnConfirmClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                PermissionUtils.toSelfSetting(LiveRoomActivity.this);
                            }
                        });
                    }
                });
    }

    private void prepareInitRoom() {
        //根据融云的IM连接状态判断是否需要获取房间信息
        RongIMClient.ConnectionStatusListener.ConnectionStatus currentConnectionStatus = RongIMClient.getInstance().getCurrentConnectionStatus();
        if (currentConnectionStatus == RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED) {
            isNeedRefresh = false;
            if (presenter != null) {
                presenter.init(mRoomId, false);
            }
        } else {
            //如果IM未连接,等待connectStatusListener回调
            isNeedRefresh = true;
            if (presenter != null) {
                presenter.connectIM();
            }
        }
    }


    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.view_live_status) {
            return;
        }
        if (id == R.id.iv_close) {
            if (currentSeatStatus != LiveRoomMsgConstants.MIC_STATUS_NORMAL) {
                showPageCloseOnMicTipDialog();
            } else {
                close();
            }
        }
        if (id == R.id.iv_switch_video_orientation) {
            //切换横竖屏
            if (mShopCarDialog != null && mShopCarDialog.isShowing()) {
                return;
            }
            changeOrientation();
            return;
        }

        if (id == R.id.iv_switch_video_orientation_full) {
            //横屏切换竖屏
            changeOrientation();
            return;
        }
        if (id == R.id.tv_input) {
            //底部输入框
            showInputDialog();
            return;
        }
        if (id == R.id.iv_mic) {
            //连麦
            if (checkMicMode()) {
                return;
            }
            if (connectStatusIsPaused) {
                //连接状态异常,暂停连麦
                return;
            }
            if (currentSeatStatus == LiveRoomMsgConstants.MIC_STATUS_NORMAL) {
                //未连麦状态 申请连麦
                if (presenter != null) {
                    presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_SEAT_APPLY);
                }
                updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_CONNECTING);
                ToastUtil.getInstance().show(LiveRoomActivity.this, getString(R.string.seat_apply_str));
            } else {
                //取消连麦
                showCloseMicTipDialog();
            }
            return;
        }

        if (id == R.id.icon_add_like) {
            if (checkAddLikeMode()) {
                return;
            }
            //点赞
            LiveRoomAddLikeHelper.getInstance().handleClick(new LiveRoomAddLikeHelper.OnAddLikeResultCallBack() {
                @Override
                public void onResult(int count) {
                    if (presenter != null) {
                        presenter.setAddLikeCount(count);
                        presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_ADD_LIKE_COUNT);
                    }
                }
            });
            LiveRoomAnimatorHelper.getInstance().startAddLikeAnimation(LiveRoomActivity.this, content_view);
            return;
        }

        if (id == R.id.view_shop_car_anim) {
            if (viewBinding.llShopCarMenu.getVisibility() != View.VISIBLE) {
                viewBinding.llShopCarMenu.setVisibility(View.VISIBLE);
            } else {
                viewBinding.llShopCarMenu.setVisibility(View.GONE);
            }
            return;
        }

        if (id == R.id.fl_live_view) {
            //点击隐藏头部信息
            if (isFullScreen()) {
                //全屏模式下
                handleHeaderAnim();
            }
            return;
        }

        if (id == R.id.tv_appointment_course || id == R.id.tv_live_course
                || id == R.id.tv_video_course || id == R.id.tv_music_sheet) {
            //约课 直播课 视频课 曲谱
            startTeacherHomePage(id);
            sendShowFloatWindowAction(SEND_JUMP_OTHER_PAGE_MSG);
            return;
        }
    }

    private void startTeacherHomePage(int viewId) {
        String params = "";
        if (viewId == R.id.tv_appointment_course) {
            params = "practice";
        }
        if (viewId == R.id.tv_live_course) {
            params = "live";
        }
        if (viewId == R.id.tv_video_course) {
            params = "video";
        }
        if (viewId == R.id.tv_music_sheet) {
            params = "music";
        }
        ARouter.getInstance()
                .build(RouterPath.WebCenter.ACTIVITY_HTML)
                .withString(WebConstants.WEB_URL, WebConstants.STUDENT_TEACHER_HOME + mRoomInfoBean.speakerId + "&tabs=" + params)
                .navigation();
    }


    private void handleHeaderAnim() {
        float animOffset = cs_header_info.getTranslationY();
        if (animOffset == 0) {
            //隐藏头部信息
            int bottom = cs_header_info.getBottom();
            if (hideHeaderInfoAnim == null) {
                hideHeaderInfoAnim = ObjectAnimator.ofFloat(cs_header_info, "translationY", 0, -bottom);
                hideHeaderInfoAnim.setDuration(500);
            }
            hideHeaderInfoAnim.start();
        } else {
            //显示头部信息
            if (showHeaderInfoAnim == null) {
                showHeaderInfoAnim = ObjectAnimator.ofFloat(cs_header_info, "translationY", animOffset, 0);
                showHeaderInfoAnim.setDuration(500);
            }
            showHeaderInfoAnim.start();
        }
    }

    private void showShopCarDialog() {
        if (mShopCarDialog == null) {
            mShopCarDialog = new LiveRoomShopCarDialog(this);
            mShopCarDialog.setOnEventListener(new LiveRoomShopCarDialog.OnEventListener() {
                @Override
                public void onOpenDetail(String url) {
                    mShopCarDialog.dismiss();
                    //发送XXX正在抢购消息
//                    presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_ON_SNAP_UP);
//                    sendShowFloatWindowAction(SEND_JUMP_OTHER_PAGE_MSG);
//                    ARouter.getInstance()
//                            .build(ARouterConstace.ACTIVITY_HTML)
//                            .withString(ARouterConstace.URL, url)
//                            .withString(ARouterConstace.TITLE, "商品详情")
//                            .navigation();
                }
            });
        }
        if (!mShopCarDialog.isShowing()) {
            mShopCarDialog.show();
        }
        mShopCarDialog.setRoomId(mRoomId);
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i("pq", "onResume");
        checkImConnectStatus();
        this.isOnResume = true;
        unbindService();
        //取消全部静音
        muteAll(false);
    }


    /**
     * 检查IM连接状态
     */
    private void checkImConnectStatus() {
        RongIMClient.ConnectionStatusListener.ConnectionStatus currentConnectionStatus = RongIMClient.getInstance().getCurrentConnectionStatus();
        if (currentConnectionStatus == RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED) {
            isNeedRefresh = false;
        } else {
            isNeedRefresh = true;
            if (presenter != null) {
                presenter.connectIM();
            }
        }
    }


    private void showPageCloseOnMicTipDialog() {
        if (mLiveRoomClosePageOnMicTipDialog == null) {
            mLiveRoomClosePageOnMicTipDialog = new LiveRoomClosePageOnMicTipDialog(this);
            mLiveRoomClosePageOnMicTipDialog.setConfirmClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    close();
                }
            });
        }
        if (!mLiveRoomClosePageOnMicTipDialog.isShowing()) {
            mLiveRoomClosePageOnMicTipDialog.show();
        }
        mLiveRoomClosePageOnMicTipDialog.setTitle("提示");
        mLiveRoomClosePageOnMicTipDialog.setContent("连麦中,是否退出房间?");
    }

    private void showCloseMicTipDialog() {
        if (mRoomCloseMicTipDialog == null) {
            mRoomCloseMicTipDialog = new LiveRoomCloseMicTipDialog(this);
            mRoomCloseMicTipDialog.setConfirmClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleCloseMicEvent();
                    mRoomCloseMicTipDialog.dismiss();
                }
            });
        }
        if (!mRoomCloseMicTipDialog.isShowing()) {
            mRoomCloseMicTipDialog.show();
        }
        mRoomCloseMicTipDialog.setContent(getString(currentSeatStatus == LiveRoomMsgConstants.MIC_STATUS_CONNECTING ? R.string.cancel_seat_on_connecting : R.string.cancel_seat_on_connected));
    }

    /**
     * 取消连麦
     */
    private void handleCloseMicEvent() {
        if (currentSeatStatus == LiveRoomMsgConstants.MIC_STATUS_CONNECTING) {
            //连接中取消连接
            if (presenter != null) {
                presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_CANCEL_SEAT_APPLY);
                updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_NORMAL);
            }
        }
        if (currentSeatStatus == LiveRoomMsgConstants.MIC_STATUS_CONNECT_SUCCESS) {
            if (presenter != null) {
                presenter.exitMic();
            }
        }
    }

    private void updateMicIcon(int micStatus) {
        //主线程更新
        iv_mic.post(new Runnable() {
            @Override
            public void run() {
                currentSeatStatus = micStatus;
                if (micStatus == LiveRoomMsgConstants.MIC_STATUS_NORMAL) {
                    iv_mic.setImageResource(R.drawable.icon_mic_unconnect);
                }
                if (micStatus == LiveRoomMsgConstants.MIC_STATUS_CONNECTING) {
                    iv_mic.setImageResource(R.drawable.icon_mic_connecting);
                }
                if (micStatus == LiveRoomMsgConstants.MIC_STATUS_CONNECT_SUCCESS) {
                    iv_mic.setImageResource(R.drawable.icon_mic_conected);
                }
            }
        });
    }

    /**
     * 检查点赞按钮是否可用
     *
     * @return
     */
    private boolean checkAddLikeMode() {
        if (liveIsFinish) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, "直播已结束");
            return true;
        }
        if (connectStatusIsPaused) {
            //连接状态异常,暂停点赞
            ToastUtil.getInstance().show(LiveRoomActivity.this, "网络连接状态异常");
            return true;
        }
        return false;
    }


    private void showInputDialog() {
        if (checkChatMode()) return;
        if (mInputBarDialog == null) {
            mInputBarDialog = new InputBarDialog(LiveRoomActivity.this, new InputBar.InputBarListener() {
                @Override
                public boolean onClickSend(String message) {
                    //发送消息
                    if (TextUtils.isEmpty(message)) {
                        ToastUtil.getInstance().show(LiveRoomActivity.this, "消息不能为空");
                        return false;
                    }
                    if (message.length() > LiveConfig.LIVE_MAX_INPUT_TEXT_LENGTH) {
                        ToastUtil.getInstance().show(LiveRoomActivity.this, "聊天消息需在40个字以内哦");
                        return false;
                    }
                    if (LiveMessageHelper.isQuickAction()) {
                        ToastUtil.getInstance().show(LiveRoomActivity.this, "您说话太快啦");
                        return false;
                    }
                    sendTextMessage(message);
                    return true;
                }


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

            mInputBarDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    if (mInputBarDialog != null) {
                        mInputBarDialog.showInput();
                    }
                }
            });
        }
        if (!mInputBarDialog.isShowing()) {
            mInputBarDialog.show();
        }
    }

    private void sendTextMessage(String message) {
        if (checkChatMode()) {
            //禁止聊天模式
            if (mInputBarDialog != null) {
                mInputBarDialog.dismiss();
            }
            return;
        }
        if (presenter != null) {
            TextMessage textMessage = TextMessage.obtain(message);
            UserInfo userInfo = new UserInfo(mUserId, UserHelper.getUserName(), null);
            textMessage.setUserInfo(userInfo);
            presenter.sendMessage(textMessage);
        }
    }

    private void changeOrientation() {
        int currentOrientation = getCurrentOrientation();
        boolean isNeedFullScreen = currentOrientation != Configuration.ORIENTATION_LANDSCAPE;
        if (isNeedFullScreen) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //改变videoView容器布局尺寸
        resetVideoContainer(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
    }

    private void close() {
        //断开连接,结束页面
        onBackPressed();
    }

    @Override
    protected LiveRoomPresenter createPresenter() {
        return new LiveRoomPresenter();
    }


    /**
     * 获取房间信息成功
     *
     * @param roomInfoBean
     */
    @Override
    public void getRoomInfoSuccess(LiveRoomInfoBean roomInfoBean) {
        if (roomInfoBean == null || isFinishing() || isDestroyed()) {
            return;
        }
        mRoomInfoBean = roomInfoBean;
        isPcClientLive = TextUtils.equals(mRoomInfoBean.os, "pc");
        resetVideoContainer(!isPcClientLive);
        currentAddLikeCount = roomInfoBean.likeNum;
        if (!TextUtils.isEmpty(roomInfoBean.roomConfig)) {
            try {
                JSONObject jsonObject = new JSONObject(roomInfoBean.roomConfig);
                int chatCtrlMode = jsonObject.optInt("whether_chat", 0);
                int micCtrlMode = jsonObject.optInt("whether_mic", 0);
                isEnableChat = chatCtrlMode == 1;
                isEnableMic = micCtrlMode == 1;
                //购物车是否显示
                int shopCarMode = jsonObject.optInt("whether_view_shop_cart", 0);
                if (shopCarMode == 1) {
                    //隐藏购物车
                    mViewShopCarAnim.setVisibility(View.GONE);
                    mIvShopCar.setVisibility(View.GONE);
                } else {
                    //显示购物车
                    mViewShopCarAnim.setVisibility(View.VISIBLE);
                    mIvShopCar.setVisibility(View.INVISIBLE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        //黑名单模式
        isEnableAll = roomInfoBean.blacklistFlag == 1;//1是黑名单 0否
        updateInputTip();
        updateAddLikeCountView();
        //更新未上麦状态
        updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_NORMAL);
        if (mTvNumPeople != null) {
            int peopleCount = roomInfoBean.lookNum + 1;//加上自己
            mTvNumPeople.setText(peopleCount + "人");
        }

        if (mIvAvatar != null) {
            //创建人头像
            Glide.with(LiveRoomActivity.this).load(roomInfoBean.speakerPic).placeholder(R.drawable.icon_teacher_default_head).error(R.drawable.icon_teacher_default_head).into(mIvAvatar);
        }
        if (mTvRoomCreateName != null) {
            //创建人昵称
            mTvRoomCreateName.setText(roomInfoBean.speakerName);
        }

        if (mMessageAdapter != null) {
            mMessageAdapter.setRoomAuthorId(roomInfoBean.speakerId);
        }
        if (presenter != null) {
            //加入聊天室
            //加入直播间
            showLoading();
            LiveEventHelper.getInstance().register(mRoomInfoBean.roomUid);
            presenter.leaveRoom(mRoomInfoBean.roomUid, false, true);
            presenter.joinChartRoom(mRoomInfoBean.roomUid, new IRongCoreCallback.OperationCallback() {
                @Override
                public void onSuccess() {
                    Log.i("pq", "加入聊天房间成功");
                    if (presenter != null) {
                        presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_JOIN_ROOM);
                    }
                }

                @Override
                public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {
                    Log.i("pq", "加入聊天房间fail:" + coreErrorCode);
                    Toast.makeText(LiveRoomActivity.this, "加入聊天房间fail:" + coreErrorCode, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private void updateInputTip() {
        if (tv_input == null) {
            return;
        }
        if (isEnableAll) {
            tv_input.setText(getString(R.string.unable_input_tip_str));
        } else {
            tv_input.setText(getString(R.string.live_input_tips_str));
        }
    }

    @Override
    public void getRoomInfoError(Throwable t) {
        if (isDestroyed() || isFinishing()) {
            return;
        }
        if (t instanceof ApiException) {
            ApiException apiException = (ApiException) t;
            ToastUtil.getInstance().show(LiveRoomActivity.this, apiException.getErrmsg());
        }
        finish();
    }

    private void resetVideoContainer(boolean isFullScreen) {
        if (mVideoViewManager != null) {
            mVideoViewManager.setIsNeedFillScreen(isFullScreen);
        }
        if (isFullScreen) {
            if (isPcClientLive) {
                //隐藏消息列表
                mGroupViews.setVisibility(View.GONE);
                mIvClose.setVisibility(View.GONE);
                //隐藏弹幕消息
                mFlJoinBarrage.clearAnimation();
                mFlJoinBarrage.setVisibility(View.GONE);
                mFlSnapUpBarrage.clearAnimation();
                mFlSnapUpBarrage.setVisibility(View.GONE);
                //隐藏竖屏全屏按钮
                mIvSwitchVideoOrientation.setVisibility(View.GONE);
                mIvSwitchVideoOrientationFull.setVisibility(View.VISIBLE);
            } else {
                //不是PC端直播
                mIvSwitchVideoOrientation.setVisibility(View.GONE);
                mIvSwitchVideoOrientationFull.setVisibility(View.GONE);
            }
            ConstraintSet set = new ConstraintSet();
            set.clone(content_view);
            set.clear(mFlLiveView.getId());
            if (!isPcClientLive) {
                //如果是非PC端直播,还需要设置下消息列表控件位置
                set.clear(R.id.fl_recycler_container);
                set.connect(R.id.fl_recycler_container, ConstraintSet.TOP, R.id.view_center, ConstraintSet.BOTTOM, SizeUtils.dp2px(50));
                set.connect(R.id.fl_recycler_container, ConstraintSet.LEFT, content_view.getId(), ConstraintSet.LEFT, 0);
                set.connect(R.id.fl_recycler_container, ConstraintSet.RIGHT, content_view.getId(), ConstraintSet.RIGHT, 0);
                set.connect(R.id.fl_recycler_container, ConstraintSet.BOTTOM, tv_input.getId(), ConstraintSet.TOP, SizeUtils.dp2px(10));
            }
            set.connect(mFlLiveView.getId(), ConstraintSet.TOP, content_view.getId(), ConstraintSet.TOP, 0);
            set.connect(mFlLiveView.getId(), ConstraintSet.LEFT, content_view.getId(), ConstraintSet.LEFT, 0);
            set.connect(mFlLiveView.getId(), ConstraintSet.RIGHT, content_view.getId(), ConstraintSet.RIGHT, 0);
            set.connect(mFlLiveView.getId(), ConstraintSet.BOTTOM, content_view.getId(), ConstraintSet.BOTTOM, 0);
            set.applyTo(content_view);
        } else {
            //pc端直播,控制居中显示,以及videoview容器的宽高
            if (cs_header_info.getTranslationY() != 0) {
                //还原显示头部信息
                handleHeaderAnim();
            }
            if (isPcClientLive) {
                mGroupViews.setVisibility(View.VISIBLE);
                mIvClose.setVisibility(View.VISIBLE);
                mIvSwitchVideoOrientation.setVisibility(View.VISIBLE);
                mIvSwitchVideoOrientationFull.setVisibility(View.GONE);
            }

            ConstraintSet set = new ConstraintSet();
            set.clone(content_view);
            set.clear(mFlLiveView.getId());
            set.connect(mFlLiveView.getId(), ConstraintSet.TOP, cs_header_info.getId(), ConstraintSet.BOTTOM, SizeUtils.dp2px(121));
            set.connect(mFlLiveView.getId(), ConstraintSet.LEFT, content_view.getId(), ConstraintSet.LEFT, 0);
            set.connect(mFlLiveView.getId(), ConstraintSet.RIGHT, content_view.getId(), ConstraintSet.RIGHT, 0);
            int maxHeightAtRatio16_9 = UiUtils.getMaxHeightAtRatio16_9(LiveRoomActivity.this);
            set.constrainHeight(mFlLiveView.getId(), maxHeightAtRatio16_9);
            set.applyTo(content_view);
        }
    }

    public void changVideoViewSize() {
        int childCount = mFlLiveView.getChildCount();
        if (childCount == 0) {
            return;
        }
        View childAt = mFlLiveView.getChildAt(0);
        if (childAt instanceof RCRTCVideoView) {
            RCRTCVideoView videoView = (RCRTCVideoView) childAt;
//            mFlLiveView.removeView(videoView);
//            mFlLiveView.addView(videoView);
        }
    }

    @Override
    public void showFinishView() {
        //显示结束view
        if (isFinishing() || isDestroyed()) {
            return;
        }
        Log.i("pq", "加入直播间异常");
    }


    /**
     * 主讲人的音频状态
     *
     * @param isCloseAudioStream
     */
    @Override
    public void syncAudioStatus(boolean isCloseAudioStream) {
        this.creatorIsCloseAudioStream = isCloseAudioStream;
        if (creatorIsCloseAudioStream && creatorIsCloseVideoStream) {
            //如果是关闭音频,同时判断下视频的状态
            showRestView();
        }
    }

    @Override
    public void showRestView() {
        showLiveStatusView(LIVE_STATUS_IS_REST);
    }

    @Override
    public void showCloseVideoView() {
        //关闭了视频
        creatorIsCloseVideoStream = true;
        if (creatorIsCloseAudioStream) {
            //同时音频也关闭了
            showRestView();
            return;
        }
        showLiveStatusView(LIVE_STATUS_IS_CLOSE_VIDEO);
    }

    private void showLiveStatusView(int status) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (mViewLiveStatus == null) {
            return;
        }
        //显示视频画面已经关闭页面
        mViewLiveStatus.post(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                mViewLiveStatus.setVisibility(View.VISIBLE);
                if (status == LIVE_STATUS_IS_REST) {
                    mIvLiveStatusCenterIcon.setImageResource(R.drawable.icon_live_room_rest_bg);
                    mTvLiveStatusTipText.setText("休息一下，马上回来！！");
                    mTvLiveStatusTipText.setTextSize(12);
                    mTvLiveStatusTipText.setBackgroundResource(com.cooleshow.base.R.drawable.shape_gray_14dp);
                } else if (status == LIVE_STATUS_IS_CLOSE_VIDEO) {
                    mIvLiveStatusCenterIcon.setImageResource(R.drawable.icon_live_room_close_video);
                    mTvLiveStatusTipText.setText("主持人已关闭画面！");
                    mTvLiveStatusTipText.setTextSize(12);
                    mTvLiveStatusTipText.setBackgroundResource(com.cooleshow.base.R.drawable.shape_gray_14dp);
                } else if (status == LIVE_STATUS_IS_OFFLINE) {
                    mIvLiveStatusCenterIcon.setImageResource(R.drawable.icon_live_end_tip);
                    mTvLiveStatusTipText.setText("直播已结束！");
                    mTvLiveStatusTipText.setTextSize(14);
                    mTvLiveStatusTipText.setBackgroundColor(Color.TRANSPARENT);
                }
            }
        });
    }

    @Override
    public void showEmptyStatusView() {
        //显示老师不在线view
        showRestView();
    }

    @Override
    public View getContentView() {
        return content_view;
    }

    @Override
    public void addMessageList(List<MessageContent> messageContents, boolean isReset) {
        //添加多条消息至页面
    }

    @Override
    public void addMessageContent(Message message, boolean isReset) {
        //添加单条消息至页面
        Log.i("pq", "收到需要显示msg:" + message);
        //只处理直播间消息,以及本直播间消息
        if (mMessageAdapter != null) {
            mMessageAdapter.addMessage(message);
            if (mRecyclerMsg != null && mLinearLayoutManager != null) {
                mRecyclerMsg.post(new Runnable() {
                    @Override
                    public void run() {
                        mRecyclerMsg.scrollToPosition(mMessageAdapter.getItemCount() - 1);
//                        mRecyclerMsg.scrollToPosition(mMessageAdapter.getItemCount() - 1);
//                        mLinearLayoutManager.scrollToPositionWithOffset(mMessageAdapter.getItemCount() - 1, 0);
                    }
                });
            }
        }
    }

    private void updateAddLikeCountView() {
        if (mTvAddLikeCount != null) {
            mTvAddLikeCount.setText(getString(R.string.live_room_add_like_count_str, currentAddLikeCount));
        }
    }

    /**
     * 控制聊天模式
     *
     * @param isEnableChat
     */
    @Override
    public void switchChatMode(boolean isEnableChat) {
        //控制聊天模式
        Log.i("pq", "聊天禁止:" + isEnableChat);
        this.isEnableChat = isEnableChat;
    }

    @Override
    public void liveRoomOffline() {
        //关闭直播间
        ToastUtil.getInstance().show(this, "直播已结束");
        this.liveIsFinish = true;
        //取消连麦
        handleCloseMicEvent();
        notifyMicDelAll();
        showLiveStatusView(LIVE_STATUS_IS_OFFLINE);
        hideDialog();
        //离开房间
        LiveEventHelper.getInstance().leaveRoom(null);
    }

    @Override
    public void liveGoodsChange(String jsonStr) {
        //直播间商品发生变化
        if (isDestroyed() || isFinishing()) {
            return;
        }
        if (mShopCarDialog != null) {
            mShopCarDialog.changeGoods(jsonStr);
        }
    }

    /**
     * 关闭一些交互弹窗
     */
    private void hideDialog() {
        //关闭邀请弹窗
        if (mInviteSeatMicTipDialog != null && mInviteSeatMicTipDialog.isShowing()) {
            mInviteSeatMicTipDialog.dismiss();
        }
        //关闭商品弹窗
        if (mShopCarDialog != null && mShopCarDialog.isShowing()) {
            mShopCarDialog.dismiss();
        }
    }

    /**
     * @param rcUserSeatResponseMessage
     */
    @Override
    public void seatResponse(RCUserSeatResponseMessage rcUserSeatResponseMessage) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (rcUserSeatResponseMessage == null || !isOwn(rcUserSeatResponseMessage.getAudienceId())) {
            return;
        }
        if (rcUserSeatResponseMessage != null) {
            int type = rcUserSeatResponseMessage.getType();
            if (type == LiveRoomMsgConstants.MIC_RESPONSE_AGREE) {
                //主播同意
                if (presenter != null) {
                    presenter.joinMic();
                }
                return;
            }
            if (type == LiveRoomMsgConstants.MIC_RESPONSE_DISAGREE) {
                //主播拒绝
                updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_NORMAL);
                ToastUtil.getInstance().show(LiveRoomActivity.this, getString(R.string.create_refuse_seat_tip));
                return;
            }
        }
    }

    @Override
    public void onSwitchRole(String userId, RCRTCLiveRole role) {
        Log.i("pq", "onSwitchRole:" + userId);
        if (role.getType() == RCRTCLiveRole.AUDIENCE.getType()) {
            //主播身份切换观众身份 下麦
            notifyMicContainerDel(userId);
            return;
        }
    }

    @Override
    public void onUserJoinRoom(RCRTCRemoteUser rcrtcRemoteUser) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        Log.i("pq", "onUserJoinRoom:" + rcrtcRemoteUser.getUserId());
        if (TextUtils.equals(rcrtcRemoteUser.getUserId(), createRoomId)) {
            createRoomId = "";//重置一下createRoomId,防止创建者退出又进入,全局控制变量未刷新
        }
    }

    @Override
    public void onUserLeftRoomMic(RCRTCRemoteUser rcrtcRemoteUser) {
        Log.i("pq", "onUserLeftRoomMic:" + rcrtcRemoteUser.getUserId());
        if (rcrtcRemoteUser != null) {
            notifyMicContainerDel(rcrtcRemoteUser.getUserId());
        }
    }

    @Override
    public void onUserOfflineRoomMic(RCRTCRemoteUser rcrtcRemoteUser) {
        Log.i("pq", "onUserOfflineRoomMic:" + rcrtcRemoteUser.getUserId());
        if (rcrtcRemoteUser != null) {
            notifyMicContainerDel(rcrtcRemoteUser.getUserId());
        }
    }

    @Override
    public void onRemoteUserPublishResource(String remoteUserId) {
        if (!TextUtils.isEmpty(remoteUserId)) {
            Log.i("pq", "收到onRemoteUserPublishResource:" + remoteUserId);
            if (mRoomInfoBean != null && TextUtils.equals(mRoomInfoBean.speakerId, remoteUserId)) {
                if (presenter != null) {
                    presenter.subscribeAVStream();
                }
            } else {
                refreshAudio(null, null);
            }
        }
    }

    @Override
    public void syncMemberCount(String count) {
        //同步成员数量
        Log.i("pq", "syncMemberCount" + count);
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (mTvNumPeople != null) {
            mTvNumPeople.setText(count + "人");
        }
    }

    @Override
    public void onUserKickOutMsg(RCUserKickOutMessage userKickOutMessage) {
        //用户被踢出消息
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (userKickOutMessage == null || !isOwn(userKickOutMessage.getTargetId())) {
            return;
        }
        String targetId = userKickOutMessage.getTargetId();
        Log.i("pq", "receive kickOut msg targetId:" + targetId);
        finish();
    }

    /**
     * 控制连麦模式
     *
     * @param isEnableMic
     */
    @Override
    public void changeMicControlMode(boolean isEnableMic) {
        //禁止连麦模式
        if (isFinishing() || isDestroyed()) {
            return;
        }
        Log.i("pq", "receive mic mode control msg 禁麦模式:" + isEnableMic);
        this.isEnableMic = isEnableMic;
        if (isEnableMic) {
            handleCloseMicEvent();
            if (mLlMicContainer != null) {
                mLlMicContainer.post(new Runnable() {
                    @Override
                    public void run() {
                        mLlMicContainer.delAll();
                    }
                });
            }
        }
    }

    private void refresh() {
        //video流信息
        List<RCRTCVideoOutputStream> outputStreams = new ArrayList<>();
        List<RCRTCVideoInputStream> videoInputStreams = new ArrayList<>();
        //音频流信息
        List<RCRTCAudioInputStream> audioinputstream = new ArrayList<>();
        List<RCRTCAudioOutputStream> audioOutputStreams = new ArrayList<>();
        presenter.getAllStreams(outputStreams, videoInputStreams, audioinputstream, audioOutputStreams);
        updateVideoView(outputStreams, videoInputStreams, audioinputstream, audioOutputStreams);
        refreshAudio(audioinputstream, audioOutputStreams);
    }

    @Override
    public void subscribeAVStreamSuccess(List<RCRTCInputStream> inputStreams) {
        if (isDestroyed() || isFinishing()) {
            return;

        }
        if (liveIsFinish) {
            //直播已结束
            return;
        }
        if (inputStreams != null) {
            refresh();
        } else {
            showEmptyStatusView();
        }
    }

    @Override
    public void onSubscribeFailed() {
        //订阅流失败
    }

    @Override
    public void setRoomData(LiveRoomInfoBean roomInfoBean) {
        //保存房间信息
        if (presenter != null) {
            //通知加入房间成功
            presenter.notifyJoinRoomAction(mRoomId, mUserId);
        }
    }

    /**
     * 清除所有连麦用户的UI
     */
    private void notifyMicDelAll() {
        if (mLlMicContainer != null) {
            mLlMicContainer.post(new Runnable() {
                @Override
                public void run() {
                    mLlMicContainer.delAll();
                }
            });
        }
    }

    private void notifyMicContainerDel(String userId) {
        mLlMicContainer.post(new Runnable() {
            @Override
            public void run() {
                mLlMicContainer.deleteMicUser(new UserInfo(userId, "", null));
            }
        });
    }

    /**
     * 连麦成功-身份切换主播成功
     */
    @Override
    public void onSeatMicSuccess() {
        updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_CONNECT_SUCCESS);
    }

    /**
     * 连麦失败
     */
    @Override
    public void onSeatMicFail() {
        updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_NORMAL);
    }

    /**
     * \
     * 退出连麦成功
     */
    @Override
    public void onExitSeatMicSuccess() {
        updateMicIcon(LiveRoomMsgConstants.MIC_STATUS_NORMAL);
        if (presenter != null) {
            presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_DOWN_SEAT_MIC);
        }
    }

    /**
     * 主播邀请上麦
     *
     * @param seatApplyMessage
     */
    @Override
    public void onInviteSeatMic(RCUserSeatApplyMessage seatApplyMessage) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (seatApplyMessage == null || !isOwn(seatApplyMessage.getAudienceId())) {
            return;
        }
        if (seatApplyMessage.getType() == LiveRoomMsgConstants.MIC_ACTION_INVITE_SEAT_BY_CREATE) {
            //主讲人邀请观众上麦
            showInviteSeatDialog();
            return;

        }
        if (seatApplyMessage.getType() == LiveRoomMsgConstants.MIC_ACTION_CANCEL_INVITE_SEAT_BY_CREATE) {
            //主讲人取消邀请观众上麦
            if (mInviteSeatMicTipDialog != null) {
                mInviteSeatMicTipDialog.dismiss();
            }
            ToastUtil.getInstance().show(LiveRoomActivity.this, getString(R.string.create_recall_seat_invite_tip));
            return;
        }

        if (seatApplyMessage.getType() == LiveRoomMsgConstants.MIC_ACTION_CANCEL_SEAT_BY_CREATE) {
            //主讲人讲观众抱下麦
            ToastUtil.getInstance().show(LiveRoomActivity.this, getString(R.string.down_mic_by_create_tip));
            if (presenter != null) {
                presenter.exitMic();
            }
        }
    }

    @Override
    public void onUserUnusualLogout(RCUserLogOutUnusualMessage loginOutUnusualMessage) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (loginOutUnusualMessage == null || !isOwn(loginOutUnusualMessage.getUserId())) {
            return;
        }
        Log.i("pq", "unusual logout");
        finish();
    }

    /**
     * 暂停直播,进行下麦处理
     */
    @Override
    public void onLivePause() {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        ToastUtil.getInstance().show(this, getString(R.string.live_is_pause_str));
        handleCloseMicEvent();
    }

    /**
     * 点赞数量同步
     *
     * @param addLikeMessage
     */
    @Override
    public void onAddLikeMessage(Message message, RCUserSyncAddLikeCountMessage addLikeMessage) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (TextUtils.equals(message.getSenderUserId(), mUserId)) {
            return;
        }
        if (addLikeMessage != null) {
            if (mTvAddLikeCount != null && addLikeMessage != null) {
//                int counts = addLikeMessage.getCounts();
//                currentAddLikeCount += counts;
                currentAddLikeCount = addLikeMessage.getCount();
                updateAddLikeCountView();
            }
        }
    }

    /**
     * 获取上麦用户信息成功
     *
     * @param friendInfoBean
     */
    @Override
    public void getFriendInfoSuccess(FriendInfoBean friendInfoBean) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (mLlMicContainer != null && friendInfoBean != null) {
            mLlMicContainer.updateTargetUserInfo(new UserInfo(friendInfoBean.friendId, friendInfoBean.friendNickname, null));
        }
    }

    @Override
    public void onPublishSuccess() {
        refresh();
    }


    /**
     * 黑名单状态切换
     *
     * @param userId
     * @param isAddBlack
     */
    @Override
    public void changeBlackUserStatus(String userId, boolean isAddBlack) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (isOwn(userId)) {
            isEnableAll = isAddBlack;
            if (isEnableAll) {
                //取消连麦
                if (currentSeatStatus != LiveRoomMsgConstants.MIC_STATUS_NORMAL) {
                    handleCloseMicEvent();
                }
            }
            updateInputTip();
        }
    }

    private boolean isOwn(String targetUserId) {
        return TextUtils.equals(mUserId, targetUserId);
    }

    private void showInviteSeatDialog() {
        if (mInviteSeatMicTipDialog == null) {
            mInviteSeatMicTipDialog = new LiveRoomInviteSeatMicTipDialog(this);
            mInviteSeatMicTipDialog.setViewClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getId() == R.id.tv_sure) {
                        //被邀请连麦->同意连麦
                        if (presenter != null) {
                            presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_CANCEL_SEAT_AGREE_RESPONSE);
                            presenter.joinMic();
                        }
                    }
                    if (v.getId() == R.id.tv_cancel) {
                        //被邀请连麦->拒绝连麦
                        if (presenter != null) {
                            presenter.handleAction(LiveRoomMsgConstants.ACTION_SEND_CANCEL_SEAT_DISAGREE_RESPONSE);
                        }
                        //重置一下连麦状态
                        handleCloseMicEvent();
                    }
                    mInviteSeatMicTipDialog.dismiss();
                }
            });
        }
        if (!mInviteSeatMicTipDialog.isShowing()) {
            mInviteSeatMicTipDialog.show();
        }
        String defaultNick = "主讲人";
        if (mRoomInfoBean != null && !TextUtils.isEmpty(mRoomInfoBean.speakerName)) {
            defaultNick = mRoomInfoBean.speakerName;
        }
        mInviteSeatMicTipDialog.setContent(defaultNick);
    }

    private void refreshAudio(List<RCRTCAudioInputStream> audioInputStreams, List<RCRTCAudioOutputStream> audioOutputStreams) {
        if (audioInputStreams == null || audioOutputStreams == null) {
            //参数为空,先获取房间里的全部音频流信息
            audioInputStreams = new ArrayList<>();
            audioOutputStreams = new ArrayList<>();
            presenter.getAudioStreams(audioInputStreams, audioOutputStreams);
        }
        ArrayList<String> onMicUserIds = new ArrayList<>();
        //先处理本地用户
        for (int i = 0; i < audioOutputStreams.size(); i++) {
            RCRTCAudioOutputStream audioInputStream = audioOutputStreams.get(i);
            if (audioInputStream.getMediaType() == RCRTCMediaType.AUDIO) {
                //音频流判断显示上麦用户
                String userId = audioInputStream.getUserId();
                if (mRoomInfoBean != null) {
                    if (!TextUtils.equals(mRoomInfoBean.speakerId, userId)) {
                        onMicUserIds.add(userId);
                    }
                }
            }
        }

        //远端用户的音频流信息
        for (int i = 0; i < audioInputStreams.size(); i++) {
            RCRTCAudioInputStream rcrtcAudioInputStream = audioInputStreams.get(i);
            if (rcrtcAudioInputStream.getMediaType() == RCRTCMediaType.AUDIO) {
                //音频流判断显示上麦用户
                String userId = rcrtcAudioInputStream.getUserId();
                if (mRoomInfoBean != null) {
                    if (!TextUtils.equals(mRoomInfoBean.speakerId, userId)) {
                        onMicUserIds.add(userId);
                    }
                }
            }
        }
        mLlMicContainer.refreshUI(onMicUserIds);
    }

    public void updateVideoView(List<RCRTCVideoOutputStream> outputStreams, List<RCRTCVideoInputStream> inputStreams, List<RCRTCAudioInputStream> audioInputStreams, List<RCRTCAudioOutputStream> audioOutputStreams) {
        mFlLiveView.post(new Runnable() {
            @Override
            public void run() {
                ArrayList<RCRTCVideoStream> videoStreams = new ArrayList<RCRTCVideoStream>();
                videoStreams.addAll(outputStreams);
                videoStreams.addAll(inputStreams);
                ArrayList<RCRTCVideoView> list = new ArrayList<>();
                boolean isNormalCreateAudioStatus = true;
                //视频流
                for (int i = 0; i < videoStreams.size(); i++) {
                    RCRTCVideoStream rcrtcVideoStream = videoStreams.get(i);
                    RCRTCVideoView rongRTCVideoView = new RCRTCVideoView(LiveRoomActivity.this);
                    rcrtcVideoStream.setVideoView(rongRTCVideoView);
                    list.add(rongRTCVideoView);
                }

                //音频流
                for (int i = 0; i < audioInputStreams.size(); i++) {
                    RCRTCAudioInputStream audioInputStream = audioInputStreams.get(i);
                    if (audioInputStream.getMediaType() == RCRTCMediaType.AUDIO) {
                        //音频流判断显示上麦用户
                        String userId = audioInputStream.getUserId();
                        if (mRoomInfoBean != null) {
                            if (TextUtils.equals(mRoomInfoBean.speakerId, userId)) {
                                //找出主播的音频流
                                RCRTCResourceState resourceState = audioInputStream.getResourceState();
                                isNormalCreateAudioStatus = resourceState == RCRTCResourceState.NORMAL;
                            }
                        }
                    }
                }
                if (list.size() != 0) {
                    creatorIsCloseVideoStream = false;
                    mViewLiveStatus.setVisibility(View.GONE);
                    checkVideoViewManager();
                    mVideoViewManager.update(list, isPcClientLive, true);
                } else {
                    creatorIsCloseVideoStream = true;
                    if (isNormalCreateAudioStatus) {
                        showCloseVideoView();
                    } else {
                        //主播关闭了视频流,同时音频流的状态也不是normal状态时
                        showRestView();
                    }
                }
            }
        });
    }

    /**
     * 检查VideoViewManager是否未null;
     */
    private void checkVideoViewManager() {
        if (mVideoViewManager == null) {
            mVideoViewManager = new VideoViewManager(mFlLiveView, mFlLiveView.getWidth(), mFlLiveView.getHeight());
            mVideoViewManager.setRetryClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (presenter != null) {
                        //重新订阅房间的流信息
                        presenter.subscribeAVStream();
                    }
                }
            });
        }
    }

    private boolean findMainScreenStreams(String streamUserId, String streamTag) {
        if (TextUtils.equals("screenshare", streamTag)) {
            //屏幕共享流
            return true;
        }
        if (TextUtils.equals(mRoomInfoBean.speakerId, streamUserId)) {
            return true;
        }
        return false;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i("LiveRoomActivity", "onSaveInstanceState");
    }

    @Override
    public void onBackPressed() {
        if (liveIsFinish) {
            super.onBackPressed();
            return;
        }
        boolean permissionOverlay = FloatWindowHelper.requestOverlayPermission(this);
        if (!permissionOverlay) {
            showOpenOverlayPermissionTipDialog();
        } else {
            if (isFullScreen()) {
                changeOrientation();
            } else {
                showExitLiveTipDialog();
            }
        }
    }

    /**
     * 退出直播间提示弹窗
     */
    private void showExitLiveTipDialog() {
        LiveRoomCloseTipDialog closeTipDialog = new LiveRoomCloseTipDialog(this);
        closeTipDialog.setConfirmClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        closeTipDialog.show();
    }

    private void showOpenOverlayPermissionTipDialog() {
        OpenOverlayPermissionTipDialog overlayPermissionTipDialog = new OpenOverlayPermissionTipDialog(this);
        overlayPermissionTipDialog.setConfirmClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overlayPermissionTipDialog.dismiss();
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, FloatWindowHelper.REQUEST_OVERLAY_CODE);
            }
        });
        overlayPermissionTipDialog.show();
        overlayPermissionTipDialog.setCancelClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overlayPermissionTipDialog.dismiss();
                finish();
            }
        });
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
//        String str =   savedInstanceState.getString("key");
        Log.i("LiveRoomActivity", "onRestoreInstanceState");
    }


    private int getCurrentOrientation() {
        return getResources().getConfiguration().orientation;
    }

    private boolean isFullScreen() {
        return getCurrentOrientation() == Configuration.ORIENTATION_LANDSCAPE;
    }


    private void startFloatWindowService() {
        boolean hasPermission = FloatWindowHelper.requestOverlayPermission(this);
        if (hasPermission) {
            if (mVideoViewManager != null && mVideoViewManager.getAllVideoViews() != null && mVideoViewManager.getAllVideoViews().size() > 0) {
                Intent intent = new Intent(this, FloatingWindowService.class);
                initServiceConnection();
                intent.putExtra("isPc", isPcClientLive);
                bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
            }
        }
    }

    private void initServiceConnection() {
        if (serviceConnection == null) {
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    if (service instanceof FloatingWindowService.MyBinder) {
                        FloatingWindowService.MyBinder mBinder = (FloatingWindowService.MyBinder) service;
                        if (mVideoViewManager != null) {
                            ArrayList<RCRTCVideoView> allVideoViews = mVideoViewManager.getAllVideoViews();
                            mBinder.addVideoView(allVideoViews);
                        }
                        mBinder.setOnEventListener(new FloatingWindowService.OnEventListener() {
                            @Override
                            public void onServiceDestroy() {
                                if (mVideoViewManager != null) {
                                    mVideoViewManager.update(mVideoViewManager.getAllVideoViews(), isPcClientLive, false);
                                }
                            }

                            @Override
                            public void onUnbindService() {
                                muteAll(true);
                                unbindService();
                            }
                        });
                    }
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    if (mVideoViewManager != null) {
                        mVideoViewManager.update(mVideoViewManager.getAllVideoViews(), isPcClientLive, false);
                    }
                }
            };
        }
    }

    /**
     * 是否静音全部
     *
     * @param isMute
     */
    private void muteAll(boolean isMute) {
        if (LiveEventHelper.getInstance().getRtcRoom() != null) {
            RCRTCRoom rtcRoom = LiveEventHelper.getInstance().getRtcRoom();
            if (rtcRoom.getLocalUser() != null) {
                LiveEventHelper.getInstance().getRtcRoom().muteAllRemoteAudio(isMute);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 销毁悬浮窗service
     */
    private void unbindService() {
        if (serviceConnection != null) {
            try {
                boolean isExist = AppUtils.isServiceWork(this, FloatingWindowService.CLASS_PATH);
                if (isExist) {
                    unbindService(serviceConnection);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        isOnResume = false;
        sendShowFloatWindowAction(SEND_APP_BACKGROUND_MSG);
    }

    private void sendShowFloatWindowAction(int what) {
        android.os.Message message = android.os.Message.obtain();
        message.what = what;
        mHandler.sendMessageDelayed(message, SEND_SHOW_FLOAT_WINDOW_TIME);
    }

    /**
     * 检查聊天模式
     */
    public boolean checkChatMode() {
        if (liveIsFinish) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, "直播已结束");
            return true;
        }
        if (isEnableAll) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, "您已被管理员禁言");
            return true;
        }
        if (isEnableChat) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, "禁止聊天模式");
            return true;
        }
        return false;
    }

    /**
     * 检查mic模式
     */
    public boolean checkMicMode() {
        if (liveIsFinish) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, "直播已结束");
            return true;
        }
        if (isEnableAll) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, "您已被管理员禁言");
            return true;
        }
        if (isEnableMic) {
            ToastUtil.getInstance().show(LiveRoomActivity.this, getString(R.string.enable_mic_mode_tip));
            return true;
        }
        return false;
    }

    @Override
    public void receiveJoinMessage(RCChatJoinRoomMessage joinRoomMessage) {
        //收到加入房间消息
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (joinRoomMessage == null) {
            return;
        }
        if (isCanShowBarrage()) {
            mTvJoinBarrage.setText(getString(R.string.enter_live_tip_str, LiveMemberHelper.getMessageName(joinRoomMessage)));
            showBarrageViewAnim(mFlJoinBarrage);
        }
    }

    /**
     * 显示弹幕消息
     */
    private void showBarrageViewAnim(View targetView) {
        if (mFlJoinBarrage.getVisibility() == View.VISIBLE || mFlSnapUpBarrage.getVisibility() == View.VISIBLE) {
            return;
        }
        LiveRoomAnimatorHelper.getInstance().startBarrageViewAnimation(this, targetView);
        hideBarrageView();
    }

    /**
     * 隐藏弹幕消息
     */
    private void hideBarrageView() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mFlJoinBarrage != null) {
                    mFlJoinBarrage.setVisibility(View.GONE);
                }
                if (mFlSnapUpBarrage != null) {
                    mFlSnapUpBarrage.setVisibility(View.GONE);
                }
            }
        }, LiveConfig.LIVE_HIDE_BARRAGE_VIEW_TIME);
    }

    @Override
    public void receiveSnapUpMessage(RCOnSnappingUpMessage onSnappingUpMessage) {
        //收到抢购消息
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (onSnappingUpMessage == null) {
            return;
        }
        if (isCanShowBarrage()) {
            mTvSnapUpBarrage.setText(getString(R.string.live_snap_up_tip_str, LiveMemberHelper.getMessageName(onSnappingUpMessage)));
            showBarrageViewAnim(mFlSnapUpBarrage);
        }
    }

    /**
     * 判断是否显示弹幕消息
     *
     * @return
     */
    private boolean isCanShowBarrage() {
        if (isFullScreen()) {
            return false;
        }
        return mFlJoinBarrage.getVisibility() != View.VISIBLE && mFlSnapUpBarrage.getVisibility() != View.VISIBLE;
    }

    @Override
    public void onDestroy() {
        if (presenter != null) {
            //通知离开房间
            presenter.notifyLeaveRoomAction();
        }
        super.onDestroy();
        if (connectStatusListener != null) {
            IMCenter.getInstance().removeConnectionStatusListener(connectStatusListener);
        }
        //取消动画
        if (hideHeaderInfoAnim != null) {
            hideHeaderInfoAnim.cancel();
            hideHeaderInfoAnim = null;
        }

        if (showHeaderInfoAnim != null) {
            showHeaderInfoAnim.cancel();
            showHeaderInfoAnim = null;
        }
        SoftKeyboardUtil.unregisterSoftInputChangedListener(getWindow());
        LiveRoomAnimatorHelper.getInstance().releaseAnimator();
        LiveRoomAddLikeHelper.getInstance().release();
        LiveEventHelper.getInstance().unRegister();
        RCRTCAudioRouteManager.getInstance().unInit();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        if (mVideoViewManager != null) {
            mVideoViewManager.onRelease();
        }
    }

    @Override
    public void onSoftInputChanged(int height) {
        Log.i("pq", "height" + height);
        if (height == 0) {
            //软键盘隐藏
            Log.i("pq", "SoftInput hide");
            if (mInputBarDialog != null && mInputBarDialog.isShowing()) {
                mInputBarDialog.dismiss();
            }
        } else {
            //软键盘弹出
            Log.i("pq", "SoftInput show");
        }
    }
}
