package com.ym.zxhcsdk.view;

import android.app.Activity;
import android.content.Intent;
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.ads.RewardVideoMediaPlayer;
import com.ym.zxhcsdk.listener.RewardVideoAdLoadListener;
import com.ym.zxhcsdk.listener.RewardVideoAdShowListener;

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 boolean isLoadFinish;
    private HcAdBean adBean;

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

    /**
     * 缓存激励视频
     * @param mlistener
     */
    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;
                    RewardVideoMediaPlayer.getInstance().setDataSource("https://zx-cdn.zhangxinhulian.com/file/2020/5/8447fa5488ee19a54d2adbe8d127693045540211.mp4");
                    //  mPlayer.setDisplay(surfaceHolder);
                    RewardVideoMediaPlayer.getInstance().prepareAsync();
                    //网络流媒体的缓冲监听
                    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();
                }
            }

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

    /**
     * 播放激励视频
     */
    public void showRewardAD(Activity context, RewardVideoAdShowListener listener) {
        
    }
}
