package com.bokecc.livemodule.localplay;

import android.content.Context;
import android.widget.TextView;

import com.bokecc.dwlivedemo.DWApplication;
import com.bokecc.livemodule.replay.video.DWReplayVideoListener;
import com.bokecc.livemodule.utils.ProRecordWorker;
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.pojo.ReplayBroadCastMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayChatMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayLiveInfo;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayPageInfo;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayQAMsg;
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 DWLocalReplayCoreHandler {
    private final static String TAG = DWLocalReplayCoreHandler.class.getSimpleName();

    private static DWLocalReplayCoreHandler dwLocalReplayCoreHandler;

    // 进度记忆功能
    private ProRecordWorker proRecordWorker;


    /**
     * 获取DWLocalReplayCoreHandler单例的实例
     */
    public static DWLocalReplayCoreHandler getInstance() {
        if (dwLocalReplayCoreHandler == null){
            synchronized (DWLocalReplayCoreHandler.class) {
                if (dwLocalReplayCoreHandler == null){
                    dwLocalReplayCoreHandler = new DWLocalReplayCoreHandler();
                }
            }
        }
        return dwLocalReplayCoreHandler;
    }

    /**
     * 私有构造函数
     */
    private DWLocalReplayCoreHandler() {

    }


    //******************************* 各类功能模块监听相关 ***************************************/
    private DWLocalDWReplayChatListener localDWReplayChatListener;

    /**
     * 设置聊天监听
     *
     * @param listener listener
     */
    public void setLocalReplayChatListener(DWLocalDWReplayChatListener listener) {
        localDWReplayChatListener = listener;
    }

    private DWLocalDWReplayIntroListener localDWReplayIntroListener;

    /**
     * 设置直播间信息监听
     *
     * @param listener listener
     */
    public void setLocalIntroListener(DWLocalDWReplayIntroListener listener) {
        this.localDWReplayIntroListener = listener;
    }

    private DWLocalDWReplayQAListener localDWReplayQAListener;

    /**
     * 设置问答监听
     *
     * @param listener listener
     */
    public void setReplayQAListener(DWLocalDWReplayQAListener listener) {
        localDWReplayQAListener = listener;
    }

    private DWLocalReplayRoomListener localReplayRoomListener;

    /**
     * 设置本地回放信息监听
     *
     * @param listener listener
     */
    public void setLocalDwReplayRoomListener(DWLocalReplayRoomListener listener) {
        localReplayRoomListener = listener;
    }

    private LocalTemplateUpdateListener localTemplateUpdateListener;

    /**
     * 设置回放模板监听
     *
     * @param listener listener
     */
    public void setLocalTemplateUpdateListener(LocalTemplateUpdateListener listener) {
        localTemplateUpdateListener = listener;
    }
    private DWReplayVideoListener dwReplayVideoListener;
    public void setReplayVideoListener(DWReplayVideoListener dwReplayVideoListener) {
        this.dwReplayVideoListener =dwReplayVideoListener;
    }

    /**
     * 回放是否有文档
     *
     * @return true:有
     */
    public boolean hasDocView() {
        return mTemplateInfo != null && mTemplateInfo.hasDoc();
    }
    /**
     * 回放是否有聊天组件
     *
     * @return true:有
     */
    public boolean hasChatView() {
        return mTemplateInfo != null && "1".equals(mTemplateInfo.getChatView());
    }

    /**
     * 直播间是否有问答组件
     *
     * @return true:有
     */
    public boolean hasQaView() {
        return mTemplateInfo != null && "1".equals(mTemplateInfo.getQaView());
    }

    public ReplayLiveInfo getReplayLiveInfo() {
        DWLiveLocalReplay dwLiveReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveReplay != null) {
            return dwLiveReplay.getReplayLiveInfo();
        }
        return null;
    }


    //******************************* 设置"播放"组件/控件相关 ***************************************/
    //播放内容地址
    private String playPath;
    //显示文档的控件
    private DocView docView;
    private String key;
    private String videoId;
    //回放模板信息
    private TemplateInfo mTemplateInfo;

    // 进度记忆控件和唯一id
    private TextView proRecordJump;
    private String recordTag;


    /**
     * 设置播放路径
     *
     * @param path ccr文件地址
     */
    public void setPlayPath(String path) {
        this.playPath = path;
        setDWLocalReplayPlayParams();
    }

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

    /**
     * 设置密钥
     *
     * @param key 加密离线回放播放密钥
     */
    public void setKey(String key) {
        this.key = key;
        setDWLocalReplayPlayParams();
    }
    /**
     * 设置密钥
     *
     * @param videoId 加密视频id
     */
    public void setVideoId(String videoId) {
        this.videoId = videoId;
        setDWLocalReplayPlayParams();
    }

    /**
     * 设置播放的参数
     */
    private void setDWLocalReplayPlayParams() {
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.setReplayParams(dwLiveLocalReplayListener, docView, playPath,key,videoId);
            dwLiveLocalReplay.setReplayPlayerListener(playbackPlayerListener);
        }
    }


    /**
     * 设置进度记忆控件
     *
     * @param textView textView
     */
    public void setProRecordJumpTextView(TextView textView) {
        proRecordJump = textView;
    }

    /**
     * 设置进度记忆唯一标识
     *
     * @param recordTag recordTag
     */
    public void setRecordTag(String recordTag) {
        this.recordTag = recordTag;
    }

    //******************************* 视频播放相关 ***************************************/

    /**
     * 开始播放
     */
    public void start(Context context) {
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.start(context);
        }
    }

    public void pause(){
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.pause();
        }
    }
    /**
     * 停止播放
     */
    public void stop() {
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.stop();
        }
    }
    public void seekTo(long time) {
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.seekTo(time);
        }
    }
    /**
     * 重试播放
     *
     * @param time:时间点，是否强制更新流地址
     */
    public void retryReplay(long time) {
        DWLiveLocalReplay dwLiveReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveReplay != null) {
            dwLiveReplay.retryReplay(time);
        }
    }
    /**
     * 释放资源
     */
    public void destroy() {
        DWLiveLocalReplay dwLiveLocalReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveLocalReplay != null) {
            dwLiveLocalReplay.onDestroy();
        }
        mTemplateInfo = null;
        dwLocalReplayCoreHandler = null;
    }

    private ReplayPlayerListener playbackPlayerListener = new ReplayPlayerListener() {
        @Override
        public void onError(CCBasePlayer.CCPlayerError error, DWLiveException exception) {
            if (localReplayRoomListener != null) {
                localReplayRoomListener.onPlayError(-1);
            }
        }

        @Override
        public void onPlayStateChange(CCBasePlayer.CCPlayerStatus state) {
            super.onPlayStateChange(state);
            if (state == CCBasePlayer.CCPlayerStatus.PREPARED){
                if (localReplayRoomListener != null) {
                    localReplayRoomListener.videoPrepared(DWLiveLocalReplay.getInstance().getDuration());
                }
                if (dwReplayVideoListener !=null){
                    dwReplayVideoListener.onPrepared();
                }
                startProRecord(DWApplication.getInstance().getContext(), new ProRecordWorker.UICallback(){

                    @Override
                    public void proRecordSeek(long time) {
                        if (isPlaying()) {
                            seekTo(time);
                        } else {
                            //如果不是播放中  为了安全起见 重新播放
                            retryReplay(time);
                        }
                    }
                });
            }else if (state == CCBasePlayer.CCPlayerStatus.BUFFERRING){
                if (dwReplayVideoListener !=null){
                    dwReplayVideoListener.onBufferStart();
                }
                if (localReplayRoomListener != null) {
                    localReplayRoomListener.onBufferStart();
                }
            }else if (state == CCBasePlayer.CCPlayerStatus.BUFFERRED){
                if (dwReplayVideoListener !=null){
                    dwReplayVideoListener.onBufferEnd();
                }
                if (localReplayRoomListener != null) {
                    localReplayRoomListener.onPlay();
                }
            }else if (state == CCBasePlayer.CCPlayerStatus.COMPLETED){
                if (localReplayRoomListener != null) {
                    localReplayRoomListener.onPlayComplete();
                }
                if (proRecordWorker != null) {
                    proRecordWorker.clear();
                }
            }else if (state == CCBasePlayer.CCPlayerStatus.SEEKED){
                if (localReplayRoomListener != null) {
                    localReplayRoomListener.onSeekComplete();
                }
            }else if (state == CCBasePlayer.CCPlayerStatus.PLAYING){
                if (dwReplayVideoListener !=null){
                    dwReplayVideoListener.onPlaying();
                }
                if (localReplayRoomListener != null) {
                    localReplayRoomListener.onPlay();
                }
            }
        }
    };

    //--------------------------------------记忆播放相关--------------------------------------------

    // 开启进度记忆
    public void startProRecord(Context context, ProRecordWorker.UICallback callback) {
        if (proRecordWorker != null) {
            return;
        }
        proRecordWorker = new ProRecordWorker(context, callback);
        proRecordWorker.setRecordTag(recordTag);
        proRecordWorker.setLocal(true);
        proRecordWorker.setTextView(proRecordJump);
        proRecordWorker.start();

    }

    public void stopProRecord() {
        if (proRecordWorker != null) {
            proRecordWorker.stop();
            proRecordWorker = null;
        }

    }

    public void clearProRecord(){
        if (proRecordWorker != null) {
            proRecordWorker.clear();
        }
    }

    //******************************* 实现 DWLiveLocalReplayListener 定义的方法 ***************************************/
    private final DWLiveLocalReplayListener dwLiveLocalReplayListener = new DWLiveLocalReplayListener() {

        @Override
        public void onQuestionAnswer(TreeSet<ReplayQAMsg> qaMsgs) {
            if (localDWReplayQAListener != null) {
                localDWReplayQAListener.onQuestionAnswer(qaMsgs);
            }
        }

        @Override
        public void onChatMessage(TreeSet<ReplayChatMsg> replayChatMsgs) {
            if (localDWReplayChatListener != null) {
                localDWReplayChatListener.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) {
            ELog.e(TAG, "onException:" + exception.getMessage());

        }

        @Override
        public void onInfo(TemplateInfo templateInfo, RoomInfo roomInfo) {
            mTemplateInfo = templateInfo;
            if (localTemplateUpdateListener != null) {
                localTemplateUpdateListener.onLocalTemplateUpdate();
            }
            if (localDWReplayIntroListener != null) {
                localDWReplayIntroListener.updateRoomInfo(roomInfo);
            }
            if (localReplayRoomListener != null&&roomInfo.getRecordInfo()!=null) {
                localReplayRoomListener.updateRoomTitle(roomInfo.getRecordInfo().getTitle());
            }
        }


        @Override
        public void onInitFinished() {

        }

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


    public boolean isPlaying() {
        return DWLiveLocalReplay.getInstance().isPlaying();
    }

    public long getCurrentPosition() {
        return DWLiveLocalReplay.getInstance().getCurrentPosition();
    }

    public void setSpeed(float v) {
        DWLiveLocalReplay.getInstance().setSpeed(v);
    }

    public float getSpeed(float v) {
        return DWLiveLocalReplay.getInstance().getSpeed();
    }

    public boolean isInPlaybackState() {
        return  DWLiveLocalReplay.getInstance().isInPlaybackState();
    }

    public long getDuration() {
        return DWLiveLocalReplay.getInstance().getDuration();
    }

    public void setDocScaleType(DocView.ScaleType centerInside) {
        DWLiveLocalReplay.getInstance().setDocScaleType(centerInside);
    }


    public interface LocalTemplateUpdateListener {
        /**
         * 模板信息获取监听器，当本地回放模板信息获取后
         * 通过该监听器回调给上层。
         */
        void onLocalTemplateUpdate();
    }

    /**
     * 获取文档显示模式
     *
     * @return 文档窗口状态：1——适合窗口，2——适合宽度
     */
    public int getDocumentDisplayMode() {
        DWLiveLocalReplay dwLiveReplay = DWLiveLocalReplay.getInstance();
        if (dwLiveReplay != null && dwLiveReplay.getRoomInfo() != null) {
            return dwLiveReplay.getRoomInfo().getDocumentDisplayMode();
        }
        return 1;
    }


}
