package com.bokecc.dwlivedemo.activity;

import static android.view.View.VISIBLE;

import android.app.Activity;
import android.app.AppOpsManager;
import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Rational;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bokecc.dwlivedemo.DWApplication;
import com.bokecc.dwlivedemo.R;
import com.bokecc.dwlivedemo.base.BaseActivity;
import com.bokecc.dwlivedemo.download.FileUtil;
import com.bokecc.dwlivedemo.popup.ExitPopupWindow;
import com.bokecc.dwlivedemo.popup.FloatingPopupWindow;
import com.bokecc.dwlivedemo.utils.NetUtils;
import com.bokecc.dwlivedemo.utils.NotificationUtil;
import com.bokecc.livemodule.base.BaseReplayRoomLayout;
import com.bokecc.livemodule.live.chat.OnChatComponentClickListener;
import com.bokecc.livemodule.localplay.DWLocalReplayCoreHandler;
import com.bokecc.livemodule.localplay.chat.LocalReplayChatComponent;
import com.bokecc.livemodule.localplay.doc.LocalReplayDocComponent;
import com.bokecc.livemodule.localplay.intro.LocalReplayIntroComponent;
import com.bokecc.livemodule.localplay.qa.LocalReplayQAComponent;
import com.bokecc.livemodule.localplay.room.LocalReplayRoomLayout;
import com.bokecc.livemodule.localplay.video.LocalReplayVideoView;
import com.bokecc.livemodule.popup.DLNAPopupWindow;
import com.bokecc.livemodule.view.CustomToast;
import com.bokecc.sdk.mobile.live.Exception.DWLiveException;
import com.bokecc.sdk.mobile.live.pojo.PlayUrlInfoBean;
import com.bokecc.sdk.mobile.live.replay.DWLiveLocalReplay;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.bokecc.dwlivedemo.utils.NotificationUtil.ACTION_DESTROY;
import static com.bokecc.dwlivedemo.utils.NotificationUtil.ACTION_PLAY_PAUSE;
import static com.bokecc.livemodule.live.chat.adapter.LivePublicChatAdapter.CONTENT_IMAGE_COMPONENT;
import static com.bokecc.livemodule.live.chat.adapter.LivePublicChatAdapter.CONTENT_ULR_COMPONET;

/**
 * 离线回放播放页
 */
public class LocalReplayPlayActivity extends BaseActivity implements DWLocalReplayCoreHandler.LocalTemplateUpdateListener {

    private static final String TAG = "LocalReplayPlayActivity";

    public static String DOWNLOAD_DIR;


    // <editor-fold defaultstate="collapsed" desc="实例变量">

    private View mRoot;
    private String mPlayPath;  // CCR文件名
    private LocalReplayVideoView mReplayVideoView;
    private RelativeLayout mReplayVideoContainer;
    private LocalReplayRoomLayout mReplayRoomLayout;
    private LinearLayout mReplayMsgLayout;

    // 悬浮弹窗（用于展示文档和视频）
    private FloatingPopupWindow mLocalReplayFloatingView;

    // 广播接收者
    private BroadcastReceiver broadcastReceiver;

    private boolean isVideoMain;       // 是否是视频大屏
    private String key;
    private String videoId;
    private PictureInPictureParams.Builder mPictureInPictureParamsBuilder;


    List<View> mLiveInfoList = new ArrayList<>();
    List<Integer> mIdList = new ArrayList<>();
    List<RadioButton> mTagList = new ArrayList<>();

    ViewPager mViewPager;

    RadioGroup mRadioGroup;
    RadioButton mIntroTag;
    RadioButton mQaTag;
    RadioButton mChatTag;
    RadioButton mDocTag;

    PagerAdapter adapter;

    //因为息屏会重新触发一次onLocalTemplateUpdate回调
    private boolean isComponentsInit = false;

    // 简介组件
    LocalReplayIntroComponent mIntroComponent;
    // 问答组件
    LocalReplayQAComponent mQaLayout;
    // 聊天组件
    LocalReplayChatComponent mChatLayout;
    // 文档组件
    LocalReplayDocComponent mDocLayout;
    private FloatingPopupWindow.FloatDismissListener floatDismissListener = new FloatingPopupWindow.FloatDismissListener() {
        @Override
        public void dismiss() {
            mReplayRoomLayout.setSwitchText(true);
        }
    };

    private ExitPopupWindow playerErrorWindow;
    ExitPopupWindow mExitPopupWindow;

    private boolean isExitPicInPicMode = false;

    private DLNAPopupWindow dlnaPopupWindow;


    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="生命周期">
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // 请求全屏
//        requestFullScreenFeature();
        // 请求状态栏黑色
        requestStatusBarBlack(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_replay_play);
        DOWNLOAD_DIR = FileUtil.getCCDownLoadPath(this);
        Intent intent = getIntent();
        String fileName = intent.getStringExtra("fileName");
        try {
            key = intent.getStringExtra("key");
            videoId = intent.getStringExtra("videoId");
        } catch (Exception e) {
            e.printStackTrace();
        }
        isVideoMain = getIntent().getBooleanExtra("isVideoMain", true);
        if (TextUtils.isEmpty(fileName)) {
            CustomToast.showToast(this, "CCR文件名为空，播放失败！", Toast.LENGTH_LONG);
            return;
        }

        File oriFile = new File(DOWNLOAD_DIR, fileName);
        mPlayPath = getUnzipDir(oriFile);

        DWLocalReplayCoreHandler handler = DWLocalReplayCoreHandler.getInstance();
        handler.setLocalTemplateUpdateListener(this);

        // 设置进度记忆唯一标识
        handler.setRecordTag(mPlayPath);
        //设置播放密钥
        if (!TextUtils.isEmpty(key)) {
            handler.setKey(key);
        }
        if (!TextUtils.isEmpty(videoId)) {
            handler.setVideoId(videoId);
        }

        //解析文件名中的id
        String accountId = "";
        String replayId = "";
        String[] parts = fileName.split("[-.]");  // 按 `-` 或 `.` 分割
        if(parts.length >= 2){
            accountId = parts[0];  // "a"
            replayId = parts[1];  // "b"
        }

        initViews(accountId,replayId);
        initReceiver();

        if (mReplayVideoView != null) {
            mReplayVideoView.start();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        if(mReplayRoomLayout != null){
            mReplayRoomLayout.initLocalSubtitle();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isExitPicInPicMode){
            isExitPicInPicMode = false;
            DWApplication.getInstance().setInPictureInPictureMode(false);
            if (!isFinishing()) {
                Log.i(TAG, "onPictureInPictureModeChanged 2");
                //显示其他元素
                mReplayMsgLayout.setVisibility(VISIBLE);
                if (DWLocalReplayCoreHandler.getInstance().hasDocView()) {
                    mLocalReplayFloatingView.show(mRoot);
                }
            }
        }
        NotificationUtil.cancelNotification(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (!isFinishing()) {
            NotificationUtil.sendLocalReplayNotification(this);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (DWApplication.getInstance().isInPictureInPictureMode()){
                DWApplication.getInstance().setInPictureInPictureMode(false);
                Log.i(TAG,"isInPictureInPictureMode, finish");
                finish();
            }
        }

    }

    @Override
    protected void onDestroy() {
        if (mRoot != null && mRoot.getHandler() != null) {
            mRoot.getHandler().removeCallbacksAndMessages(null);
        }
        super.onDestroy();
        if (mLocalReplayFloatingView!=null){
            mLocalReplayFloatingView.dismiss();
        }
        mReplayVideoView.destroy();
        NotificationUtil.cancelNotification(this);
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
        Log.i(TAG, "onPictureInPictureModeChanged:" + isInPictureInPictureMode);
        if (isInPictureInPictureMode) {
            DWApplication.getInstance().setInPictureInPictureMode(true);
            //隐藏其他元素，只保留视频元素
        } else {
            //到onResume在判断是否退出画中画
            isExitPicInPicMode = true;
        }

    }

    @Override
    public void onUserLeaveHint() {
        Log.i(TAG, "onUserLeaveHint:");

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode==KeyEvent.KEYCODE_VOLUME_UP){
            // 音量增大按键被按下
            if (dlnaPopupWindow!=null&&dlnaPopupWindow.onVolumeChange(true)){
                return true;
            }
        }else if (keyCode==KeyEvent.KEYCODE_VOLUME_DOWN){
            // 音量增大按键被按下
            if (dlnaPopupWindow!=null&&dlnaPopupWindow.onVolumeChange(false)){
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (!isPortrait()) {
            quitFullScreen();
            return;
        }
        finish();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 横屏隐藏状态栏
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility(true));
        } else {
            getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility(false));
        }
        //调整窗口的位置
        if (mLocalReplayFloatingView != null) {
            mLocalReplayFloatingView.onConfigurationChanged(newConfig.orientation);
        }
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="对外方法">

    /**
     * 启动回放界面
     *
     * @param activity    activity
     * @param isVideoMain 是否是以大屏为主
     * @param fileName    离线文件路径，用于记忆播放
     */
    public static void openActivity(Activity activity, boolean isVideoMain, String fileName) {
        Intent intent = new Intent(activity, LocalReplayPlayActivity.class);
        intent.putExtra("isVideoMain", isVideoMain);
        intent.putExtra("fileName", fileName);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    /**
     * 启动回放界面
     *
     * @param activity    activity
     * @param isVideoMain 是否是以大屏为主
     * @param fileName    离线文件路径，用于记忆播放
     */
    public static void openActivity(Activity activity, boolean isVideoMain, String fileName, String key, String videoId) {
        Intent intent = new Intent(activity, LocalReplayPlayActivity.class);
        intent.putExtra("isVideoMain", isVideoMain);
        intent.putExtra("fileName", fileName);
        intent.putExtra("key", key);
        intent.putExtra("videoId", videoId);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    public static String getUnzipDir(File oriFile) {

        String fileName = oriFile.getName();

        StringBuilder sb = new StringBuilder();
        sb.append(oriFile.getParent());
        sb.append("/");
        int index = fileName.indexOf(".");
        if (index == -1) {
            sb.append(fileName);
        } else {
            sb.append(fileName.substring(0, index));
        }

        return sb.toString();
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="私有方法">

    /**
     * 初始化ViewPager
     */
    private void initViewPager() {
        adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return mLiveInfoList.size();
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                container.addView(mLiveInfoList.get(position));

                return mLiveInfoList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mLiveInfoList.get(position));
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
        };

        mViewPager.setAdapter(adapter);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTagList.get(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                mViewPager.setCurrentItem(mIdList.indexOf(i), true);
            }
        });
        if (mTagList != null && mTagList.size() > 0) {
            mTagList.get(0).performClick();
        }
    }


    /**
     * 根据直播间模版初始化相关组件
     */
    private void initComponents() {
        if (isComponentsInit) {
            return;
        }
        isComponentsInit = true;

        mLiveInfoList.clear();
        mIdList.clear();

        DWLocalReplayCoreHandler dwLocalReplayCoreHandler = DWLocalReplayCoreHandler.getInstance();
        if (dwLocalReplayCoreHandler == null) {
            return;
        }
        // 初始化视频控件
        initVideoLayout();
        // 判断当前直播间模版是否有"文档"功能
        if (dwLocalReplayCoreHandler.hasDocView()) {
            initDocLayout();
        }
        // 判断当前直播间模版是否有"聊天"功能
        if (dwLocalReplayCoreHandler.hasChatView()) {
            initChatLayout();
        }
        // 判断当前直播间模版是否有"问答"功能
        if (dwLocalReplayCoreHandler.hasQaView()) {
            initQaLayout();
        }
        // 直播间简介
        initIntroLayout();
        // 模块初始化
        initViewPager();

    }

    // 初始化聊天布局区域
    private void initChatLayout() {
        mIdList.add(R.id.live_portrait_info_chat);
        mTagList.add(mChatTag);
        mChatTag.setVisibility(View.VISIBLE);
        mLiveInfoList.add(mChatLayout);
    }

    // 初始化问答布局区域
    private void initQaLayout() {
        mIdList.add(R.id.live_portrait_info_qa);
        mTagList.add(mQaTag);
        mQaTag.setVisibility(View.VISIBLE);
        mLiveInfoList.add(mQaLayout);
    }

    // 初始化简介布局区域
    private void initIntroLayout() {
        mIdList.add(R.id.live_portrait_info_intro);
        mTagList.add(mIntroTag);
        mIntroTag.setVisibility(View.VISIBLE);
        mLiveInfoList.add(mIntroComponent);
    }

    // 初始化文档布局区域
    private void initDocLayout() {
        mDocLayout = new LocalReplayDocComponent(this);
        if (isVideoMain) {
            mLocalReplayFloatingView.addView(mDocLayout);
        } else {
            if (DWLocalReplayCoreHandler.getInstance().hasDocView()) {
                mReplayVideoContainer.addView(mDocLayout);
                mDocLayout.setDocScrollable(true);
            }
        }
        // 显示小窗
        showFloatingDocLayout();
    }

    // 初始化视频布局区域
    private void initVideoLayout() {
        if (isVideoMain) {
            mReplayVideoContainer.addView(mReplayVideoView);
        } else {
            if (DWLocalReplayCoreHandler.getInstance().hasDocView()) {
                mLocalReplayFloatingView.addView(mReplayVideoView);
            } else {
                mReplayVideoContainer.addView(mReplayVideoView);
            }
        }
    }

    // 注册广播接收者
    private void initReceiver() {
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (NotificationUtil.ACTION_PLAY_PAUSE.equals(action)) {
                    if (mReplayRoomLayout != null) {
                        mReplayRoomLayout.changePlayerStatus();
                    }
                    NotificationUtil.sendLocalReplayNotification(LocalReplayPlayActivity.this);
                } else if (NotificationUtil.ACTION_DESTROY.equals(action)) {
                    exit();
                }
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_PLAY_PAUSE);
        intentFilter.addAction(ACTION_DESTROY);
        registerReceiver(broadcastReceiver, intentFilter);
    }


    private void initViews(String accountId, String replayId) {
        mRoot = getWindow().getDecorView().findViewById(android.R.id.content);
        mReplayVideoContainer = findViewById(R.id.rl_video_container);
        mReplayRoomLayout = findViewById(R.id.replay_room_layout);
        mReplayRoomLayout.setReplayRoomStatusListener(roomStatusListener);
        mReplayMsgLayout = findViewById(R.id.ll_pc_replay_msg_layout);
        mViewPager = findViewById(R.id.live_portrait_container_viewpager);
        mRadioGroup = findViewById(R.id.rg_infos_tag);
        mIntroTag = findViewById(R.id.live_portrait_info_intro);
        mQaTag = findViewById(R.id.live_portrait_info_qa);
        mChatTag = findViewById(R.id.live_portrait_info_chat);
        mDocTag = findViewById(R.id.live_portrait_info_document);

        mLocalReplayFloatingView = new FloatingPopupWindow(this);
        mLocalReplayFloatingView.setFloatDismissListener(floatDismissListener);

        // 视频显示区域
        mReplayVideoView = new LocalReplayVideoView(this);
        mReplayVideoView.setPlayPath(mPlayPath);

        mReplayVideoView.setOnPlayerErrorListener(new LocalReplayVideoView.OnPlayerErrorListener() {
            @Override
            public void onPlayError(int code, DWLiveException exception) {
                showPlayerErrorDialog();
            }
        });

        //退出确认弹框
        mExitPopupWindow = new ExitPopupWindow(this);

        mChatLayout = new LocalReplayChatComponent(this);
        mQaLayout = new LocalReplayQAComponent(this);
        mIntroComponent = new LocalReplayIntroComponent(this);
        mDocLayout = new LocalReplayDocComponent(this);

        mReplayRoomLayout.setOnPicInPicListener(new BaseReplayRoomLayout.OnPicInPicListener() {
            @Override
            public void onPicInPicClick() {
                //如果当前是横屏状态，强制切换竖屏，然后进入画中画模式
                if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
                    quitFullScreen();
                    mReplayRoomLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            checkAndEnterPicInPicMode();
                        }
                    }, 1000);
                }else {
                    checkAndEnterPicInPicMode();
                }


            }
        });
        mReplayRoomLayout.init(isVideoMain, accountId,replayId);
        mReplayRoomLayout.setActivity(this);

        mReplayRoomLayout.setOnDlnaListener(new BaseReplayRoomLayout.OnDlnaListener() {
            @Override
            public void onDlnaClick(PlayUrlInfoBean infoBean) {
                //如果是横屏，切回竖屏
                if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
                    quitFullScreen();
                }
                if (dlnaPopupWindow==null){
                    dlnaPopupWindow = new DLNAPopupWindow(LocalReplayPlayActivity.this);
                    dlnaPopupWindow.setOnExitListener(new DLNAPopupWindow.OnExitListener() {
                        @Override
                        public void onExit() {
                            DWLiveLocalReplay.getInstance().resume();

                        }
                    });
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    if (infoBean!=null&&infoBean.getVideos()!=null&& !infoBean.getVideos().isEmpty() &&infoBean.getVideos().get(0)!=null&&infoBean.getVideos().get(0).getVideoStream()!=null&& !infoBean.getVideos().get(0).getVideoStream().isEmpty()){
                        String url = infoBean.getVideos().get(0).getVideoStream().get(0);
                        Log.i(TAG,"投屏url："+url);
                        //如果为加密视频，需要将返回的url中的127.0.0.1的ip转换为手机局域网ip，由于涉及到网络ACCESS_NETWORK_STATE权限，所以不在sdk中处理
                        String playUrl = url;
                        String localIp = NetUtils.getLocalIpAddress(LocalReplayPlayActivity.this);
                        if (url.contains(".pcm")){
                            playUrl = url.replace("127.0.0.1",localIp);
                        }else {
                            //离线回放本地mp4视频，需要走sdk的代理
                            playUrl = "http://"+localIp+":3176/c.mp4";

                        }

                        dlnaPopupWindow.show(playUrl,LocalReplayPlayActivity.this,mRoot, Gravity.CENTER,0,0);
                        DWLiveLocalReplay.getInstance().pause();
                    }
                }
            }
        });
        mChatLayout.setOnChatComponentClickListener(new OnChatComponentClickListener() {
            @Override
            public void onClickChatComponent(Bundle bundle) {
                if (bundle == null) {
                    return;
                }

                String type = bundle.getString("type");
                if (CONTENT_IMAGE_COMPONENT.equals(type)) {
                    Intent intent = new Intent(LocalReplayPlayActivity.this, ImageDetailsActivity.class);
                    intent.putExtra("imageUrl", bundle.getString("url"));
                    startActivity(intent);
                } else if (CONTENT_ULR_COMPONET.equals(type)) {
                    Uri uri = Uri.parse(bundle.getString("url"));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }

            }
        });
    }


    private void checkAndEnterPicInPicMode() {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
            //进入画中画模式
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
                if (AppOpsManager.MODE_ALLOWED == appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE,
                        getApplicationInfo().uid, getPackageName())) {

                    //画中画开关开启
                    if (mPictureInPictureParamsBuilder == null) {
                        mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder();
                    }
                    //如果是文档大屏模式，需要切换一下
                    if (DWLocalReplayCoreHandler.getInstance().hasDocView() && !isVideoMain) {
                        switchView(true);
                        mReplayRoomLayout.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                minimize();
                            }
                        }, 1000);
                    } else {
                        minimize();
                    }
                } else {
                    //画中画开关关闭
                    toastOnUiThread("请前往设置手动打开画中画开关！");

                }


            } else {
                toastOnUiThread("版本不支持画中画功能！");
            }
        } else {
            toastOnUiThread("设备不支持画中画功能！");
        }
    }

    private void showPlayerErrorDialog() {
        playerErrorWindow = new ExitPopupWindow(this);
        playerErrorWindow.setContent("暂时无法播放该文件");
        playerErrorWindow.setHideCancelButton();
        playerErrorWindow.show(mRoot);
        playerErrorWindow.setConfirmExitRoomListener(new ExitPopupWindow.ConfirmExitRoomListener() {
            @Override
            public void onConfirmExitRoom() {
                if (playerErrorWindow!=null){
                    playerErrorWindow.dismiss();
                }
            }
        });
    }

    // 展示文档悬浮窗布局
    private void showFloatingDocLayout() {
        DWLocalReplayCoreHandler dwLocalReplayCoreHandler = DWLocalReplayCoreHandler.getInstance();
        if (dwLocalReplayCoreHandler == null) {
            return;
        }
        // 判断当前直播间模版是否有"文档"功能，如果没文档，则小窗功能也不应该有
        if (dwLocalReplayCoreHandler.hasDocView()) {
            mLocalReplayFloatingView.show(mRoot);
        }
    }

    private void switchView(boolean isVideoMain) {
        if (mReplayVideoView.getParent() != null) {
            ((ViewGroup) mReplayVideoView.getParent()).removeView(mReplayVideoView);
        }
        if (mDocLayout.getParent() != null) {
            ((ViewGroup) mDocLayout.getParent()).removeView(mDocLayout);
        }
        if (isVideoMain) {
            // 缓存视频的切换前的画面
            mLocalReplayFloatingView.addView(mDocLayout);
            mReplayVideoContainer.addView(mReplayVideoView);
            mDocLayout.setDocScrollable(false);//webview不可切换
        } else {
            // 缓存视频的切换前的画面
            mLocalReplayFloatingView.addView(mReplayVideoView);
            mReplayVideoContainer.addView(mDocLayout);
            mDocLayout.setDocScrollable(true);//webview不可切换
        }
    }

    private void quitFullScreen() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mReplayMsgLayout.setVisibility(View.VISIBLE);
        getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility(false));
    }


    // 退出房间
    private void exit() {
        if (mExitPopupWindow!=null){
            mExitPopupWindow.dismiss();
        }

        finish();
    }


    private static int getSystemUiVisibility(boolean isFull) {
        if (isFull) {
            int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                flags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            }
            return flags;
        } else {
            return View.SYSTEM_UI_FLAG_VISIBLE;
        }

    }

    private void minimize() {
        //1. 隐藏下方布局
        mReplayMsgLayout.setVisibility(View.GONE);
        //2. 隐藏浮窗
        if (DWLocalReplayCoreHandler.getInstance().hasDocView()) {
            if (mLocalReplayFloatingView!=null){
                mLocalReplayFloatingView.dismiss();
            }
        }
        //3. 隐藏记忆播放提示
        mReplayRoomLayout.hideMemoryPlayTip();
        //4. 隐藏视频控制条
        mReplayRoomLayout.hideController();


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Rational aspectRatio = new Rational(mReplayVideoView.getWidth(), mReplayVideoView.getHeight());
            mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
            enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
        }
    }

    // </editor-fold>


    // <editor-fold defaultstate="collapsed" desc="回调函数">
    private final LocalReplayRoomLayout.ReplayRoomStatusListener roomStatusListener =
            new LocalReplayRoomLayout.ReplayRoomStatusListener() {

                @Override
                public void switchVideoDoc(boolean isVideoMain) {
                    switchView(isVideoMain);
                }

                @Override
                public void openVideoDoc() {
                    if (mReplayRoomLayout.isVideoMain()) {
                        mLocalReplayFloatingView.show(mRoot);
                        if (mDocLayout.getParent() != null) {
                            ((ViewGroup) mDocLayout.getParent()).removeView(mDocLayout);
                        }
                        mLocalReplayFloatingView.addView(mDocLayout);
                    } else {
                        mLocalReplayFloatingView.show(mRoot);
                        if (mReplayVideoView.getParent() != null) {
                            ((ViewGroup) mReplayVideoView.getParent()).removeView(mReplayVideoView);
                        }
                        mLocalReplayFloatingView.addView(mReplayVideoView);
                    }
                }

                @Override
                public void closeRoom() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 如果当前状态是竖屏，则弹出退出确认框，否则切换为竖屏
                            if (isPortrait()) {
                                if (mExitPopupWindow != null) {
                                    mExitPopupWindow.setConfirmExitRoomListener(confirmExitRoomListener);
                                    mExitPopupWindow.show(mRoot);
                                }
                            } else {
                                quitFullScreen();
                            }
                        }
                    });
                }

                @Override
                public void fullScreen() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                            mReplayMsgLayout.setVisibility(View.GONE);
                            getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility(true));
                        }
                    });
                }

                @Override
                public void onClickDocScaleType(int scaleType) {
                    if (mDocLayout != null) {
                        mDocLayout.setScaleType(scaleType);
                    }
                }
            };
    ExitPopupWindow.ConfirmExitRoomListener confirmExitRoomListener = new ExitPopupWindow.ConfirmExitRoomListener() {
        @Override
        public void onConfirmExitRoom() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    exit();
                }
            });
        }
    };

    /**
     * 模板信息获取监听器，当本地回放模板信获取成功后该方法将会回调
     */
    @Override
    public void onLocalTemplateUpdate() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                initComponents();
            }
        });
    }
    // </editor-fold>


}
