package com.ez.ertc.meeting.presenter;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.ez.basertc.RTCConstant;
import com.ez.basertc.RTCListener;
import com.ez.baselib.utils.LogUtil;
import com.ez.ertc.meeting.ErtcEzmHelper;
import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.bean.ErtcAudioStatusBean;
import com.ez.ertc.meeting.constant.ExtraKeys;
import com.ez.ertc.meeting.constant.SharingType;
import com.ez.ertc.meeting.event.MtAccountNotifyEvent;
import com.ez.ertc.meeting.event.MtAudioModeEvent;
import com.ez.ertc.meeting.event.MtExitRoomEvent;
import com.ez.ertc.meeting.event.MtMemberEnterEvent;
import com.ez.ertc.meeting.event.MtMemberExitEvent;
import com.ez.ertc.meeting.ui.ErtcRoomViewHolder;
import com.ez.ertc.meeting.ui.JoinMeetingActivity;
import com.ez.ertc.meeting.utils.ErtcNetLevelUtils;
import com.ez.ertc.meeting.widget.ErtcTextureView;
import com.ez.ertcmeeting.bean.DeviceState;
import com.ez.ertcmeeting.bean.UserAccount;
import com.ez.network.BaseError;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.ez.ertc.meeting.ui.ErtcRoomActivity.AUDIO_LEVEL_OFF;

public class ErtcRoomEventListener extends RTCListener {
    private final String TAG = "ErtcRoomEventImpl";
    private ErtcAudioStatusCallbackImpl ertcAudioStatusCallback;
    private long mStartUpTime;
    private WeakReference<ErtcRoomPresenter> mReference;
//    private DelayQueue<DelayedElement> mDelayQueueEnter,mDelayQueueExit;//延时队列，每条消息间隔 delay ms

//    private static class DelayedElement implements Delayed {
//        private static final long delay = 30; //延迟时间ms 必须在风远回调之后
//        private long expire;  //到期时间
//        private UserAccount account;
//
//        public DelayedElement(UserAccount account) {
//            this.account = account;
//            expire = System.currentTimeMillis() + delay;    //到期时间 = 当前时间+延迟时间
//        }
//
//        @Override
//        public long getDelay(TimeUnit unit) {
//            return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
//        }
//
//        @Override
//        public int compareTo(Delayed o) {
//            return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
//        }
//    }

    public ErtcRoomEventListener(ErtcRoomPresenter roomPresenter) {
        this.mReference = new WeakReference(roomPresenter);
        ertcAudioStatusCallback = new ErtcAudioStatusCallbackImpl(mReference.get());
//        mDelayQueueEnter = new DelayQueue<DelayedElement>();
//        mDelayQueueExit = new DelayQueue<DelayedElement>();
    }

    @Override
    public void onEnterRoomSuccess() {
        super.onEnterRoomSuccess();
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener localUser加入房间成功");
        final ErtcRoomPresenter roomPresenter = mReference.get();
        if (roomPresenter == null) {
            return;
        }
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                roomPresenter.toggleMicro(roomPresenter.isAudioOn? DeviceState.DS_Active : DeviceState.DS_Closed);
                roomPresenter.toggleCamera(roomPresenter.isVideoOn? DeviceState.DS_Active : DeviceState.DS_Closed);
            }
        });
    }

    @Override
    public void onExitRoom(int reason) {
        super.onExitRoom(reason);
        final ErtcRoomPresenter roomPresenter = mReference.get();
        if (roomPresenter.mSharingDesktopUserId.equals(roomPresenter.mMyAccount.getUserId())) {
            roomPresenter.stopRecording();
        }

        switch (reason) {
            case RTCConstant.ERTC_EXIT_REASON_BAN: //离开房间
                JoinMeetingActivity.start(ErtcEzmHelper.getContext(), ExtraKeys.ACTION_MEETING_END);
                break;
            case RTCConstant.ERTC_EXIT_REASON_DISSOLVE://房间解散
                roomPresenter.release("onNotifyKickOut" + reason);
                JoinMeetingActivity.start(ErtcEzmHelper.getContext(), ExtraKeys.ACTION_KICK_OUT);
                break;
        }
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onExitRoom reason:" + reason);
    }

    @SuppressLint("NewApi")
    @Override
    public void onRemoteUserEnterRoom(final String userId) {
        super.onRemoteUserEnterRoom(userId);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onRemoteUserEnterRoom RemoteUser加入房间成功 userId:"+ userId);

        if (mStartUpTime == 0) {
            mStartUpTime = System.currentTimeMillis();
        }
        final ErtcRoomPresenter roomPresenter = mReference.get();
        if (roomPresenter == null) {
            return;
        }
        final UserAccount account = new UserAccount();
        account.setUserId(userId);

        if(roomPresenter.isErtcSharingScreen){
            account.setVideoStatus(DeviceState.DS_Closed.getValue());
        }

        synchronized (ErtcRoomPresenter.sAccountList){
            boolean isAdd = true;
            Iterator<UserAccount> iterator = ErtcRoomPresenter.sAccountList.iterator();
            while (iterator.hasNext()){
                UserAccount a = iterator.next();
                if (account.getUserId().equals(a.getUserId())){
                    isAdd = false;
                    break;
                }
            }
            if(isAdd) {
                ErtcRoomPresenter.sAccountList.add(account);
            }
        }

        if (roomPresenter.mIsShowMemberToast && System.currentTimeMillis() - mStartUpTime > 2000) {
            roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    roomPresenter.mView.showToast(String.format(roomPresenter.mView.getActivity().getResources().getString(R.string.ys_mt_room_toast_join_format), account.getUserId()));
                }
            });
        }

        final int size = ErtcRoomPresenter.sAccountList.size();
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onRemoteUserEnterRoom size:" + size);
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                roomPresenter.mView.updateMemberCount(size + 1);
                roomPresenter.mGroupView.showGroupView();
                if (size == 1) {
                    //notifyDataChanged需在setScreenAccount之前
//                    roomPresenter.mGroupView.notifyDataChanged(ErtcRoomPresenter.sAccountList);
                    roomPresenter.setScreenAccount(account);
                } else if (size <= ErtcRoomViewHolder.PAGE_SIZE) {
                    LogUtil.d("EZRTCImpl", "self bindView onNotifyEnter size<=3 account:" + userId);
//                    roomPresenter.mGroupView.notifyDataChanged(ErtcRoomPresenter.sAccountList);
                    if (!roomPresenter.mSharingDesktopUserId.isEmpty() && roomPresenter.mSharingDesktopUserId.equals(account.getUserId())) {
                        roomPresenter.setScreenAccount(account);
                    } else {
                        roomPresenter.setScreenAccount(roomPresenter.getScreenAccount());
                    }
//                    roomPresenter.mGroupView.updateBtnVisibility();
                } else {
                    LogUtil.d("EZRTCImpl", "self bindView onNotifyEnter size>3 account:" + userId);
                    if (!roomPresenter.mSharingDesktopUserId.isEmpty() && roomPresenter.mSharingDesktopUserId.equals(account.getUserId())) {
                        roomPresenter.setScreenAccount(account);
                    } else {
                        roomPresenter.setScreenAccount(roomPresenter.getScreenAccount());
                    }
                    roomPresenter.mGroupView.updateBtnVisibility();
                }
                MtMemberEnterEvent event = new MtMemberEnterEvent(account);
                EventBus.getDefault().post(event);
            }
        });
    }

    @Override
    public void onRemoteUserLeaveRoom(final String userId, int reason) {
        super.onRemoteUserLeaveRoom(userId, reason);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener 离开房间:"+ userId);
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.accountVideoStateMap.remove(userId);
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                UserAccount leaveAccount= roomPresenter.getAccountByUserId(userId);
                if(leaveAccount != null) {
                    if (roomPresenter.mIsShowMemberToast && System.currentTimeMillis() - mStartUpTime > 2000) {
                        roomPresenter.mView.showToast(String.format(roomPresenter.mView.getActivity().getResources().getString(R.string.ys_mt_room_toast_leave_format), userId));
                    }
                    roomPresenter.mNamesMap.remove(userId);
                    roomPresenter.mGroupView.notifyExit(leaveAccount);
                    LogUtil.e("EZRTCImpl", "updateMemberCount onRemoteUserLeaveRoom count:" + (ErtcRoomPresenter.sAccountList.size() + 1));
                    roomPresenter.mView.updateMemberCount(ErtcRoomPresenter.sAccountList.size() + 1);
                    MtMemberExitEvent event = new MtMemberExitEvent(leaveAccount.getUserId());
                    EventBus.getDefault().post(event);
                    if (roomPresenter.mMonitorDialog != null) {
                        roomPresenter.mMonitorDialog.removeRemoteAudioStats(userId);
                        roomPresenter.mMonitorDialog.removeRemoteVideoStats(userId);
                    }
                    LogUtil.d("EZRTCImpl", "isErtcSharingScreen:" + roomPresenter.isErtcSharingScreen + " userId:" + userId + " mSharingAccountId:" + roomPresenter.mSharingAccountId);
                    if(userId.equals(roomPresenter.mSharingAccountId)) {
                        roomPresenter.onErtcShareModeNone(false, false);
                        roomPresenter.mSharingType = SharingType.ST_None;
                        roomPresenter.isErtcSharingScreen = false;
                        if (roomPresenter.mMonitorDialog != null) {
                            roomPresenter.mMonitorDialog.onSharingTypeChanged(SharingType.ST_None, TextUtils.equals(userId, roomPresenter.mMyAccount.getUserId()));
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onFirstLocalVideoFrame(int width, int height) {
        super.onFirstLocalVideoFrame(width, height);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onFirstLocalVideoFrame self res[" + width +" * " + height + "]");
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ErtcTextureView ertcTextureView = roomPresenter.mGroupView.findTextureView(roomPresenter.mMyAccount.getUserId());
                if(ertcTextureView != null) {
                    ertcTextureView.hideProgress();
                }
                if(roomPresenter.mMyAccount.getUserId().equals(roomPresenter.mScreenAccount.getUserId())){
                    roomPresenter.mView.getScreenTextureView().hideProgress();
                }
            }
        });
    }

    @Override
    public void onFirstRemoteVideoFrame(final String userId, int width, int height) {
        super.onFirstRemoteVideoFrame(userId, width, height);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onFirstRemoteVideoFrame userId["+userId+"] res["+width+" * "+height+"]");
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final UserAccount account = roomPresenter.getAccountByUserId(userId);
                if(account != null){
                    ErtcTextureView ertcTextureView = roomPresenter.mGroupView.findTextureView(account.getUserId());
                    if(ertcTextureView != null) {
                        ertcTextureView.hideProgress();
                    }
                }

                if(userId.equals(roomPresenter.mScreenAccount.getUserId())){
                    roomPresenter.mView.getScreenTextureView().hideProgress();
                }
            }
        });
    }

    @Override
    public void onUserVideoAvailable(final String userId, final boolean available, final int streamType) {
        super.onUserVideoAvailable(userId, available, streamType);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onUserVideoAvailable:"+ userId + " available:" + available + " streamType:" + streamType);
        final ErtcRoomPresenter roomPresenter = mReference.get();

        for (UserAccount account : ErtcRoomPresenter.sAccountList) {
            if (userId.equals(account.getUserId())) {
                account.setVideoStatus(available ? DeviceState.DS_Active.getValue() : DeviceState.DS_Closed.getValue());
                break;
            }
        }
        if (userId.equals(roomPresenter.mScreenAccount.getUserId())) {
            roomPresenter.mScreenAccount.setVideoStatus(available ? DeviceState.DS_Active.getValue() : DeviceState.DS_Closed.getValue());
        }
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
//                if(streamType == RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL) {
//                    roomPresenter.mGroupView.notifyItemChanged(roomPresenter.getAccountByUserId(userId));
//                } else {
                    roomPresenter.updateAccountView(roomPresenter.getAccountByUserId(userId));
//                }
                if(!available){
                    if (roomPresenter.mMonitorDialog != null) {
                        roomPresenter.mMonitorDialog.removeRemoteVideoStats(userId);
                    }
                }
            }
        });
        EventBus.getDefault().post(new MtAccountNotifyEvent(roomPresenter.getAccountByUserId(userId)));
    }

    @Override
    public void onUserAudioAvailable(final String userId, final boolean available) {
        super.onUserAudioAvailable(userId, available);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onUserAudioAvailable:用户" + userId + "声音" + available);

        for (UserAccount account : ErtcRoomPresenter.sAccountList) {
            if (userId.equals(account.getUserId())) {
                account.setAudioStatus(available ? DeviceState.DS_Active.getValue() : DeviceState.DS_Closed.getValue());
                break;
            }
        }
        final ErtcRoomPresenter roomPresenter = mReference.get();
        if (userId.equals(roomPresenter.mScreenAccount.getUserId())) {
            roomPresenter.mScreenAccount.setAudioStatus(available ? DeviceState.DS_Active.getValue() : DeviceState.DS_Closed.getValue());
        }
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(!available) {
                    if (userId.equals(roomPresenter.mScreenAccount.getUserId())) {
                        roomPresenter.mView.setScreenAudioLevel(AUDIO_LEVEL_OFF);
                    }
                    UserAccount account = roomPresenter.getAccountByUserId(userId);
                    if(account != null){
                        roomPresenter.mGroupView.setMemberAudioLevel(account.getUserId(), AUDIO_LEVEL_OFF);
                    }
                    if (roomPresenter.mMonitorDialog != null) {
                        roomPresenter.mMonitorDialog.removeRemoteAudioStats(userId);
                    }
                }
            }
        });
        EventBus.getDefault().post(new MtAccountNotifyEvent(roomPresenter.getAccountByUserId(userId)));
    }

    @Override
    public void onVolumeEvaluation(List<RTCConstant.AudioVolumeInfo> audioVolumeInfoList, int total) {
        super.onVolumeEvaluation(audioVolumeInfoList, total);
//        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onVolumeEvaluation audioVolumeInfoList total:" + total);
        if(ertcAudioStatusCallback != null) {
            synchronized (ErtcRoomPresenter.sAccountList) {
                List<ErtcAudioStatusBean> ertcAudioStatusBeanList = new ArrayList<>();
                for (RTCConstant.AudioVolumeInfo audioVolumeInfo : audioVolumeInfoList) {
                    if(audioVolumeInfo.userId.equals(mReference.get().mMyAccount.getUserId())) {
                        ErtcAudioStatusBean audioStatusBean = new ErtcAudioStatusBean(audioVolumeInfo.userId, audioVolumeInfo.volume);
                        ertcAudioStatusBeanList.add(audioStatusBean);
                    } else {
                        for (UserAccount account : ErtcRoomPresenter.sAccountList) {
                            if (audioVolumeInfo.userId.equals(account.getUserId())) {
                                ErtcAudioStatusBean audioStatusBean = new ErtcAudioStatusBean(audioVolumeInfo.userId, audioVolumeInfo.volume);
                                ertcAudioStatusBeanList.add(audioStatusBean);
                            }
                        }
                    }
                }
                ertcAudioStatusCallback.onAudioStatus(ertcAudioStatusBeanList);
            }
        }
    }

    @Override
    public void onNetworkQuality(final String uid, final int upNetQuality, final int downNetQuality) {
        super.onNetworkQuality(uid, upNetQuality, downNetQuality);

//        LogUtil.d("EZRTCImpl", "onNetworkQuality wifi level quality:" + quality + " uid:" + uid);
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //网络不好时大窗口切为小码流，网络好时大窗口切为大码流
                if(downNetQuality == RTCConstant.ERTC_QUALITY_DOWN) {
                    roomPresenter.mForceLowStream = true;
                } else {
                    if(roomPresenter.mForceLowStream){
                        roomPresenter.mForceLowStream = false;
                        roomPresenter.setHighStream(roomPresenter.getScreenAccount());
                    }
                }

                if(roomPresenter.mMyAccount.getUserId().equals(uid)){
                    // 本地上行极差关视频， 本地下行极差切语音
                    int localUpNetLevel = ErtcNetLevelUtils.getNetLevel(upNetQuality);
                    if(upNetQuality != roomPresenter.mMyAccount.getUpNetQuality() || downNetQuality != roomPresenter.mMyAccount.getDownNetQuality()) {
                        roomPresenter.mMyAccount.setUpNetQuality(upNetQuality);
                        roomPresenter.mMyAccount.setDownNetQuality(downNetQuality);
                        roomPresenter.mView.updateFlowViewLevel(localUpNetLevel);
                        roomPresenter.mGroupView.updateSelfDelayLevel(roomPresenter.mMyAccount.getUserId(), localUpNetLevel);

                        if(roomPresenter.mScreenAccount.getUserId().equals(uid)) {
                            if(upNetQuality != roomPresenter.mScreenAccount.getUpNetQuality() || downNetQuality != roomPresenter.mScreenAccount.getDownNetQuality()) {
                                roomPresenter.mScreenAccount.setUpNetQuality(upNetQuality);
                                roomPresenter.mScreenAccount.setDownNetQuality(downNetQuality);
                                roomPresenter.mView.updateScreenKpsLevel(localUpNetLevel);
                            }
                        }

                        switch (localUpNetLevel) {
                            case 4:
                                break;
                            case 3:
//                                roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_mid);
                                break;
                            case 2:
                                roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_bad);
                                break;
                            case 1:
                                if (roomPresenter.mMyAccount.getVideoStatus() == DeviceState.DS_Active.getValue()) {
                                    roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_very_bad_v);
                                    roomPresenter.toggleCamera(DeviceState.DS_Closed);
                                } else {
                                    roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_very_bad);
                                }
                                break;
                        }

                        int localDownNetLevel = ErtcNetLevelUtils.getNetLevel(downNetQuality);
                        switch (localDownNetLevel) {
                            case 4:
                                break;
                            case 3:
//                                roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_mid);
                                break;
                            case 2:
                                roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_bad);
                                break;
                            case 1:
                                if (!roomPresenter.isAudioMode) {
                                    roomPresenter.isAudioMode = true;
                                    EventBus.getDefault().post(new MtAudioModeEvent(roomPresenter.isAudioMode));
                                    roomPresenter.mView.showToast(R.string.ys_mt_room_down_stream_very_bad);
                                }
                                break;
                        }

                    }

                } else {
                    int remoteNetLevel = ErtcNetLevelUtils.getNetLevel(upNetQuality);
                    //仅在1V1时, 当A网络为【中】、【差】、【极差】时，toast提示另一方：“当前对方网络不佳 ”、“当前对方网络较差 ”、“当前对方网络极差 ”
                    if (ErtcRoomPresenter.sAccountList.size() == 1 && ErtcRoomPresenter.sAccountList.get(0).getUserId() == uid) {
                        if (upNetQuality != ErtcRoomPresenter.sAccountList.get(0).getUpNetQuality()) {
                            switch (remoteNetLevel) {
                                case 3:
//                                    roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_very_bad_b);
                                    break;
                                case 2:
                                    roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_bad_b);
                                    break;
                                case 1:
                                    roomPresenter.mView.showToast(R.string.ys_mt_room_toast_bitrate_level_mid_b);
                                    break;
                            }
                        }
                    }
                    UserAccount account = roomPresenter.getAccountByUserId(uid);
                    if(account != null) {
                        account.setUpNetQuality(upNetQuality);
                        account.setDownNetQuality(downNetQuality);
                        roomPresenter.mGroupView.updateSelfDelayLevel(account.getUserId(), remoteNetLevel);
                    }
                    if(roomPresenter.mScreenAccount.getUserId().equals(uid)) {
                        if(upNetQuality != roomPresenter.mScreenAccount.getUpNetQuality() || downNetQuality != roomPresenter.mScreenAccount.getDownNetQuality()) {
                            roomPresenter.mScreenAccount.setUpNetQuality(upNetQuality);
                            roomPresenter.mScreenAccount.setDownNetQuality(downNetQuality);
                            roomPresenter.mView.updateScreenKpsLevel(remoteNetLevel);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onError(final int errorCode) {
        super.onError(errorCode);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onError errorCode:" + errorCode);
        mReference.get().mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (errorCode == BaseError.ERR_OPERATION_SSL_HAND_SHAKE_ERROR) {
                    mReference.get().mView.showToast("SSL握手失败，可能网络不通！请重新入会。");
                } else if (errorCode == BaseError.ERR_OPERATION_UNKNOWN_HOST) {
                    mReference.get().mView.showToast("域名解析失败，可能网络不通！请重新入会。");
                } else if (errorCode == BaseError.ERR_OPERATION_NET_TIMEOUT) {
                    mReference.get().mView.showToast("连接超时，可能网络不通！请重新入会。");
                } else if (errorCode == 300009) {
                    mReference.get().mView.showToast("入会失败，请检查参数，并重新入会。");
                } else {
                    mReference.get().mView.showToast("errorCode:" + errorCode);
                }
                EventBus.getDefault().post(new MtExitRoomEvent());
            }
        });
    }

    @Override
    public void onWarning(int warnCode) {
        super.onWarning(warnCode);
    }

    @Override
    public void onShareStateChanged(final String userId, final boolean enable) {
        super.onShareStateChanged(userId, enable);
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.isErtcSharingScreen = enable;
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onShareStateChanged userId:" + userId + " enable:" + enable);
        roomPresenter.mSharingAccountId = enable ? userId : "";
        final boolean isSelfSharing = enable ? TextUtils.equals(userId, mReference.get().mMyAccount.getUserId()) : false;
//
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (roomPresenter.mMonitorDialog != null) {
                    roomPresenter.mMonitorDialog.onSharingTypeChanged(enable ? SharingType.ST_Desktop : SharingType.ST_None, TextUtils.equals(userId, roomPresenter.mMyAccount.getUserId()));
                }
                //1、找出正在共享的人的昵称，此昵称不会是自己，因为自己不在mAccountList中
                //2、被主持人停止共享，找出主持人的昵称
                UserAccount account = roomPresenter.getAccountByUserId(userId);
                roomPresenter.mSharingAccountId = account != null ? account.getUserId() : "";

                if(enable && account != null) {
                    roomPresenter.mSharingType = SharingType.ST_Desktop;
                    roomPresenter.onErtcShareModeDesktop(account.getUserId());
                    //发起和接收共享模式时，将本地（自己）视频关闭，开启点击开启时，toast提示“正在共享中，暂停使用视频”；
                    if (roomPresenter.mSharingType != SharingType.ST_None && roomPresenter.mMyAccount.getVideoStatus() == DeviceState.DS_Active.getValue()) {
                        roomPresenter.toggleCamera(DeviceState.DS_Closed);
                    }
//                    if (!roomPresenter.mSharingDesktopUserId.isEmpty() && !roomPresenter.mSharingDesktopUserId.equals(roomPresenter.mMyAccount.getUserId())) {
//                        roomPresenter.onShareConflict("onNotifyRoom===共享冲突，停止自己的录屏");
//                    }
                } else {
                    roomPresenter.mSharingType = SharingType.ST_None;
                    roomPresenter.onErtcShareModeNone(isSelfSharing, false);
                }
            }
        });
    }

    @Override
    public void onRecvCustomMsg(String userId, byte[] message, int messageSize) {
        super.onRecvCustomMsg(userId, message, messageSize);
        LogUtil.d("EZRTCImpl", "ErtcRoomEventListener onRecvCustomMsg userId:" + userId + " message:" + message.toString());
    }



    @Override
    public void onDisplayLoadingState(final String userId, final long unDisplayTime) {
        super.onDisplayLoadingState(userId, unDisplayTime);
        final ErtcRoomPresenter roomPresenter = mReference.get();
        roomPresenter.mView.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(roomPresenter.mScreenAccount.getUserId().equals(userId)){
                    ErtcTextureView screenTextureView = roomPresenter.mView.getScreenTextureView();
                    if(unDisplayTime > 2 * 1000) {
                        screenTextureView.showProgress();
                    } else {
                        screenTextureView.hideProgress();
                    }
                }
                UserAccount account = roomPresenter.getAccountByUserId(userId);
                if(account != null) {
                    ErtcTextureView groupTextureView = roomPresenter.mGroupView.findTextureView(account.getUserId());
                    if (groupTextureView != null) {
                        if(unDisplayTime > 2*1000) {
                            groupTextureView.showProgress();
                        } else {
                            groupTextureView.hideProgress();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onForceStopShareScreen() {
        super.onForceStopShareScreen();
        mReference.get().onErtcShareModeNone(true, false);
    }
}
