package com.bokecc.livemodule.replaymix;

import android.content.Context;
import android.util.Log;

import com.bokecc.sdk.mobile.live.Exception.DWLiveException;
import com.bokecc.sdk.mobile.live.ReplayPlayerListener;
import com.bokecc.sdk.mobile.live.common.log.ELog;
import com.bokecc.sdk.mobile.live.pojo.RoomInfo;
import com.bokecc.sdk.mobile.live.pojo.TemplateInfo;
import com.bokecc.sdk.mobile.live.replay.DWLiveLocalReplay;
import com.bokecc.sdk.mobile.live.replay.DWLiveLocalReplayListener;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplay;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplayListener;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplayLoginListener;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayBroadCastMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayChatMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayLoginInfo;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayPageInfo;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayQAMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.Viewer;
import com.bokecc.sdk.mobile.live.stream.live.rtc.HDMediaView;
import com.bokecc.sdk.mobile.live.widget.DocView;
import com.bokecc.stream.ali.CCBasePlayer;

import java.util.ArrayList;
import java.util.TreeSet;

/**
 * "在线&离线混合回放" 核心处理机制（用于支持在线回放&离线回放同页面播放）
 */
public class DWReplayMixCoreHandler {

    private static final String TAG = "DWReplayMixCoreHandler";

    /**
     * 当前播放视频的类型
     */
    enum PlayType {
        LIVE/*在线回放*/, LOCAL/*本地离线回放*/
    }

    //当前播放视频的类型
    private PlayType mCurrentPlayType;

    //显示文档控件
    private DocView docView;

    public static DWReplayMixCoreHandler mHandler = new DWReplayMixCoreHandler();

    public static DWReplayMixCoreHandler getInstance() {
        return mHandler;
    }
    // region ---------------------- 通用监听设置相关  ---------------------------------

    private DWReplayMixChatListener replayMixChatListener;

    /**
     * 设置回放聊天监听
     */
    public void setReplayMixChatListener(DWReplayMixChatListener chatListener) {
        replayMixChatListener = chatListener;
    }

    private DWReplayMixIntroListener dwReplayMixIntroListener;

    /**
     * 设置回放简介监听
     */
    public void setMixIntroListener(DWReplayMixIntroListener listener) {
        this.dwReplayMixIntroListener = listener;
    }

    private DWReplayMixQAListener dwReplayMixQAListener;

    /**
     * 设置回放问答监听
     */
    public void setReplayQAListener(DWReplayMixQAListener listener) {
        this.dwReplayMixQAListener = listener;
    }

    private DWReplayMixVideoListener dwReplayMixVideoListener;

    /**
     * 设置回放视频监听
     */
    public void setDwReplayMixVideoListener(DWReplayMixVideoListener dwReplayMixVideoListener) {
        this.dwReplayMixVideoListener = dwReplayMixVideoListener;
    }

    private DWReplayMixRoomListener dwReplayMixRoomListener;

    /**
     * 设置直播间状态回调监听
     */
    public void setDwReplayMixRoomListener(DWReplayMixRoomListener listener) {
        dwReplayMixRoomListener = listener;
    }

    // endregion

    // region ------------------------ 通用设置相关  ----------------------------------

    /**
     * 拖拽到指定位置
     *
     * @param progress
     */
    public void seekTo(int progress) {
        if (mCurrentPlayType == PlayType.LIVE) {
            DWLiveReplay.getInstance().seekTo(progress);
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            DWLiveLocalReplay.getInstance().seekTo(progress);
        }
    }

    /**
     * 设置文档展示控件
     *
     * @param docView 文档展示控件
     */
    public void setDocView(DocView docView) {
        this.docView = docView;
    }

    /**
     * 设置倍速
     *
     * @param v
     */
    public void setSpeed(float v) {
        if (mCurrentPlayType == PlayType.LIVE) {
            DWLiveReplay.getInstance().setSpeed(v);
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            DWLiveLocalReplay.getInstance().setSpeed(v);
        }
    }

    /**
     * 暂停
     */
    public void pause() {
        DWLiveReplay dwLiveReplay = DWLiveReplay.getInstance();
        if (dwLiveReplay != null) {
            dwLiveReplay.pause();
        }
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.pause();
        }
    }

    /**
     * 重新开始
     *
     * @param context
     */
    public void resume(Context context) {
        if (mCurrentPlayType == PlayType.LIVE) {
            DWLiveReplay.getInstance().start(context);
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            DWLiveLocalReplay.getInstance().start(context);
        }
    }

    /**
     * 销毁
     */
    public void destroy() {
        DWLiveReplay dwLiveReplay = DWLiveReplay.getInstance();
        if (dwLiveReplay != null) {
            dwLiveReplay.onDestroy();
        }
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.onDestroy();
        }
        docView = null;
    }

    public ReplayPlayerListener replayPlayerListener = new ReplayPlayerListener() {
        @Override
        public void onError(CCBasePlayer.CCPlayerError error, DWLiveException exception) {

        }

        @Override
        public void onVideoSizeChanged(int width, int height) {
            super.onVideoSizeChanged(width, height);
        }

        @Override
        public void onPlayStateChange(CCBasePlayer.CCPlayerStatus state) {
            super.onPlayStateChange(state);
            if (state == CCBasePlayer.CCPlayerStatus.PREPARED) {
                if (dwReplayMixRoomListener != null) {
                    dwReplayMixRoomListener.videoPrepared();
                    dwReplayMixRoomListener.showVideoDuration(getDuration());

                }
                if (dwReplayMixVideoListener != null) {
                    dwReplayMixVideoListener.onPlayerPrepared();
                }
            } else if (state == CCBasePlayer.CCPlayerStatus.BUFFERRING) {
                if (dwReplayMixVideoListener != null) {
                    dwReplayMixVideoListener.onBufferStart();
                }
            } else if (state == CCBasePlayer.CCPlayerStatus.BUFFERRED) {
                if (dwReplayMixVideoListener != null) {
                    dwReplayMixVideoListener.onBufferEnd();
                }
            } else if (state == CCBasePlayer.CCPlayerStatus.COMPLETED) {

            } else if (state == CCBasePlayer.CCPlayerStatus.SEEKED) {

            } else if (state == CCBasePlayer.CCPlayerStatus.PLAYING) {
                if (dwReplayMixVideoListener != null) {
                    dwReplayMixVideoListener.onRenderStart();
                }
            }
        }

        @Override
        public void onBufferSpeed(float speed) {
            super.onBufferSpeed(speed);
        }
    };
    // endregion

    // region ---------------------- 对外获取数据相关  ---------------------------------

    /**
     * 获取个人信息
     *
     * @return
     */
    public Viewer getViewer() {
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getViewer();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return null;
        }
        return null;
    }

    /**
     * 获取房间信息
     *
     * @return
     */
    public RoomInfo getRoomInfo() {
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getRoomInfo();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getRoomInfo();
        }
        return null;
    }

    /**
     * 是否播放中
     *
     * @return
     */
    public boolean isPlaying() {
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().isPlaying();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().isPlaying();
        }
        return false;
    }

    /**
     * 获取视频时长
     *
     * @return
     */
    public long getDuration() {
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getDuration();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getDuration();
        }
        return 0;
    }

    /**
     * 获取当前播放点
     *
     * @return
     */
    public long getCurrentPosition() {
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getCurrentPosition();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getCurrentPosition();
        }
        return 0;
    }

    /**
     * 获取当前的倍速
     *
     * @return
     */
    public float getSpeed() {
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getSpeed();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getSpeed();
        }
        return 0;
    }
    //是否有文档
    public boolean hasDoc(){
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getTemplateInfo().hasDoc();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getTemplateInfo().hasDoc();
        }
        return false;
    }
    //是否有聊天
    public boolean hasChat(){
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getTemplateInfo().hasChat();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getTemplateInfo().hasChat();
        }
        return false;
    }
    //是否有问答
    public boolean hasQA(){
        if (mCurrentPlayType == PlayType.LIVE) {
            return DWLiveReplay.getInstance().getTemplateInfo().hasQa();
        } else if (mCurrentPlayType == PlayType.LOCAL) {
            return DWLiveLocalReplay.getInstance().getTemplateInfo().hasQa();
        }
        return false;
    }
    // endregion

    // region ---------------------- 在线回放控制相关  ---------------------------------

    /**
     * 开始播放在线回放
     */
    public void startLiveReplay(ReplayLoginInfo info, final Context context) {
        if (mCurrentPlayType == PlayType.LOCAL) {
            DWLiveLocalReplay.getInstance().stop();
        } else if (mCurrentPlayType == PlayType.LIVE) {
            DWLiveReplay.getInstance().stop();
            docView.clearDrawInfo();
        }
        mCurrentPlayType = PlayType.LIVE;
        if (replayMixChatListener != null) {
            replayMixChatListener.onChatMessage(new TreeSet<ReplayChatMsg>());
        }
        // 执行登录操作
        DWLiveReplay.getInstance().startLogin(info, new DWLiveReplayLoginListener() {

            // 登录失败
            @Override
            public void onException(final DWLiveException exception) {
                ELog.e(TAG, exception.toString());

            }

            // 登录成功
            @Override
            public void onLogin(TemplateInfo templateInfo) {
                if (dwReplayMixRoomListener != null) {
                    dwReplayMixRoomListener.updateRoomTitle(DWLiveReplay.getInstance().getRoomInfo().getName());
                }
                if (dwReplayMixRoomListener != null) {
                    dwReplayMixRoomListener.onPlayOtherReplayVideo();
                }
                DWLiveReplay dwLiveReplay = DWLiveReplay.getInstance();
                if (dwLiveReplay != null) {
                    dwLiveReplay.setReplayListener(dwLiveReplayListener);
                    dwLiveReplay.setReplayDocView(docView);
                    dwLiveReplay.setReplayPlayerListener(replayPlayerListener);
                }
                DWLiveReplay.getInstance().start(context);
            }
        });

    }

    /**
     * 在线回放内部监听器
     */
    public DWLiveReplayListener dwLiveReplayListener = new DWLiveReplayListener() {

        /**
         * 提问回答信息
         *
         * @param qaMsgs 问答信息
         */
        @Override
        public void onQuestionAnswer(TreeSet<ReplayQAMsg> qaMsgs) {
            if (dwReplayMixQAListener != null) {
                dwReplayMixQAListener.onQuestionAnswer(qaMsgs);
            }
        }

        /**
         * 聊天信息
         *
         * @param replayChatMsgs 聊天信息
         */
        @Override
        public void onChatMessage(TreeSet<ReplayChatMsg> replayChatMsgs) {
            if (replayMixChatListener != null) {
                replayMixChatListener.onChatMessage(replayChatMsgs);
            }
        }

        @Override
        public void onBroadCastMessage(ArrayList<ReplayBroadCastMsg> broadCastMsgList) {

        }

        @Override
        public void onPlayBackStreamViewPrepared(HDMediaView hdMediaView) {
            if (dwReplayMixVideoListener != null) {
                dwReplayMixVideoListener.onPlayBackStreamViewPrepared(hdMediaView);
            }
        }

        @Override
        public void onPageInfoList(ArrayList<ReplayPageInfo> infoList) {

        }

        @Override
        public void onPageChange(String docId, String docName, int width, int height, int pageNum, int docTotalPage) {

        }

        @Override
        public void onException(DWLiveException exception) {

        }

        @Override
        public void onInitFinished() {
            if (dwReplayMixRoomListener != null) {
                dwReplayMixRoomListener.updateRoomTitle(DWLiveReplay.getInstance().getRoomInfo().getName());
            }
            if (dwReplayMixIntroListener != null) {
                dwReplayMixIntroListener.updateRoomInfo(DWLiveReplay.getInstance().getRoomInfo());
            }
        }


    };
    // endregion

    // region ---------------------- 离线回放控制相关  ---------------------------------

    /**
     * 开始播放离线回放
     */
    public void startLocalReplay(String localPath, Context context) {
        if (mCurrentPlayType == PlayType.LOCAL) {
            DWLiveLocalReplay.getInstance().stop();
        } else if (mCurrentPlayType == PlayType.LIVE) {
            DWLiveReplay.getInstance().stop();
            docView.clearDrawInfo();
        }
        if (replayMixChatListener != null) {
            replayMixChatListener.onChatMessage(new TreeSet<ReplayChatMsg>());
        }

        DWLiveLocalReplay liveLocalReplay = DWLiveLocalReplay.getInstance();
        mCurrentPlayType = PlayType.LOCAL;
        if (dwReplayMixRoomListener != null) {
            dwReplayMixRoomListener.onPlayOtherReplayVideo();
        }
        liveLocalReplay.setReplayParams(localReplayListener, docView, localPath);
        liveLocalReplay.setReplayPlayerListener(replayPlayerListener);
        liveLocalReplay.start(context);
    }

    /**
     * 本地回放内部监听器
     */
    private final DWLiveLocalReplayListener localReplayListener = new DWLiveLocalReplayListener() {
        @Override
        public void onQuestionAnswer(TreeSet<ReplayQAMsg> qaMsgs) {
            if (dwReplayMixQAListener != null) {
                dwReplayMixQAListener.onQuestionAnswer(qaMsgs);
            }
        }

        @Override
        public void onChatMessage(TreeSet<ReplayChatMsg> replayChatMsgs) {
            Log.d(TAG, "localReplayListener onChatMessage: ");
            if (replayMixChatListener != null) {
                replayMixChatListener.onChatMessage(replayChatMsgs);
            }
        }

        @Override
        public void onBroadCastMessage(ArrayList<ReplayBroadCastMsg> broadCastMsgList) {

        }

        @Override
        public void onPageInfoList(ArrayList<ReplayPageInfo> infoList) {

        }

        @Override
        public void onPageChange(String docId, String docName, int pageNum, int docTotalPage) {

        }

        @Override
        public void onException(DWLiveException exception) {

        }

        @Override
        public void onInfo(TemplateInfo templateInfo, RoomInfo roomInfo) {
            if (dwReplayMixRoomListener != null) {
                dwReplayMixRoomListener.updateRoomTitle(roomInfo.getName());
            }
            if (dwReplayMixIntroListener != null) {
                dwReplayMixIntroListener.updateRoomInfo(roomInfo);
            }
        }

        @Override
        public void onInitFinished() {

        }

        @Override
        public void onPlayBackStreamViewPrepared(HDMediaView hdMediaView) {
            if (dwReplayMixVideoListener != null) {
                dwReplayMixVideoListener.onPlayBackStreamViewPrepared(hdMediaView);
            }
        }
    };
    // endregion


}
