package com.ym.zxhcsdk.view;

import android.app.Activity;
import android.content.Intent;

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.ads.RewardVideoPortraitADActivity;
import com.ym.zxhcsdk.listener.RewardVideoAdLoadListener;
import com.ym.zxhcsdk.listener.RewardVideoAdShowListener;

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.setVideoStartReport(false);
                adBean.setVideoCompleteReport(false);
                adBean.setVideoPauseReport(false);
                adBean.setVideoResumeReport(false);
                adBean.setVideoPlay34Report(false);
                adBean.setVideoPlay12Report(false);
                adBean.setVideoPlay14Report(false);
                adBean.setVideoCloseReport(false);
                adBean.setVideoSkipReport(false);


                adBean.setClickDpLinkSucReport(false);
                adBean.setClickDpLinkfailReport(false);
                if (null == adBean.getVideo_url() || adBean.getVideo_url().equals("")) {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mlistener != null) {
                                mlistener.OnLoadError("没有视频资源");
                            }
                        }
                    });
                } else {
                    RewardVideoMediaPlayer.getInstance().setHcAdBean(adBean);//将广告资源加载到MediaPlayer
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mlistener != null) {
                                mlistener.OnAdLoaded();
                            }
                        }
                    });
                }
            }

            @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 activity, RewardVideoAdShowListener listener) {
        RewardVideoMediaPlayer.getInstance().setShowRewardVideoListener(activity, listener);
        activity.startActivity(new Intent(activity, RewardVideoPortraitADActivity.class));
    }
}
