package com.tencent.liteav.live.ui;

import static com.tencent.liteav.live.ui.widget.page.MeetingPageLayoutManager.HORIZONTAL;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.DialogFragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.tencent.liteav.live.R;
import com.tencent.liteav.live.model.TRTCMeeting;
import com.tencent.liteav.live.model.TRTCMeetingCallback;
import com.tencent.liteav.live.model.TRTCMeetingDef;
import com.tencent.liteav.live.model.TRTCMeetingDelegate;
import com.tencent.liteav.live.ui.remote.RemoteUserListView;
import com.tencent.liteav.live.ui.utils.StateBarUtils;
import com.tencent.liteav.live.ui.widget.InputTextMsgDialog;
import com.tencent.liteav.live.ui.widget.RoomListDialog;
import com.tencent.liteav.live.ui.widget.RoomSettingDialog;
import com.tencent.liteav.live.ui.widget.base.ConfirmDialogFragment;
import com.tencent.liteav.live.ui.widget.feature.FeatureConfig;
import com.tencent.liteav.live.ui.widget.feature.FeatureSettingFragmentDialog;
import com.tencent.liteav.live.ui.widget.msg.TCChatEntity;
import com.tencent.liteav.live.ui.widget.msg.TCChatMsgListAdapter;
import com.tencent.liteav.live.ui.widget.msg.TCConstants;
import com.tencent.liteav.live.ui.widget.page.MeetingPageLayoutManager;
import com.tencent.liteav.live.ui.widget.page.PagerSnapHelper;
import com.tencent.liteav.live.ui.widget.settingitem.LinkListDialog;
import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudDef;
import com.wisewoods.base.app.BaseApp;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.RedisList;
import com.wisewoods.base.utils.$;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MeetingMainActivity extends AppCompatActivity implements TRTCMeetingDelegate, View.OnClickListener, InputTextMsgDialog.OnTextSendListener {
    private static final String TAG = MeetingMainActivity.class.getName();

    public static final String KEY_ROOM_ID = "room_id";
    public static final String KEY_USER_ID = "user_id";
    public static final String KEY_USER_NAME = "user_name";
    public static final String KEY_USER_AVATAR = "user_avatar";
    public static final String KEY_OPEN_CAMERA = "open_camera";
    public static final String KEY_OPEN_AUDIO = "open_audio";
    public static final String KEY_AUDIO_QUALITY = "audio_quality";
    public static final String KEY_VIDEO_QUALITY = "video_quality";
    public static final String KEY_ISFRONT_CAMERA = "is_front_camera";
    public static final String KEY_IS_WORK = "is_work";
    //    public static final  String TYPE_CREATE     = "create";
    //    private static final String KEY_ENTER_TYPE  = "start_type";
    //    private static final String TYPE_ENTER      = "enter";

    private String mRoomId;
    private String mUserId;
    private String mUserAvatar;
    private boolean mOpenCamera;
    private boolean mOpenAudio;
    private int mAudioQuality;
    private int mVideoQuality;
    private String mUserName;
    private boolean isCreating = false;
    private boolean mIsUserEnterMuteAudio = false; //后续人员进入都进入静音模式
    private boolean isFrontCamera = true;
    private boolean isWork = false;
    private boolean isUseSpeaker = true;
    private TRTCMeeting mTRTCMeeting;
    private List<MemberEntity> mMemberEntityList;
    private MemberListAdapter mMemberListAdapter;
    private Map<String, MemberEntity> mStringMemberEntityMap;

    private RecyclerView mListRv;
    private MeetingVideoView mViewVideo;
    private FrameLayout mContainerFl;
    private Button btnRotate;
    private MeetingHeadBarView mMeetingHeadBarView;
    //    private BeautyPanel                  mBeautyControl;
    private AppCompatImageButton mAudioImg;
    private AppCompatImageButton mVideoImg;
    //    private AppCompatImageButton mBeautyImg;
    private AppCompatImageButton mMessageImg;
    private AppCompatImageButton mMemberImg;
    private AppCompatImageButton mCloseImg;
    private AppCompatImageButton mMoreImg;
    private AppCompatImageButton mLinkImg;
    private ViewStub mStubRemoteUserView;
    private RemoteUserListView mRemoteUserView;
    private FeatureSettingFragmentDialog mFeatureSettingFragmentDialog; //更多设置面板
    private View mScreenCaptureGroup;
    private Group mBottomToolBarGroup;
    private TextView mStopScreenCaptureTv;
    private View mFloatingWindow;
    private List<MemberEntity> mVisibleVideoStreams;
    private String mShowUserId = "";
    private boolean isScreenCapture;


    /**
     * 消息列表相关
     */
    private ListView mLvMessage;             // 消息控件
    private InputTextMsgDialog mInputTextMsgDialog;    // 消息输入框
    private TCChatMsgListAdapter mChatMsgListAdapter;    // 消息列表的Adapter
    private ArrayList<TCChatEntity> mArrayListChatEntity;   // 消息内容
    private RoomSettingDialog roomSettingDialog;

    private ArrayList<TRTCMeetingDef.UserInfo> mLinkList;
    private ArrayList<RedisList.RedisData.DataBean> mInviteList;

    Runnable runnable = () -> {
        doTaskWrok();
    };
    private LinkListDialog linkListDialog;
    private RoomListDialog roomListDialog;

    private void doTaskWrok() {
        //检查是否有连麦超时
        if (mLinkList != null) {
            ArrayList<TRTCMeetingDef.UserInfo> outList = new ArrayList<>();
            for (TRTCMeetingDef.UserInfo userInfo : mLinkList) {
                if (TimeUtils.getTimeSpanByNow(userInfo.time, TimeConstants.SEC) < -30) {
                    outList.add(userInfo);
                }
            }
            mLinkList.removeAll(outList);
            if (outList.size() > 0) {
                //通知adapter更新数据
                if (linkListDialog != null) {
                    linkListDialog.refresh();
                }
            }

//            LogUtils.e("定时任务", mLinkList);
            if (mLinkList.size() > 0) {
                mLinkImg.setImageResource(R.drawable.ic_bell_red);
            } else {
                mLinkImg.setImageResource(R.drawable.ic_bell);
            }


            ArrayList<RedisList.RedisData.DataBean> outList2 = new ArrayList<>();
            for (RedisList.RedisData.DataBean dataBean : mInviteList) {
                if (TimeUtils.getTimeSpanByNow(dataBean.time, TimeConstants.SEC) < -30) {
                    outList2.add(dataBean);
                }
            }
            mInviteList.removeAll(outList2);
            if (outList2.size() > 0) {
                //通知adapter更新数据
                if (roomListDialog != null) {
                    roomListDialog.refresh();
                }
            }

        }
        BaseApp.mHandler.postDelayed(runnable, 1000);
//        LogUtils.e("定时任务继续");
    }

    public static void enterRoom(Context context,
//                                 int roomId,
                                 String roomId,
                                 String userId,
                                 String userName,
                                 String userAvatar,
                                 boolean openCamera,
                                 boolean openAudio,
                                 int audioQuality,
                                 int videoQuality,
                                 boolean isFrontCamera,
                                 boolean isWork
    ) {
        Intent starter = new Intent(context, MeetingMainActivity.class);
        starter.putExtra(KEY_ROOM_ID, roomId);
        starter.putExtra(KEY_USER_ID, userId);
        starter.putExtra(KEY_USER_NAME, userName);
        starter.putExtra(KEY_USER_AVATAR, userAvatar);
        starter.putExtra(KEY_OPEN_CAMERA, openCamera);
        starter.putExtra(KEY_OPEN_AUDIO, openAudio);
        starter.putExtra(KEY_AUDIO_QUALITY, audioQuality);
        starter.putExtra(KEY_VIDEO_QUALITY, videoQuality);
        starter.putExtra(KEY_ISFRONT_CAMERA, isFrontCamera);
        starter.putExtra(KEY_IS_WORK, isWork);
        context.startActivity(starter);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void groupInfoEvent(BaseEvent.GroupInfoEvent event) {
        if (roomSettingDialog != null) {
            roomSettingDialog.setData(event.key, event.isCheck);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void userEvent(BaseEvent.UserEvent event) {
        if (roomListDialog != null) {
            roomListDialog.change(event);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        // 应用运行时，保持不锁屏、全屏化
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        StateBarUtils.setDarkStatusBar(this);
        setContentView(R.layout.trtclive_activity_meeting_main);
        initData();
        mArrayListChatEntity = new ArrayList<>();
        mLinkList = new ArrayList<>();
        mInviteList = new ArrayList<>();
        BaseApp.mHandler.postDelayed(runnable, 1000);
        initView();

        $.requestLive(this, () -> {
            startCreateOrEnterMeeting();
        });
    }

    @Override
    public void onBackPressed() {
        if (mRemoteUserView != null && mRemoteUserView.isShown()) {
            mRemoteUserView.setVisibility(View.GONE);
            StateBarUtils.setDarkStatusBar(MeetingMainActivity.this);
            return;
        }
        preExitMeeting();
    }

    @Override
    protected void onDestroy() {
        hideFloatingWindow();
//        mBeautyControl.clear();
        mTRTCMeeting.setDelegate(null);
        mTRTCMeeting.stopScreenCapture();
//        mTRTCMeeting.stopCameraPreview();
        BaseApp.mHandler.removeCallbacks(runnable);
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (null != mBeautyControl && mBeautyControl.isShowing() ) {
//            mBeautyControl.hide();
//        }
        return super.dispatchTouchEvent(ev);
    }

    private void preExitMeeting() {
        String notifyMsg = "";
//        if (isCreating) {
//            notifyMsg = getString(R.string.trtclive_msg_exit_meeting);
//        } else {
        notifyMsg = getString(R.string.trtclive_msg_confirm_exit_meeting);
//        }
        showExitInfoDialog(notifyMsg, false);
    }

    private void exitMeetingConfirm() {
        if (isCreating) {
            mTRTCMeeting.destroyMeeting(mRoomId, null);
        } else {
            mTRTCMeeting.leaveMeeting(null);
        }
    }

    private void createMeeting() {
        int role = TRTCCloudDef.TRTCRoleAnchor;
        if (isWork) {
            role = TRTCCloudDef.TRTCRoleAudience;
        }
        int finalRole = role;
        mTRTCMeeting.createMeeting(mRoomId, finalRole, new TRTCMeetingCallback.ActionCallback() {
            @Override
            public void onCallback(int code, String msg) {
                if (code == 0) {
                    // 创建房间成功
                    isCreating = true;
//                    ToastUtils.showLong(getString(R.string.trtclive_toast_create_meeting_successfully));
                    mMeetingHeadBarView.setTitle("");
//                    mMeetingHeadBarView.setTitle(String.valueOf(mRoomId));
                    changeResolution();
                    return;
                }
                isCreating = false;
                // 会议创建不成功，进入会议
                mTRTCMeeting.enterMeeting(mRoomId, finalRole, new TRTCMeetingCallback.ActionCallback() {
                    @Override
                    public void onCallback(int code, String msg) {
                        if (code != 0) {
                            // 进房失败
                            ToastUtils.showShort(msg);
                            finish();
                        }
                        changeResolution();
                        mMeetingHeadBarView.setTitle("");
//                        mMeetingHeadBarView.setTitle(String.valueOf(mRoomId));
                    }
                });
            }
        });
    }

    private void startCreateOrEnterMeeting() {
        //设置默认状态
        FeatureConfig.getInstance().setRecording(false);
        FeatureConfig.getInstance().setAudioVolumeEvaluation(true);
        mTRTCMeeting.setDelegate(this);
        mTRTCMeeting.setSelfProfile(mUserName, mUserAvatar, null);
        mMeetingHeadBarView.setTitle(getString(R.string.trtclive_title_entering));
        createMeeting();
        // 根据外面传入的设置，选择是否打开相应的功能
        mTRTCMeeting.setAudioQuality(mAudioQuality);
        if (mOpenAudio) {
            mTRTCMeeting.startMicrophone();
        } else {
            mTRTCMeeting.stopMicrophone();
        }
        if (mOpenCamera) {
            mTRTCMeeting.startCameraPreview(isFrontCamera, mViewVideo.getLocalPreviewView());
        }
        mTRTCMeeting.setSpeaker(isUseSpeaker);
        mMeetingHeadBarView.setHeadsetImg(isUseSpeaker);
        mTRTCMeeting.enableAudioEvaluation(FeatureConfig.getInstance().isAudioVolumeEvaluation());
    }

    private void initBeauty() {
//        BeautyParams beautyParams = new BeautyParams();
//        mTRTCMeeting.getBeautyManager().setBeautyStyle(beautyParams.mBeautyStyle);
//        mTRTCMeeting.getBeautyManager().setBeautyLevel(beautyParams.mBeautyLevel);
//        mTRTCMeeting.getBeautyManager().setWhitenessLevel(beautyParams.mWhiteLevel);
//        mTRTCMeeting.getBeautyManager().setRuddyLevel(beautyParams.mRuddyLevel);
    }


    private void initData() {
        mTRTCMeeting = TRTCMeeting.sharedInstance(this);
        mStringMemberEntityMap = new HashMap<>();
        mMemberEntityList = new ArrayList<>();
        //从外界获取数据源
        Intent starter = getIntent();
        mRoomId = starter.getStringExtra(KEY_ROOM_ID);
        mUserId = starter.getStringExtra(KEY_USER_ID);
        mUserName = starter.getStringExtra(KEY_USER_NAME);
        mUserAvatar = starter.getStringExtra(KEY_USER_AVATAR);
        mOpenCamera = starter.getBooleanExtra(KEY_OPEN_CAMERA, true);
        mOpenAudio = starter.getBooleanExtra(KEY_OPEN_AUDIO, true);
        mAudioQuality = starter.getIntExtra(KEY_AUDIO_QUALITY, TRTCCloudDef.TRTC_AUDIO_QUALITY_DEFAULT);
        mVideoQuality = starter.getIntExtra(KEY_VIDEO_QUALITY, CreateMeetingActivity.VIDEO_QUALITY_FAST);
        isFrontCamera = starter.getBooleanExtra(KEY_ISFRONT_CAMERA, true);
        isWork = starter.getBooleanExtra(KEY_IS_WORK, false);

        //创建自己的 MemberEntity
        MemberEntity entity = new MemberEntity();
        MeetingVideoView meetingVideoView = new MeetingVideoView(this);
        meetingVideoView.setSelfView(true);
        meetingVideoView.setMeetingUserId(mUserId);
        meetingVideoView.setListener(mMeetingViewClick);
        meetingVideoView.setNeedAttach(true);
        meetingVideoView.setBackgroundColor(Color.BLACK);
        entity.setMeetingVideoView(meetingVideoView);
        entity.setShowAudioEvaluation(FeatureConfig.getInstance().isAudioVolumeEvaluation());
        entity.setAudioAvailable(mOpenAudio);
        entity.setVideoAvailable(mOpenCamera);
        entity.setMuteAudio(false);
        entity.setMuteVideo(false);
        entity.setUserId(mUserId);
        entity.setUserName(mUserName);
        entity.setUserAvatar(mUserAvatar);
        addMemberEntity(entity);

    }

    private void initView() {
        mLvMessage = (ListView) findViewById(R.id.lv_im_msg);
        mChatMsgListAdapter = new TCChatMsgListAdapter(this, mLvMessage, mArrayListChatEntity);
        mLvMessage.setAdapter(mChatMsgListAdapter);

        mInputTextMsgDialog = new InputTextMsgDialog(this, R.style.TRTCLiveRoomInputDialog);
        mInputTextMsgDialog.setmOnTextSendListener(this);


        mListRv = (RecyclerView) findViewById(R.id.rv_list);
        mViewVideo = mMemberEntityList.get(0).getMeetingVideoView();
        mContainerFl = (FrameLayout) findViewById(R.id.fl_container);
        btnRotate = (Button) findViewById(R.id.btn_rotate);


        mMemberListAdapter = new MemberListAdapter(this, mTRTCMeeting, mMemberEntityList, new MemberListAdapter.ListCallback() {
            @Override
            public void onItemClick(int position) {

            }

            @Override
            public void onItemDoubleClick(final int position) {
            }
        });

        mListRv.setHasFixedSize(true);
        MeetingPageLayoutManager pageLayoutManager = new MeetingPageLayoutManager(2, 2, HORIZONTAL);
        pageLayoutManager.setAllowContinuousScroll(false);
        pageLayoutManager.setPageListener(new MeetingPageLayoutManager.PageListener() {
            @Override
            public void onPageSizeChanged(int pageSize) {

            }

            @Override
            public void onPageSelect(int pageIndex) {

            }

            @Override
            public void onItemVisible(int fromItem, int toItem) {
                Log.d(TAG, "onItemVisible: " + fromItem + " to " + toItem);
                if (fromItem == 0) {
                    //第0个是自己的video，分开处理
                    processSelfVideoPlay();
                    processVideoPlay(1, toItem);
                } else {
                    processVideoPlay(fromItem, toItem);
                }
            }
        });
        mListRv.setLayoutManager(pageLayoutManager);
        mListRv.setAdapter(mMemberListAdapter);
        mListRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d(TAG, "onScrollStateChanged: " + newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                }
            }
        });
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(mListRv);
        mMeetingHeadBarView = (MeetingHeadBarView) findViewById(R.id.view_meeting_head_bar);
        mAudioImg = (AppCompatImageButton) findViewById(R.id.img_audio);
        mAudioImg.setOnClickListener(this);
        mVideoImg = (AppCompatImageButton) findViewById(R.id.img_video);
        mVideoImg.setOnClickListener(this);
//        mBeautyImg = (AppCompatImageButton) findViewById(R.id.img_beauty);
//        mBeautyImg.setOnClickListener(this);
        mMessageImg = (AppCompatImageButton) findViewById(R.id.img_message);
        mMessageImg.setOnClickListener(this);
        mMemberImg = (AppCompatImageButton) findViewById(R.id.img_member);
        mMemberImg.setOnClickListener(this);
        mMoreImg = (AppCompatImageButton) findViewById(R.id.img_more);
        mMoreImg.setOnClickListener(this);

        mCloseImg = (AppCompatImageButton) findViewById(R.id.img_close);
        mCloseImg.setOnClickListener(this);

        mLinkImg = (AppCompatImageButton) findViewById(R.id.img_link);
        mLinkImg.setOnClickListener(this);
//        mBeautyControl = new BeautyPanel(this);
//        mBeautyControl.setBeautyManager(mTRTCMeeting.getBeautyManager());
        mStubRemoteUserView = (ViewStub) findViewById(R.id.view_stub_remote_user);
        mFeatureSettingFragmentDialog = new FeatureSettingFragmentDialog();
        mFeatureSettingFragmentDialog.setTRTCMeeting(mTRTCMeeting);
        mFeatureSettingFragmentDialog.setShareButtonClickListener(new FeatureSettingFragmentDialog.OnShareButtonClickListener() {
            @Override
            public void onClick() {
                onShareScreenClick();
            }
        });
        // 设置界面UI
        mVideoImg.setSelected(mOpenCamera);
        mAudioImg.setSelected(mOpenAudio);
//        mMeetingHeadBarView.setTitle(String.valueOf(mRoomId));
        mMeetingHeadBarView.setTitle("");
        mMeetingHeadBarView.setHeadBarCallback(new MeetingHeadBarView.HeadBarCallback() {
            @Override
            public void onHeadSetClick() {
                isUseSpeaker = !isUseSpeaker;
                mTRTCMeeting.setSpeaker(isUseSpeaker);
                mMeetingHeadBarView.setHeadsetImg(isUseSpeaker);
            }

            @Override
            public void onSwitchCameraClick() {
                isFrontCamera = !isFrontCamera;
                mTRTCMeeting.switchCamera(isFrontCamera);
            }

            @Override
            public void onExitClick() {
                preExitMeeting();
            }
        });
        mScreenCaptureGroup = findViewById(R.id.group_screen_capture);
        mBottomToolBarGroup = (Group) findViewById(R.id.group_bottom_tool_bar);
        mStopScreenCaptureTv = (TextView) findViewById(R.id.tv_stop_screen_capture);

        // 注册本地变更的通知
        IntentFilter intentFilter = new IntentFilter(FeatureConfig.AUDIO_EVALUATION_CHANGED);
        LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                boolean isAudioVolumeEvaluationShow = FeatureConfig.getInstance().isAudioVolumeEvaluation();
                for (MemberEntity entity : mMemberEntityList) {
                    entity.setShowAudioEvaluation(isAudioVolumeEvaluationShow);
                }

                mMemberListAdapter.notifyItemRangeChanged(0, mMemberEntityList.size(), MemberListAdapter.VOLUME_SHOW);
            }
        }, intentFilter);

        if (isWork) {
            mMeetingHeadBarView.setCameraSwitchImg(false);
            mBottomToolBarGroup.setVisibility(View.INVISIBLE);
        } else {
            mMeetingHeadBarView.setCameraSwitchImg(true);
            mBottomToolBarGroup.setVisibility(View.VISIBLE);
        }
    }

    private void processSelfVideoPlay() {
        if (mMemberEntityList.get(0).isShowOutSide()) {
            return;
        }
        MeetingVideoView meetingVideoView = mMemberEntityList.get(0).getMeetingVideoView();
        meetingVideoView.refreshParent();
    }

    /**
     * 处理页面中需要展示的item
     * 如果滑动到新的页面，旧的页面所有item需要停止播放
     * 新的页面根据是否打开了video来判断需要播放页面
     *
     * @param fromItem
     * @param toItem
     */
    private void processVideoPlay(int fromItem, int toItem) {
        List<String> oldUserIds = new ArrayList<>();
        List<String> newUserIds = new ArrayList<>();
        List<String> needStopIds = new ArrayList<>();
        List<MemberEntity> newVisibleStream = new ArrayList<>();
        if (mVisibleVideoStreams == null) {
            mVisibleVideoStreams = new ArrayList<>();
        }
        for (int i = fromItem; i <= toItem; i++) {
            newUserIds.add(mMemberEntityList.get(i).getUserId());
            newVisibleStream.add(mMemberEntityList.get(i));
        }
        for (MemberEntity entity : mVisibleVideoStreams) {
            oldUserIds.add(entity.getUserId());
            if (!newUserIds.contains(entity.getUserId())) {
                needStopIds.add(entity.getUserId());
            }
        }
        for (MemberEntity entity : newVisibleStream) {
            if (entity.isShowOutSide()) {
                continue;
            }
            MeetingVideoView meetingVideoView = entity.getMeetingVideoView();
            meetingVideoView.refreshParent();
            if (entity.isNeedFresh()) {
                entity.setNeedFresh(false);
                if (!entity.isMuteVideo() && entity.isVideoAvailable()) {
                    meetingVideoView.setPlaying(true);
                    mTRTCMeeting.startRemoteView(entity.getUserId(), entity.getMeetingVideoView().getPlayVideoView(),
                            (code, msg) -> {
                                if (code == 0) {
                                    try {
                                        String[] split = msg.split(",");
                                        if (split.length == 3) {
                                            MemberEntity memberEntity = mStringMemberEntityMap.get(entity.getUserId());
                                            if (memberEntity != null) {
                                                if (Integer.parseInt(split[1]) > Integer.parseInt(split[2])) {
                                                    memberEntity.isLandscape = true;
                                                } else {
                                                    memberEntity.isLandscape = false;
                                                }
                                            }
                                        }
                                    } catch (Exception e) {

                                    }
                                }
                            });
                } else {
                    if (meetingVideoView.isPlaying()) {
                        meetingVideoView.setPlaying(false);
                        mTRTCMeeting.stopRemoteView(entity.getUserId(), null);
                    }
                }
                continue;
            }
            if (!oldUserIds.contains(entity.getUserId())) {
                if (!entity.isMuteVideo() && entity.isVideoAvailable()) {
                    if (!meetingVideoView.isPlaying()) {
                        meetingVideoView.setPlaying(true);
                        mTRTCMeeting.startRemoteView(entity.getUserId(), entity.getMeetingVideoView().getPlayVideoView(),
                                (code, msg) -> {
                                    if (code == 0) {
                                        try {
                                            String[] split = msg.split(",");
                                            if (split.length == 3) {
                                                MemberEntity memberEntity = mStringMemberEntityMap.get(entity.getUserId());
                                                if (memberEntity != null) {
                                                    if (Integer.parseInt(split[1]) > Integer.parseInt(split[2])) {
                                                        memberEntity.isLandscape = true;
                                                    } else {
                                                        memberEntity.isLandscape = false;
                                                    }
                                                }
                                            }
                                        } catch (Exception e) {

                                        }
                                    }
                                });
                        meetingVideoView.refreshParent();
                    }
                } else {
                    if (meetingVideoView.isPlaying()) {
                        meetingVideoView.setPlaying(false);
                        mTRTCMeeting.stopRemoteView(entity.getUserId(), null);
                    }
                }
            } else {
                if (entity.isMuteVideo() || !entity.isVideoAvailable()) {
                    if (meetingVideoView.isPlaying()) {
                        meetingVideoView.setPlaying(false);
                        mTRTCMeeting.stopRemoteView(entity.getUserId(), null);
                    }
                }
            }
        }
        for (String id : needStopIds) {
            MemberEntity entity = mStringMemberEntityMap.get(id);
            if (entity != null) {
                entity.getMeetingVideoView().setPlayingWithoutSetVisible(false);
            }
            mTRTCMeeting.stopRemoteView(id, null);
        }
        mVisibleVideoStreams = newVisibleStream;
    }

    @Override
    public void onError(int code, String message) {
        if (code == -1308) {
            ToastUtils.showLong(getString(R.string.trtclive_toast_start_screen_recording_failed));
            stopScreenCapture();
        } else {
            ToastUtils.showLong(getString(R.string.trtclive_toast_error, code, message));
            finish();
        }
    }

    @Override
    public void onRoomDestroy(String roomId) {
        //TODO
        if (String.valueOf(mRoomId).equals(roomId)) {
            ToastUtils.showShort(getString(R.string.trtclive_toast_end_meeting));
            EventBus.getDefault().post(new BaseEvent.EndLiveEvent());
            finish();
        }

        if ((mRoomId + "_").equals(roomId)) {
            EventBus.getDefault().post(new BaseEvent.EndLiveEvent());
            finish();
        }
    }

    @Override
    public void onNetworkQuality(TRTCCloudDef.TRTCQuality localQuality, List<TRTCCloudDef.TRTCQuality> remoteQuality) {
        Log.d(TAG, "onNetworkQuality: ");
        matchQuality(localQuality, mStringMemberEntityMap.get(mUserId));
        for (TRTCCloudDef.TRTCQuality quality : remoteQuality) {
            matchQuality(quality, mStringMemberEntityMap.get(quality.userId));
        }
    }

    private void matchQuality(TRTCCloudDef.TRTCQuality trtcQuality, MemberEntity entity) {
        if (entity == null) {
            return;
        }
        int oldQulity = entity.getQuality();
        switch (trtcQuality.quality) {
            case TRTCCloudDef.TRTC_QUALITY_Excellent:
            case TRTCCloudDef.TRTC_QUALITY_Good:
                entity.setQuality(MemberEntity.QUALITY_GOOD);
                break;
            case TRTCCloudDef.TRTC_QUALITY_Poor:
            case TRTCCloudDef.TRTC_QUALITY_Bad:
                entity.setQuality(MemberEntity.QUALITY_NORMAL);
                break;
            case TRTCCloudDef.TRTC_QUALITY_Vbad:
            case TRTCCloudDef.TRTC_QUALITY_Down:
                entity.setQuality(MemberEntity.QUALITY_BAD);
                break;
            default:
                entity.setQuality(MemberEntity.QUALITY_NORMAL);
                break;
        }
        if (oldQulity != entity.getQuality()) {
            mMemberListAdapter.notifyItemChanged(mMemberEntityList.indexOf(entity), MemberListAdapter.QUALITY);
        }
    }

    @Override
    public void onUserVolumeUpdate(String userId, int volume) {
        if (!FeatureConfig.getInstance().isAudioVolumeEvaluation()) {
            return;
        }
        if (userId == null) {
            userId = mUserId;
        }
        MemberEntity memberEntity = mStringMemberEntityMap.get(userId);
        if (memberEntity != null) {
            memberEntity.setAudioVolume(volume);
            boolean change = false;
            if (memberEntity.getAudioVolume() > 20) {
                if (!memberEntity.isTalk()) {
                    memberEntity.setTalk(true);
                    change = true;
                }
            } else {
                if (memberEntity.isTalk()) {
                    memberEntity.setTalk(false);
                    change = true;
                }
            }
            if (change) {
                mMemberListAdapter.notifyItemChanged(mMemberEntityList.indexOf(memberEntity), MemberListAdapter.VOLUME);
            }
        }
    }

    @Override
    public void onUserEnterRoom(final String userId) {
        final int insertIndex = mMemberEntityList.size();
        final MemberEntity entity = new MemberEntity();
        final MeetingVideoView meetingVideoView = new MeetingVideoView(this);
        meetingVideoView.setMeetingUserId(userId);
        meetingVideoView.setNeedAttach(false);
        meetingVideoView.setListener(mMeetingViewClick);
        meetingVideoView.setBackgroundColor(Color.BLACK);
        entity.setUserId(userId);
        entity.setMeetingVideoView(meetingVideoView);
        entity.setMuteAudio(mIsUserEnterMuteAudio);
        entity.setMuteVideo(false);
        entity.setVideoAvailable(false);
        entity.setAudioAvailable(false);
        entity.setShowAudioEvaluation(FeatureConfig.getInstance().isAudioVolumeEvaluation());
        addMemberEntity(entity);
        changeResolution();

        mMemberListAdapter.notifyItemInserted(insertIndex);
        if (mRemoteUserView != null) {
            mRemoteUserView.notifyDataSetChanged();
        }
        mTRTCMeeting.muteRemoteAudio(userId, mIsUserEnterMuteAudio);
        mTRTCMeeting.getUserInfo(userId, new TRTCMeetingCallback.UserListCallback() {
            @Override
            public void onCallback(int code, String msg, List<TRTCMeetingDef.UserInfo> list) {
                if (code == 0 && list != null && list.size() != 0) {
                    entity.setUserName(list.get(0).userName);
                    entity.setUserAvatar(list.get(0).userAvatar);
                    mMemberListAdapter.notifyItemChanged(mMemberEntityList.indexOf(entity));
                }
            }
        });

    }

    private void changeResolution() {
        if (isScreenCapture) {
            return;
        }
        if (mVideoQuality == CreateMeetingActivity.VIDEO_QUALITY_HD) {
            TRTCCloudDef.TRTCNetworkQosParam qosParam = new TRTCCloudDef.TRTCNetworkQosParam();
            qosParam.preference = TRTCCloudDef.TRTC_VIDEO_QOS_PREFERENCE_CLEAR;
            mTRTCMeeting.setNetworkQosParam(qosParam);
            if (mMemberEntityList.size() <= 2) {
                mTRTCMeeting.setVideoResolution(TRTCCloudDef.TRTC_VIDEO_RESOLUTION_960_540);
                mTRTCMeeting.setVideoFps(15);
                mTRTCMeeting.setVideoBitrate(1300);
            } else if (mMemberEntityList.size() < 4) {
                mTRTCMeeting.setVideoResolution(TRTCCloudDef.TRTC_VIDEO_RESOLUTION_640_360);
                mTRTCMeeting.setVideoFps(15);
                mTRTCMeeting.setVideoBitrate(800);
            } else {
                mTRTCMeeting.setVideoResolution(TRTCCloudDef.TRTC_VIDEO_RESOLUTION_480_270);
                mTRTCMeeting.setVideoFps(15);
                mTRTCMeeting.setVideoBitrate(400);
            }
        } else {
            TRTCCloudDef.TRTCNetworkQosParam qosParam = new TRTCCloudDef.TRTCNetworkQosParam();
            qosParam.preference = TRTCCloudDef.TRTC_VIDEO_QOS_PREFERENCE_SMOOTH;
            mTRTCMeeting.setNetworkQosParam(qosParam);
            if (mMemberEntityList.size() < 5) {
                // 包括自己，一共四个人，选择360p分辨率
                mTRTCMeeting.setVideoResolution(TRTCCloudDef.TRTC_VIDEO_RESOLUTION_640_360);
                mTRTCMeeting.setVideoFps(15);
                mTRTCMeeting.setVideoBitrate(700);
            } else {
                mTRTCMeeting.setVideoResolution(TRTCCloudDef.TRTC_VIDEO_RESOLUTION_480_270);
                mTRTCMeeting.setVideoFps(15);
                mTRTCMeeting.setVideoBitrate(350);
            }
        }
    }

    @Override
    public void onUserLeaveRoom(String userId) {
        if (mShowUserId.equals(userId)) {
            mShowUserId = "";
            mContainerFl.removeAllViews();
            mContainerFl.setVisibility(View.GONE);
        }
        int index = removeMemberEntity(userId);
        changeResolution();
        if (index >= 0) {
            mMemberListAdapter.notifyItemRemoved(index);
        }
        if (mRemoteUserView != null) {
            mRemoteUserView.notifyDataSetChanged();
        }

        if (roomListDialog != null) {
            roomListDialog.refresh();
        }
    }


    @Override
    public void onUserVideoAvailable(String userId, boolean available) {
        MemberEntity entity = mStringMemberEntityMap.get(userId);
        if (!available && mShowUserId.equals(userId)) {
            mShowUserId = "";
            mContainerFl.removeAllViews();
            mContainerFl.setVisibility(View.GONE);
            entity.setShowOutSide(false);
        }
        if (entity != null) {
            entity.setNeedFresh(true);
            entity.setVideoAvailable(available);
            entity.getMeetingVideoView().setNeedAttach(available);
            mMemberListAdapter.notifyItemChanged(mMemberEntityList.indexOf(entity));
        }
    }

    @Override
    public void onUserAudioAvailable(String userId, boolean available) {
        MemberEntity entity = mStringMemberEntityMap.get(userId);
        if (entity != null) {
            entity.setAudioAvailable(available);
            //界面暂时没有变更
        }
    }

    @Override
    public void onRecvRoomTextMsg(String message, TRTCMeetingDef.UserInfo userInfo) {
        handleTextMsg(userInfo, message);
    }

    @Override
    public void onRecvRoomCustomMsg(String cmd, String message, TRTCMeetingDef.UserInfo userInfo) {
        try {
            int type = Integer.valueOf(cmd);
            JSONObject jsonObject = new JSONObject(message);
            long time = jsonObject.getLong("time");
            String to = jsonObject.getString("to");
            switch (type) {
                case TCConstants.IMCMD_PRAISE:
//                handlePraiseMsg(userInfo);
                    break;
                case TCConstants.IMCMD_DANMU:
//                handleDanmuMsg(userInfo, message);
                    break;
                case TCConstants.CODE_ROOM_LINK:
                    if (mLinkList != null) {
                        if (TextUtils.isEmpty(to)) {
                            userInfo.time = time;
                            mLinkList.add(userInfo);
                            mLinkImg.setImageResource(R.drawable.ic_bell_red);
                            linkListDialog.refresh();
                        } else {
                            if (to.equals(UserManager.getWorkid())) {
                                showCommonDialog(
                                        userInfo.userName + "邀请你上麦？", "同意", "拒绝",
                                        new ConfirmDialogFragment.PositiveClickListener() {

                                            @Override
                                            public void onClick() {
                                                mTRTCMeeting.sendRoomCustomMsg(
                                                        TCConstants.CODE_ROOM_LINK_OK + "",
                                                        time, userInfo.userId, (code, msg) -> {
                                                            if (code == 0) {
                                                                switchOk();
                                                            } else {
                                                                $.toast("操作失败" + msg);
                                                            }
                                                        }
                                                );
                                            }
                                        },
                                        new ConfirmDialogFragment.NegativeClickListener() {

                                            @Override
                                            public void onClick() {
                                                mTRTCMeeting.sendRoomCustomMsg(
                                                        TCConstants.CODE_ROOM_LINK_CANCEL + "",
                                                        time, userInfo.userId, null
                                                );
                                            }
                                        }
                                );
                            }
                        }
                    }
                    break;
                case TCConstants.CODE_ROOM_LINK_OK:
                    //如果to为空  是观众同意主播   不为空 是主播同意观众
                    if (TextUtils.isEmpty(to)) {
                        if (mInviteList != null) {
                            ArrayList<RedisList.RedisData.DataBean> outList = new ArrayList<>();
                            for (RedisList.RedisData.DataBean item : mInviteList) {
                                if (userInfo.userId.equals(item.id)) {
                                    outList.add(item);
                                    $.toast(item.name + "同意了连麦");
                                }
                            }
                            mInviteList.removeAll(outList);
                            if (roomListDialog != null) {
                                roomListDialog.refresh();
                            }
                        }
                    } else {
                        //删除自己队列里的申请
                        if (mLinkList != null) {
                            ArrayList<TRTCMeetingDef.UserInfo> outList = new ArrayList<>();
                            for (TRTCMeetingDef.UserInfo item : mLinkList) {
                                if (to.equals(item.userId)) {
                                    outList.add(item);
                                }
                            }
                            mLinkList.removeAll(outList);
                            if (mLinkList.size() > 0) {
                                mLinkImg.setImageResource(R.drawable.ic_bell_red);
                            } else {
                                mLinkImg.setImageResource(R.drawable.ic_bell);
                            }
                            if (linkListDialog != null) {
                                linkListDialog.refresh();
                            }
                            if (roomListDialog != null) {
                                roomListDialog.refresh();
                            }
                        }
                    }
                    break;
                case TCConstants.CODE_ROOM_LINK_CANCEL:
                    if (TextUtils.isEmpty(to)) {
                        if (mInviteList != null) {
                            ArrayList<RedisList.RedisData.DataBean> outList = new ArrayList<>();
                            for (RedisList.RedisData.DataBean item : mInviteList) {
                                if (userInfo.userId.equals(item.id)) {
                                    outList.add(item);
                                    $.toast(item.name + "拒绝了连麦");
                                }
                            }
                            mInviteList.removeAll(outList);
                            if (roomListDialog != null) {
                                roomListDialog.refresh();
                            }
                        }
                    } else {
                        //删除自己队列里的申请
                        if (mLinkList != null) {
                            ArrayList<TRTCMeetingDef.UserInfo> outList = new ArrayList<>();
                            for (TRTCMeetingDef.UserInfo item : mLinkList) {
                                if (to.equals(item.userId)) {
                                    outList.add(item);
                                }
                            }
                            mLinkList.removeAll(outList);
                            if (mLinkList.size() > 0) {
                                mLinkImg.setImageResource(R.drawable.ic_bell_red);
                            } else {
                                mLinkImg.setImageResource(R.drawable.ic_bell);
                            }
                            if (linkListDialog != null) {
                                linkListDialog.refresh();
                            }
                        }
                    }
                    break;
                case TCConstants.CODE_ROOM_LINK_CLOSE:
                    if (!TextUtils.isEmpty(to)) {
                        if (to.equals(UserManager.getWorkid())) {
                            $.toast("您已被老师下麦");
                            switchCancel();
                        }
                    }
                    break;
                default:
                    break;
            }
        } catch (Exception e) {

        }
    }

    private void switchCancel() {
        mCloseImg.setVisibility(View.GONE);
        TRTCCloud.sharedInstance(Utils.getApp())
                .switchRole(TRTCCloudDef.TRTCRoleAudience);
        mOpenAudio = false;
        mTRTCMeeting.stopMicrophone();
        mAudioImg.setSelected(mOpenAudio);

        mOpenCamera = false;
        MemberEntity entity = mMemberEntityList.get(0);
        if (mShowUserId.equals(mUserId)) {
            mShowUserId = "";
            mStringMemberEntityMap.get(mUserId).setShowOutSide(false);
            mContainerFl.removeAllViews();
            mContainerFl.setVisibility(View.GONE);
        }
        mTRTCMeeting.stopCameraPreview();

        entity.setVideoAvailable(mOpenCamera);
        mVideoImg.setSelected(mOpenCamera);
        mMemberListAdapter.notifyItemChanged(0);

        mMeetingHeadBarView.setCameraSwitchImg(false);
    }

    private void switchOk() {
        mCloseImg.setVisibility(View.VISIBLE);
        TRTCCloud.sharedInstance(Utils.getApp())
                .switchRole(TRTCCloudDef.TRTCRoleAnchor);
        mOpenAudio = true;
        mTRTCMeeting.startMicrophone();
        mAudioImg.setSelected(mOpenAudio);

        mOpenCamera = true;
        MemberEntity entity = mMemberEntityList.get(0);
        if (mShowUserId.equals(mUserId)) {
            mShowUserId = "";
            mStringMemberEntityMap.get(mUserId).setShowOutSide(false);
            mContainerFl.removeAllViews();
            mContainerFl.setVisibility(View.GONE);
        }
        MeetingVideoView videoView = entity.getMeetingVideoView();
        mTRTCMeeting.startCameraPreview(isFrontCamera, videoView.getLocalPreviewView());

        entity.setVideoAvailable(mOpenCamera);
        mVideoImg.setSelected(mOpenCamera);
        mMemberListAdapter.notifyItemChanged(0);

        mMeetingHeadBarView.setCameraSwitchImg(true);
    }

    @Override
    public void onScreenCaptureStarted() {
        isScreenCapture = true;
    }

    @Override
    public void onScreenCapturePaused() {

    }

    @Override
    public void onScreenCaptureResumed() {

    }

    @Override
    public void onScreenCaptureStopped(int reason) {
        isScreenCapture = false;
        changeResolution();
        if (mOpenCamera) {
            mTRTCMeeting.startCameraPreview(isFrontCamera, mViewVideo.getLocalPreviewView());
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.img_audio) {
            boolean isAudioOn = mOpenAudio;
            if (isAudioOn) {
                mTRTCMeeting.stopMicrophone();
            } else {
                mTRTCMeeting.startMicrophone();
            }
            mAudioImg.setSelected(!isAudioOn);
            mOpenAudio = !isAudioOn;
        } else if (id == R.id.img_video) {
            boolean isVideoOn = mOpenCamera;
            MemberEntity entity = mMemberEntityList.get(0);
            if (mShowUserId.equals(mUserId)) {
                mShowUserId = "";
                mStringMemberEntityMap.get(mUserId).setShowOutSide(false);
                mContainerFl.removeAllViews();
                mContainerFl.setVisibility(View.GONE);
            }
            if (isVideoOn) {
                mTRTCMeeting.stopCameraPreview();
            } else {
                BaseApp.mHandler.postDelayed(() -> {
                    MeetingVideoView videoView = entity.getMeetingVideoView();
                    mTRTCMeeting.startCameraPreview(isFrontCamera, videoView.getLocalPreviewView());
                },500);
            }
            entity.setVideoAvailable(!isVideoOn);
            mVideoImg.setSelected(!isVideoOn);
            mOpenCamera = !isVideoOn;
            mMemberListAdapter.notifyItemChanged(0);
//            mMemberListAdapter.setList(changeToAdapter());
        }
//        else if (id == R.id.img_beauty) {
//            if (mBeautyControl.isShowing()) {
//                mBeautyControl.hide();
//            } else {
//                mBeautyControl.show();
//            }
//        }
        else if (id == R.id.img_message) {
            showInputMsgDialog();
        } else if (id == R.id.img_link) {
            linkListDialog = new LinkListDialog(mLinkList);
            linkListDialog.setListener(new LinkListDialog.LinkListener() {
                @Override
                public void ok(TRTCMeetingDef.UserInfo item) {
                    mLinkList.remove(item);
                    if (mLinkList.size() > 0) {
                        mLinkImg.setImageResource(R.drawable.ic_bell_red);
                    } else {
                        mLinkImg.setImageResource(R.drawable.ic_bell);
                    }

                    linkListDialog.refresh();
                    mTRTCMeeting.sendRoomCustomMsg(
                            TCConstants.CODE_ROOM_LINK_OK + "",
                            item.time, item.userId, (code, msg) -> {
                                if (code == 0) {

                                } else {
                                    $.toast("操作失败" + msg);
                                }
                            }
                    );
                }

                @Override
                public void cancel(TRTCMeetingDef.UserInfo item) {
                    mLinkList.remove(item);
                    if (mLinkList.size() > 0) {
                        mLinkImg.setImageResource(R.drawable.ic_bell_red);
                    } else {
                        mLinkImg.setImageResource(R.drawable.ic_bell);
                    }
                    linkListDialog.refresh();
                    mTRTCMeeting.sendRoomCustomMsg(
                            TCConstants.CODE_ROOM_LINK_CANCEL + "",
                            item.time, item.userId, null
                    );
                }
            });
            linkListDialog.show();
        } else if (id == R.id.img_member) {
            roomListDialog = new RoomListDialog(mRoomId, mInviteList, mMemberEntityList);
            roomListDialog.setListener(item -> {
                mInviteList.add(item);
                roomListDialog.refresh();
            });
            roomListDialog.show();
//            handleMemberListView();
        } else if (id == R.id.img_more) {
            roomSettingDialog = new RoomSettingDialog(mRoomId + "");
            roomSettingDialog.show();
//            showDialogFragment(mFeatureSettingFragmentDialog, "FeatureSettingFragmentDialog");
        } else if (id == R.id.img_close) {
            showCommonDialog("确认退出连麦", this::switchCancel);
        }
    }

    private void onShareScreenClick() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!PermissionUtils.isGrantedDrawOverlays()) {
                ToastUtils.showLong(getString(R.string.trtclive_toast_need_floating_window_permission));
                PermissionUtils.requestDrawOverlays(new PermissionUtils.SimpleCallback() {
                    @Override
                    public void onGranted() {
                        startScreenCapture();
                    }

                    @Override
                    public void onDenied() {
                        ToastUtils.showLong(getString(R.string.trtclive_toast_need_floating_window_permission));
                    }
                });
            } else {
                startScreenCapture();
            }
        } else {
            startScreenCapture();
        }
    }

    private void startScreenCapture() {
        mListRv.setVisibility(View.GONE);
        mScreenCaptureGroup.setVisibility(View.VISIBLE);
        mBottomToolBarGroup.setVisibility(View.GONE);

        mStopScreenCaptureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopScreenCapture();
            }
        });

        TRTCCloudDef.TRTCVideoEncParam encParams = new TRTCCloudDef.TRTCVideoEncParam();
        encParams.videoResolution = TRTCCloudDef.TRTC_VIDEO_RESOLUTION_1280_720;
        encParams.videoResolutionMode = TRTCCloudDef.TRTC_VIDEO_RESOLUTION_MODE_PORTRAIT;
        encParams.videoFps = 10;
        encParams.enableAdjustRes = false;
        encParams.videoBitrate = 1500;

        TRTCCloudDef.TRTCScreenShareParams params = new TRTCCloudDef.TRTCScreenShareParams();
        mTRTCMeeting.stopCameraPreview();
        mTRTCMeeting.startScreenCapture(encParams, params);

        if (mFloatingWindow == null) {
            LayoutInflater inflater = LayoutInflater.from(this);
            mFloatingWindow = inflater.inflate(R.layout.trtclive_screen_capture_floating_window, null, false);
            mFloatingWindow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "onClick: 悬浮窗");
                }
            });
        }
        showFloatingWindow();
    }

    private void stopScreenCapture() {
        hideFloatingWindow();
        mListRv.setVisibility(View.VISIBLE);
        mScreenCaptureGroup.setVisibility(View.GONE);
        mBottomToolBarGroup.setVisibility(View.VISIBLE);
        mTRTCMeeting.stopScreenCapture();
    }

    private void showFloatingWindow() {
        if (mFloatingWindow == null) {
            return;
        }
        WindowManager windowManager = (WindowManager) mFloatingWindow.getContext().getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null) {
            return;
        }
        //TYPE_TOAST仅适用于4.4+系统，假如要支持更低版本使用TYPE_SYSTEM_ALERT(需要在manifest中声明权限)
        //7.1（包含）及以上系统对TYPE_TOAST做了限制
        int type = WindowManager.LayoutParams.TYPE_TOAST;
        if (Build.VERSION.SDK_INT >= 26) {
            type = 2038; // WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(type);
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.gravity = Gravity.RIGHT;
        windowManager.addView(mFloatingWindow, layoutParams);
    }

    private void hideFloatingWindow() {
        if (mFloatingWindow == null) {
            return;
        }
        WindowManager windowManager = (WindowManager) mFloatingWindow.getContext().getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            windowManager.removeViewImmediate(mFloatingWindow);
        }
        mFloatingWindow = null;
    }

    /**
     * 展示dialog界面
     */
    private void showDialogFragment(DialogFragment dialogFragment, String tag) {
        if (dialogFragment != null) {
            if (dialogFragment.isVisible()) {
                try {
                    dialogFragment.dismissAllowingStateLoss();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                if (!dialogFragment.isAdded()) {
                    dialogFragment.show(getSupportFragmentManager(), tag);
                }
            }
        }
    }

    /**
     * 处理成员列表
     */
    private void handleMemberListView() {
        if (mRemoteUserView == null) {
            mStubRemoteUserView.inflate();
            mRemoteUserView = (RemoteUserListView) findViewById(R.id.view_remote_user);
            // 监听成员列表的变化
            mRemoteUserView.setRemoteUserListCallback(new RemoteUserListView.RemoteUserListCallback() {
                @Override
                public void onFinishClick() {
                    mRemoteUserView.setVisibility(View.GONE);
                    StateBarUtils.setDarkStatusBar(MeetingMainActivity.this);
                }

                @Override
                public void onMuteAllAudioClick() {
                    ToastUtils.showShort(getString(R.string.trtclive_toast_mute_all_audio));
                    mIsUserEnterMuteAudio = true;
                    for (int i = 1; i < mMemberEntityList.size(); i++) {
                        MemberEntity entity = mMemberEntityList.get(i);
                        entity.setMuteAudio(true);
                        mTRTCMeeting.muteRemoteAudio(entity.getUserId(), true);
                    }
                    mRemoteUserView.notifyDataSetChanged();
                }

                @Override
                public void onMuteAllAudioOffClick() {
                    ToastUtils.showShort(getString(R.string.trtclive_toast_not_mute_all_audio));
                    mIsUserEnterMuteAudio = false;
                    for (int i = 1; i < mMemberEntityList.size(); i++) {
                        MemberEntity entity = mMemberEntityList.get(i);
                        entity.setMuteAudio(false);
                        mTRTCMeeting.muteRemoteAudio(entity.getUserId(), false);
                    }
                    mRemoteUserView.notifyDataSetChanged();
                }

                @Override
                public void onMuteAllVideoClick() {
                    ToastUtils.showShort(getString(R.string.trtclive_toast_mute_all_video));
                    for (int i = 1; i < mMemberEntityList.size(); i++) {
                        MemberEntity entity = mMemberEntityList.get(i);
                        entity.setMuteVideo(true);
                        entity.setNeedFresh(true);
                        entity.getMeetingVideoView().setNeedAttach(false);
                        mTRTCMeeting.muteRemoteVideoStream(entity.getUserId(), true);
                    }
                    mMemberListAdapter.notifyDataSetChanged();
//                    mMemberListAdapter.setList(changeToAdapter());
                    mRemoteUserView.notifyDataSetChanged();
                }

                @Override
                public void onMuteAudioClick(int position) {
                    MemberEntity entity = mMemberEntityList.get(position);
                    if (entity != null) {
                        boolean isMuteAudio = !entity.isMuteAudio();
                        entity.setMuteAudio(isMuteAudio);
                        mTRTCMeeting.muteRemoteAudio(entity.getUserId(), isMuteAudio);
                        mRemoteUserView.notifyDataSetChanged();
                    }
                }

                @Override
                public void onMuteVideoClick(int position) {
                    MemberEntity entity = mMemberEntityList.get(position);
                    if (entity != null) {
                        boolean isMuteVideo = !entity.isMuteVideo();
                        entity.setMuteVideo(isMuteVideo);
                        entity.setNeedFresh(true);
                        entity.getMeetingVideoView().setNeedAttach(!isMuteVideo);
                        mTRTCMeeting.muteRemoteVideoStream(entity.getUserId(), isMuteVideo);
                        mMemberListAdapter.notifyItemChanged(position);
//                        mMemberListAdapter.setList(changeToAdapter());
                        mRemoteUserView.notifyDataSetChanged();
                    }
                }

                @Override
                public void onMuteAllVideoOffClick() {
                    ToastUtils.showShort(getString(R.string.trtclive_toast_not_mute_all_video));
                    for (int i = 1; i < mMemberEntityList.size(); i++) {
                        MemberEntity entity = mMemberEntityList.get(i);
                        entity.setMuteVideo(false);
                        entity.setNeedFresh(true);
                        entity.getMeetingVideoView().setNeedAttach(true);
                        mTRTCMeeting.muteRemoteVideoStream(entity.getUserId(), false);
                    }
                    mMemberListAdapter.notifyDataSetChanged();
//                    mMemberListAdapter.setList(changeToAdapter());
                    mRemoteUserView.notifyDataSetChanged();
                }
            });
            mRemoteUserView.setRemoteUser(mMemberEntityList);
            mRemoteUserView.notifyDataSetChanged();
            StateBarUtils.setLightStatusBar(MeetingMainActivity.this);
        } else {
            if (mRemoteUserView.isShown()) {
                mRemoteUserView.setVisibility(View.GONE);
                StateBarUtils.setDarkStatusBar(MeetingMainActivity.this);
            } else {
                mRemoteUserView.setVisibility(View.VISIBLE);
                StateBarUtils.setLightStatusBar(MeetingMainActivity.this);
            }
        }
    }

    private void addMemberEntity(MemberEntity entity) {
        mMemberEntityList.add(entity);
        mStringMemberEntityMap.put(entity.getUserId(), entity);
    }

    private int removeMemberEntity(String userId) {
        MemberEntity entity = mStringMemberEntityMap.remove(userId);
        if (entity != null) {
            int i = mMemberEntityList.indexOf(entity);
            mMemberEntityList.remove(entity);
            return i;
        }
        return -1;
    }

    public void showCommonDialog(String msg, ConfirmDialogFragment.PositiveClickListener listener) {
        this.showCommonDialog(msg, "确定", "取消", listener, null);
    }

    public void showCommonDialog(String msg, String ok, String cancel, ConfirmDialogFragment.PositiveClickListener listener, ConfirmDialogFragment.NegativeClickListener listener2) {
        final ConfirmDialogFragment dialogFragment = new ConfirmDialogFragment();
        dialogFragment.setCancelable(true);
        dialogFragment.setMessage(msg);
        if (dialogFragment.isAdded()) {
            dialogFragment.dismiss();
            return;
        }
        dialogFragment.setPositiveText(ok);
        dialogFragment.setNegativeText(cancel);
        dialogFragment.setPositiveClickListener(new ConfirmDialogFragment.PositiveClickListener() {
            @Override
            public void onClick() {
                dialogFragment.dismiss();
                if (listener != null)
                    listener.onClick();
            }
        });

        dialogFragment.setNegativeClickListener(new ConfirmDialogFragment.NegativeClickListener() {
            @Override
            public void onClick() {
                dialogFragment.dismiss();
                if (listener2 != null)
                    listener2.onClick();
            }
        });

        dialogFragment.show(getFragmentManager(), "showCommonDialog");
    }

    /**
     * 显示确认消息
     *
     * @param msg     消息内容
     * @param isError true错误消息（必须退出） false提示消息（可选择是否退出）
     */
    public void showExitInfoDialog(String msg, Boolean isError) {
        final ConfirmDialogFragment dialogFragment = new ConfirmDialogFragment();
        dialogFragment.setCancelable(true);
        dialogFragment.setMessage(msg);
        if (dialogFragment.isAdded()) {
            dialogFragment.dismiss();
            return;
        }
        if (!isError) {
            dialogFragment.setPositiveText(getString(R.string.trtclive_dialog_ok));
            dialogFragment.setNegativeText(getString(R.string.trtclive_dialog_cancel));
            dialogFragment.setPositiveClickListener(new ConfirmDialogFragment.PositiveClickListener() {
                @Override
                public void onClick() {
                    dialogFragment.dismiss();
                    exitMeetingConfirm();
                    finish();
                }
            });

            dialogFragment.setNegativeClickListener(new ConfirmDialogFragment.NegativeClickListener() {
                @Override
                public void onClick() {
                    dialogFragment.dismiss();
                }
            });
        } else {
            //当情况为错误的时候，直接停止推流
            dialogFragment.setPositiveText(getString(R.string.trtclive_dialog_ok));
            dialogFragment.setPositiveClickListener(new ConfirmDialogFragment.PositiveClickListener() {
                @Override
                public void onClick() {
                    dialogFragment.dismiss();
                }
            });
        }
        dialogFragment.show(getFragmentManager(), "ConfirmDialogFragment");
    }

    private MeetingVideoView.Listener mMeetingViewClick = new MeetingVideoView.Listener() {
        @Override
        public void onSingleClick(View view) {

        }

        @Override
        public void onDoubleClick(View view) {
            final MeetingVideoView videoView = (MeetingVideoView) view;
            MemberEntity entity = mStringMemberEntityMap.get(videoView.getMeetingUserId());
            final ViewParent viewGroup = videoView.getViewParent();
            if (viewGroup == mContainerFl) {//变为小的
                TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewFillMode(mShowUserId,
                        TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL);
                TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewRotation(mShowUserId, TRTCCloudDef.TRTC_VIDEO_ROTATION_0);
                entity.setShowOutSide(false);
                mContainerFl.removeView(videoView);
                mContainerFl.setVisibility(View.GONE);
                mShowUserId = "";
                videoView.refreshParent();
                mMessageImg.setVisibility(View.VISIBLE);
                if (!isWork) {
                    mBottomToolBarGroup.setVisibility(View.VISIBLE);
                }

            } else if (viewGroup instanceof ViewGroup) {//变为大的

                entity.setShowOutSide(true);
                mShowUserId = entity.getUserId();
                videoView.detach();
                mContainerFl.setVisibility(View.VISIBLE);
                videoView.addViewToViewGroup(mContainerFl);
                mMessageImg.setVisibility(View.INVISIBLE);
                if (!isWork) {
                    mBottomToolBarGroup.setVisibility(View.INVISIBLE);

                }

                if (entity.isLandscape) {
                    isLandscape = false;
                    btnRotate.setVisibility(View.VISIBLE);
                    TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewFillMode(entity.getUserId(),
                            TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FIT);
                } else {
                    btnRotate.setVisibility(View.GONE);
                }
            }
        }
    };

    private boolean isLandscape;

    public void rotate(View view) {
        if (isLandscape) {
            TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewRotation(mShowUserId, TRTCCloudDef.TRTC_VIDEO_ROTATION_0);
            isLandscape = false;
            mLvMessage.setVisibility(View.VISIBLE);

            TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewFillMode(mShowUserId,
                    TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FIT);
        } else {
            TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewRotation(mShowUserId, TRTCCloudDef.TRTC_VIDEO_ROTATION_270);
            isLandscape = true;
            mLvMessage.setVisibility(View.GONE);

            TRTCMeeting.sharedInstance(Utils.getApp()).setRemoteViewFillMode(mShowUserId,
                    TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL);
        }
    }


    private void showInputMsgDialog() {
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = mInputTextMsgDialog.getWindow().getAttributes();
        lp.width = display.getWidth(); //设置宽度
        mInputTextMsgDialog.getWindow().setAttributes(lp);
        mInputTextMsgDialog.setCancelable(true);
        mInputTextMsgDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mInputTextMsgDialog.show();
    }

    @Override
    public void onTextSend(String msg, boolean danmuOpen) {
        if (msg.length() == 0) {
            return;
        }
        byte[] byte_num = msg.getBytes(StandardCharsets.UTF_8);
        if (byte_num.length > 160) {
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
            return;
        }

        //消息回显
        TCChatEntity entity = new TCChatEntity();
        entity.setSenderName("我");
        entity.setContent(msg);
        entity.setType(TCConstants.TEXT_TYPE);
        notifyMsg(entity);

        mTRTCMeeting.sendRoomTextMsg(msg, new TRTCMeetingCallback.ActionCallback() {
            @Override
            public void onCallback(int code, String msg) {
                if (code == 0) {
                    ToastUtils.showShort("发送成功");
                } else {
                    notifyRemoveMsg(entity);
                    if (code == 10017) {
                        $.toast("你已被禁言");
                    } else {
                        ToastUtils.showShort("消息发生失败:" + code + ":" + msg);
                    }
                }
            }
        });
    }

    private void notifyMsg(final TCChatEntity entity) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mArrayListChatEntity.size() > 1000) {
                    while (mArrayListChatEntity.size() > 900) {
                        mArrayListChatEntity.remove(0);
                    }
                }
                mArrayListChatEntity.add(entity);
                mChatMsgListAdapter.notifyDataSetChanged();
            }
        });
    }

    //TODO 处理 禁言后删除消息
    protected void notifyRemoveMsg(TCChatEntity entity) {
        if (mChatMsgListAdapter != null) {
            runOnUiThread(() -> {
                mArrayListChatEntity.remove(entity);
                mChatMsgListAdapter.notifyDataSetChanged();
            });
        }
    }

    protected void handleTextMsg(TRTCMeetingDef.UserInfo userInfo, String text) {
        TCChatEntity entity = new TCChatEntity();
        entity.setSenderName(userInfo.userName);
        entity.setContent(text);
        entity.setType(TCConstants.TEXT_TYPE);
        notifyMsg(entity);
    }


//    /**
//     * 处理点赞信息
//     *
//     * @param userInfo
//     */
//    protected void handlePraiseMsg( TRTCMeetingDef.UserInfo userInfo) {
//        TCChatEntity entity = new TCChatEntity();
//        entity.setSenderName(getString(R.string.trtcliveroom_notification));
//        if (TextUtils.isEmpty(userInfo.userName)) {
//            entity.setContent(getString(R.string.trtcliveroom_user_click_like, userInfo.userId));
//        } else {
//            entity.setContent(getString(R.string.trtcliveroom_user_click_like, userInfo.userName));
//        }
//        mLayoutHeart.addFavor();
//        mHeartCount++;
//
//        entity.setType(TCConstants.PRAISE);
//        notifyMsg(entity);
//    }


}
