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.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 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;
    }
}
