package com.mints.goodmoney.ad.video;

import android.app.Activity;
import android.os.SystemClock;

import com.mints.goodmoney.common.Constant;
import com.mints.goodmoney.manager.TrackManager;
import com.mints.goodmoney.manager.YlhAdManager;
import com.mints.goodmoney.mvp.presenters.YlhVedioAdPresenter;
import com.mints.goodmoney.mvp.views.VedioAdManagerView;
import com.mints.goodmoney.utils.LogUtil;
import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;

import java.util.HashMap;

/**
 * 优量汇视频
 */
public class YlhVedioAdManager extends BaseVideoAd implements VedioAdManagerView {

    private static YlhVedioAdManager _inst;

    // 优量汇激励视频
    private RewardVideoAD rewardVideoAD;
    private int curCoin;
    private String extraId;

    private YlhVedioAdPresenter ylhVedioAdPresenter;
    private YlhVedioAdListener ylhVedioAdListener;

    Activity activity;

    private boolean isClickScreen = true; // 是否点击屏幕跳转广告

    public static YlhVedioAdManager getInstance(Activity activity) {
        if (_inst != null) {
            return _inst;
        } else {
            _inst = new YlhVedioAdManager(activity);
            return _inst;
        }
    }

    private YlhVedioAdManager(Activity activity) {
        super(activity);
        init(activity);
        return;
    }

    private void init(Activity activity) {
        this.activity = activity;

        ylhVedioAdPresenter = new YlhVedioAdPresenter();
        ylhVedioAdPresenter.attachView(this);
    }

    @Override
    public void onDestory() {
        if (ylhVedioAdPresenter != null) {
            ylhVedioAdPresenter.dispose();
            ylhVedioAdPresenter.detachView();
        }

        activity = null;
    }

    /**
     * 加载优量汇激励视频
     */
    @Override
    public void loadAd(Activity activity, int curCoin, String carrierType, String extraId) {
        this.activity = activity;
        isClickScreen = true;
        this.curCoin = curCoin;
        this.extraId = extraId;

        String ylhPostId;
        switch (carrierType) {
            case Constant.CARRIER_VERSUS_VIDEO:// 每日任务视频
                ylhPostId = YlhAdManager.YLH_AD_VEDIO_MAIN_POSTID;
                break;
            case Constant.CARRIER_OFFLINE_DOUBLE:// 离线收益翻倍
            case Constant.CARRIER_SIGNIN_HOMEPAGE_CARD:// 签到翻倍
            case Constant.CARRIER_WALK_BUBBLE:// 汽泡
                ylhPostId = YlhAdManager.YLH_AD_VEDIO_MOREDIALOG_POSTID;
                break;
            case Constant.CARRIER_HOMEWATER:// 喝水
                ylhPostId = YlhAdManager.YLH_AD_VEDIO_DRINK_POSTID;
                break;
            case Constant.CARRIER_CHALLENGE_TURN:// 大转盘
                ylhPostId = YlhAdManager.YLH_AD_VEDIO_TURNABLE_POSTID;
                break;
            case Constant.CARRIER_CHALLENGE_CARD:// 刮刮乐
                ylhPostId = YlhAdManager.YLH_AD_VEDIO_ERASE_POSTID;
                break;
            default:// 在线收益翻倍
                ylhPostId = YlhAdManager.YLH_AD_VEDIO_MOREDIALOG_POSTID;
                break;
        }

        if (rewardVideoAD != null) {
            rewardVideoAD = null;
        }

        rewardVideoAD = new RewardVideoAD(activity, YlhAdManager.YLH_AD_APPID, ylhPostId, new RewardVideoADListener() {
            @Override
            public void onADLoad() {//广告加载成功，可在此回调后进行广告展示，此时广告过期时间确定，可通过RewardVideoAD.getExpireTimestamp()获取
                try {
                    if (!rewardVideoAD.hasShown()) {//广告展示检查2：当前广告数据还没有展示过
                        long delta = 1000;//建议给广告过期时间加个buffer，单位ms，这里demo采用1000ms的buffer
                        //广告展示检查3：展示广告前判断广告数据未过期
                        if (SystemClock.elapsedRealtime() < (rewardVideoAD.getExpireTimestamp() - delta)) {
                            rewardVideoAD.showAD();
                        } else {
                            showToast("广告已过期，请再次请求广告后进行广告展示！");
                        }
                    } else {
                        showToast("此条广告已经展示过，请再次请求广告后进行广告展示！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_YLH, Constant.EVENT_TYPE_ONE, "999999", "Load Fail");

                    if (ylhVedioAdListener != null) {
                        ylhVedioAdListener.ylhVedioAdFail();
                    }
                }
            }

            @Override
            public void onVideoCached() {//视频素材缓存成功，可在此回调后进行广告展示

            }

            @Override
            public void onADShow() {//激励视频广告页面展示，此后RewardVideoAD.hasShown()返回true
                TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_YLH, Constant.EVENT_TYPE_ZERO, "", "");

            }

            @Override
            public void onADExpose() {//激励视频广告曝光

            }

            @Override
            public void onReward() {//激励视频广告激励发放
                if (activity != null) {
                    ylhVedioAdPresenter.reportAddCoinMsg(activity, getAdMapVO(carrierType));
                }
            }

            @Override
            public void onADClick() {//激励视频广告被点击
                TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_YLH, Constant.EVENT_TYPE_TWO, "", "");

                if (isClickScreen) {
                    // 防止重复
                    TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_YLH, Constant.EVENT_TYPE_FOUR, "", "");
                    isClickScreen = false;
                }
                if (ylhVedioAdListener != null) {
                    ylhVedioAdListener.ylhVedioAdDownload();
                }
            }

            @Override
            public void onVideoComplete() {//广告视频素材播放完毕

            }

            @Override
            public void onADClose() {//激励视频广告被关闭
                TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_YLH, Constant.EVENT_TYPE_THREE, "", "");

                if (ylhVedioAdListener != null) {
                    ylhVedioAdListener.ylhVedioAdSuccess();
                }
            }

            @Override
            public void onError(AdError adError) {//广告流程出错，AdError中包含错误码和错误描述
                if (adError != null) {

                    LogUtil.d("YlhVedioAdManager", "onError  " + adError.getErrorCode() + adError.getErrorMsg());

                    // 0 成功 1失败 2点击 3-有效展示
                    TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_YLH, Constant.EVENT_TYPE_ONE, String.valueOf(adError.getErrorCode()), adError.getErrorMsg());
                }
                if (ylhVedioAdListener != null) {
                    ylhVedioAdListener.ylhVedioAdFail();
                }
            }
        });

        rewardVideoAD.loadAD();
    }

    /**
     * 第四步 封装广告数据，请求服务器
     *
     * @param carrierType
     * @return
     */
    private HashMap<String, Object> getAdMapVO(String carrierType) {
        HashMap<String, Object> vo = new HashMap<>();
        vo.put("carrierType", carrierType);
        vo.put("adtype", Constant.ADTYPE_VEDIO);
        vo.put("adsource", Constant.AD_SOURCE_YLH);

        switch (carrierType) {
            case Constant.CARRIER_VERSUS_VIDEO:// 每日任务-视频
                vo.put("coin", curCoin);
                break;
            case Constant.CARRIER_HOMEWATER:// 首页喝水汽泡
                vo.put("waterCoin", curCoin);
                break;
            case Constant.CARRIER_OFFLINE_DOUBLE:// 离线收益翻倍
                vo.put("offline_income", curCoin);
            case Constant.CARRIER_CHALLENGE_TURN:// 挑战大转盘
            case Constant.CARRIER_CHALLENGE_CARD:// 挑战刮刮乐
                vo.put("turntableKey", extraId);
                break;
        }
        return vo;
    }

    public void setYlhVedioAdListener(YlhVedioAdListener ylhVedioAdListener) {
        this.ylhVedioAdListener = ylhVedioAdListener;
    }

    public interface YlhVedioAdListener {
        void ylhVedioAdFail();

        void ylhVedioAdSuccess();

        void ylhVedioAdDownload();
    }
}
