package com.ym.zxhcsdk.view;

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.PhoneUtils;
import com.ym.zxhcsdk.ZXHCConstant;
import com.ym.zxhcsdk.listener.RewardVideoAdLoadListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ZXHCRewardVideoAD implements Serializable {

    private Activity context;
    private String codeId;
    public MediaPlayer mMediaPlayer;
    public boolean isLoadFinish;

    public ZXHCRewardVideoAD(Activity context, String codeId) {
        this.context = context;
        this.codeId = codeId;
    }

    private HcAdBean adBean;
    public void loadRewardAD(RewardVideoAdLoadListener mlistener) {
        Map<String, String> map = new HashMap<>();
        map.put("pid", codeId);
        map.put("type", "api");
        map.put("ad_type", "3");
        map.put("ad_w",PhoneUtils.getInstance().getScreenWidth(context)+"");
        map.put("ad_h",PhoneUtils.getInstance().getScreenHeight(context)+"");
        HttpClientUtils.get(ZXHCConstant.BASE_URL, map, new HttpClientUtils.OnRequestCallBack() {
            @Override
            public void onSuccess(String json) {
                adBean = PhoneUtils.getInstance().jsonToObj(json);
                adBean.setShowReport(false);
                adBean.setClickReport(false);
                adBean.setClickDpLinkSucReport(false);
                adBean.setClickDpLinkfailReport(false);
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mlistener!=null){
                            mlistener.OnAdLoaded();
                        }
                    }
                });

                if (adBean.getSrcUrls().size() > 0) {

                }

                try {
                    isLoadFinish = false;
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource("https://zx-cdn.zhangxinhulian.com/file/2020/5/8447fa5488ee19a54d2adbe8d127693045540211.mp4");
                    //  mPlayer.setDisplay(surfaceHolder);
                    mMediaPlayer.prepareAsync();
                    //网络流媒体的缓冲监听
                    mMediaPlayer.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();
                }
            }

            @Override
            public void onError(String errorMsg) {
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mlistener!=null){
                            mlistener.OnLoadError(errorMsg);
                        }
                        //listener.OnError(errorMsg);
                    }
                });
            }
        });
    }
}
