package com.ym.zxhcsdk.ads;

import android.app.Activity;
import android.media.MediaPlayer;
import android.util.Log;

import com.ym.zxhcsdk.HcAdBean;
import com.ym.zxhcsdk.HttpClientUtils;
import com.ym.zxhcsdk.listener.RewardVideoAdShowListener;
import com.ym.zxhcsdk.listener.RewardVideoAdUpdateViewListener;

import java.io.IOException;

/**
 * 激励视频播放器
 */
public class RewardVideoMediaPlayer {

    private static volatile RewardVideoMediaPlayer mRewardVideoMediaPlayer = null;
    private static MediaPlayer mMediaPlayer = null;
    public boolean isPrepared = false;//是否装载媒体资源完毕
    private RewardVideoAdShowListener mShowListener;//播放激励视频监听回调
    private RewardVideoAdUpdateViewListener updateViewListener;
    private Activity mActivity;
    private boolean isShowVideo;//是否加载完成视频立即播放视频
    private HcAdBean mHcAdBean;//广告资源

    private RewardVideoMediaPlayer() {
    }

    /**
     * 单例
     *
     * @return
     */
    public static RewardVideoMediaPlayer getInstance() {
        synchronized (RewardVideoMediaPlayer.class) {
            if (null == mRewardVideoMediaPlayer) {
                mRewardVideoMediaPlayer = new RewardVideoMediaPlayer();
            }
        }
        return mRewardVideoMediaPlayer;
    }

    /**
     * MediaPlayer实例化
     *
     * @return
     */
    public MediaPlayer getMediaPlayer() {
        if (null == mMediaPlayer) {
            mMediaPlayer = new MediaPlayer();
        }
        return mMediaPlayer;
    }

    public Activity getActivity() {
        return mActivity;
    }

    public void setHcAdBean(HcAdBean mHcAdBean) {
        try {
            this.mHcAdBean = mHcAdBean;
            isPrepared = false;
            getMediaPlayer().reset();
            getMediaPlayer().setDataSource(mHcAdBean.getVideo_url());
            getMediaPlayer().prepareAsync();
            getMediaPlayer().setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    isPrepared = true;
                    if (isShowVideo) {
                        startVideo();
                    }
                }
            });
            getMediaPlayer().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {//网络流媒体播放结束监听
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d("wxw", "MediaPlayer onCompletion");
                    if (null != updateViewListener) {
                        mShowListener.OnVideoCompleted();
                        updateViewListener.OnCompletionListener();
                    }
                }
            });
            getMediaPlayer().setOnErrorListener(new MediaPlayer.OnErrorListener() {//设置错误信息监听
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.d("wxw", "MediaPlayer error " + what + " extra " + extra);
                    if (null != updateViewListener) {
                        mShowListener.OnVideoError("" + what);
                        updateViewListener.OnErrorListener(what + "");
                    }
                    return false;
                }
            });
/*            getMediaPlayer().setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
                @Override
                public void onTimedText(MediaPlayer mp, TimedText text) {
                    Log.d("wxw", "text " + text );
                }
            });*/
/*            RewardVideoMediaPlayer.getInstance().setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {//网络流媒体的缓冲监听
                @Override
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    Log.d("wxw", "percent" + percent);
                    if (percent==100){
//                                mp.start();
                        isLoadFinish = true;
                    }
                    Log.d("wxw", "mp.getCurrentPosition() " + mp.getCurrentPosition());
                }
            });*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 开始播放激励视频
     */
    public void startVideo() {
        if (isPrepared) {//资源加载完成
            isShowVideo = false;
            if (null != mShowListener) {
                mShowListener.OnAdShow();
            }
            if (null != updateViewListener) {
                updateViewListener.OnShowListener();

            }
            getMediaPlayer().start();
        } else {//资源加载未完成
            isShowVideo = true;
        }
    }

    /**
     * 关闭激励视频页面
     */
    public void closeVideoAdPage() {
        mShowListener.OnAdClosed();
    }

    /**
     * 可以领取奖励
     */
    public void rewardVerify() {
        mShowListener.OnRewardVerify();
    }

    /**
     * 点击激励视频下载
     */
    public void clickVideoAd() {
        mShowListener.OnAdClicked();
    }

    /**
     * @return 广告资源
     */
    public HcAdBean getHcAdBean() {
        return mHcAdBean;
    }

    /**
     * 设置播放激励视频监听回调
     */
    public void setShowRewardVideoListener(Activity activity, RewardVideoAdShowListener showListener) {
        this.mActivity = activity;
        this.mShowListener = showListener;
    }

    /**
     * 更新Activity View监听
     */
    public void setRewardVideoUpdateListener(RewardVideoAdUpdateViewListener updateListener) {
        this.updateViewListener = updateListener;
    }

    /**
     * 开始播放上报
     */
    public void StartPlayReport() {
        if (mHcAdBean.getVideo_start() == null || mHcAdBean.isVideoStartReport()) {
            return;
        }
        mHcAdBean.setVideoStartReport(true);
        //开始播放上报
        if (mHcAdBean.getVideo_start().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_start().size(); i++) {
                String startReport = mHcAdBean.getVideo_start().get(i);
                HttpClientUtils.GetEvent(startReport);
            }
        }
    }

    /**
     * 播放完成上报
     */
    public void CompleteReport() {
        if (mHcAdBean.getVideo_complete() == null || mHcAdBean.isVideoCompleteReport()) {
            return;
        }
        mHcAdBean.setVideoCompleteReport(true);
        //播放完成上报
        if (mHcAdBean.getVideo_complete().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_complete().size(); i++) {
                String completeReport = mHcAdBean.getVideo_complete().get(i);
                HttpClientUtils.GetEvent(completeReport);
            }
        }
    }

    /**
     * 恢复播放上报
     */
    public void VideoOnResumeReport() {
        if (mHcAdBean.getVideo_resume() == null || mHcAdBean.isVideoResumeReport()) {
            return;
        }
        mHcAdBean.setVideoResumeReport(true);
        //恢复播放上报
        if (mHcAdBean.getVideo_resume().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_resume().size(); i++) {
                String resumeReport = mHcAdBean.getVideo_resume().get(i);
                HttpClientUtils.GetEvent(resumeReport);
            }
        }
    }

    /**
     * 播放暂停上报
     */
    public void VideoOnPauseReport() {
        if (mHcAdBean.getVideo_pause() == null || mHcAdBean.isVideoPauseReport()) {
            return;
        }
        mHcAdBean.setVideoPauseReport(true);
        //播放暂停上报
        if (mHcAdBean.getVideo_pause().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_pause().size(); i++) {
                String pauseReport = mHcAdBean.getVideo_pause().get(i);
                HttpClientUtils.GetEvent(pauseReport);
            }
        }
    }

    /**
     * 播放跳过上报
     */
    public void VideoSkipReport() {
        if (mHcAdBean.getVideo_skip() == null || mHcAdBean.isVideoSkipReport()) {
            return;
        }
        mHcAdBean.setVideoSkipReport(true);
        if (mHcAdBean.getVideo_skip().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_skip().size(); i++) {
                String skipReport = mHcAdBean.getVideo_skip().get(i);
                HttpClientUtils.GetEvent(skipReport);
            }
        }
    }

    /**
     * 播放关闭上报
     */
    public void VideoCloseReport() {
        if (mHcAdBean.getVideo_close() == null || mHcAdBean.isVideoCloseReport()) {
            return;
        }
        mHcAdBean.setVideoCloseReport(true);
        if (mHcAdBean.getVideo_close().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_close().size(); i++) {
                String closeReport = mHcAdBean.getVideo_close().get(i);
                HttpClientUtils.GetEvent(closeReport);
            }
        }
    }

    /**
     * 播放1/4上报
     */
    public void VideoShowOneQuarterReport() {
        if (mHcAdBean.getVideo_one_quarter() == null || mHcAdBean.isVideoPlay14Report()) {
            return;
        }
        mHcAdBean.setVideoPlay14Report(true);
        if (mHcAdBean.getVideo_one_quarter().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_one_quarter().size(); i++) {
                String oneQuarterReport = mHcAdBean.getVideo_one_quarter().get(i);
                HttpClientUtils.GetEvent(oneQuarterReport);
            }
        }
    }

    /**
     * 播放1/2上报
     */
    public void VideoShowOneHalfReport() {
        if (mHcAdBean.getVideo_one_half() == null || mHcAdBean.isVideoPlay12Report()) {
            return;
        }
        mHcAdBean.setVideoPlay12Report(true);
        if (mHcAdBean.getVideo_one_half().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_one_half().size(); i++) {
                String oneHalfReport = mHcAdBean.getVideo_one_half().get(i);
                HttpClientUtils.GetEvent(oneHalfReport);
            }
        }
    }

    /**
     * 播放3/4上报
     */
    public void VideoShowThreeQuarterReport() {
        if (mHcAdBean.getVideo_three_quarter() == null || mHcAdBean.isVideoPlay34Report()) {
            return;
        }
        mHcAdBean.setVideoPlay34Report(true);
        if (mHcAdBean.getVideo_three_quarter().size() > 0) {
            for (int i = 0; i < mHcAdBean.getVideo_three_quarter().size(); i++) {
                String threeQuarterReport = mHcAdBean.getVideo_three_quarter().get(i);
                HttpClientUtils.GetEvent(threeQuarterReport);
            }
        }
    }


    /**
     * 点击上报
     */
    public void VideoClickReport() {
        if (mHcAdBean.getClickUrl() == null || mHcAdBean.isClickReport()) {
            return;
        }
        mHcAdBean.setClickReport(true);
        //点击上报
        if (mHcAdBean.getClickUrl().size() > 0) {
            for (int i = 0; i < mHcAdBean.getClickUrl().size(); i++) {
                String clickReport = mHcAdBean.getClickUrl().get(i);

                HttpClientUtils.GetEvent(clickReport);
            }
        }
    }

    /**
     * 曝光上报
     */
    public void VideoShowReport() {
        if (mHcAdBean.getMonitorUrl() == null || mHcAdBean.isShowReport()) {
            return;
        }
        mHcAdBean.setShowReport(true);
        //曝光上报
        if (mHcAdBean.getMonitorUrl().size() > 0) {
            for (int i = 0; i < mHcAdBean.getMonitorUrl().size(); i++) {
                String showReport = mHcAdBean.getMonitorUrl().get(i);
                HttpClientUtils.GetEvent(showReport);
            }
        }
    }

}
