package com.duben.microtribe.ad.video;

import android.app.Activity;
import android.os.Bundle;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.duben.microtribe.BuildConfig;
import com.duben.microtribe.ad.AdManager;
import com.duben.microtribe.ad.AdStatusListener;
import com.duben.microtribe.common.Constant;
import com.duben.microtribe.utils.AppPreferencesManager;
import com.duben.microtribe.utils.LogUtil;

import java.lang.ref.WeakReference;
import java.util.HashMap;

/**
 * **实时加载
 * <p>
 * 应用内-GroMore激励视频广告
 */
public class InMoneyVideoNoPre {

    private static final String TAG = InMoneyVideoNoPre.class.getSimpleName();

    private static InMoneyVideoNoPre _inst;

    public static InMoneyVideoNoPre getInstance() {
        if (_inst == null) {
            _inst = new InMoneyVideoNoPre();
        }
        return _inst;
    }

    private InMoneyVideoNoPre() {
    }

    private String carrierType = "";

    // 当前播放的广告
    private String nowAdcode = "";
    private String nowEcpm = "";
    private String nowAdSource = "";

    private TTRewardVideoAd mttRewardAd;

    private WeakReference<Activity> weakActivity;

    private AdStatusListener adStatusListener;

    /**
     * 激励视频
     */
    public void loadAd(Activity _activity, AdStatusListener adStatusListener, String carrier) {
        this.carrierType = carrier;
        this.adStatusListener = adStatusListener;
        this.weakActivity = new WeakReference(_activity);

        preLoadAd(_activity);
    }

    private void preLoadAd(Activity activity) {

        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);

        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(BuildConfig.GROMORE_VIDEO_CODE)
                .setUserID("")
                .setOrientation(TTAdConstant.VERTICAL)
                .setMediationAdSlot(
                        new MediationAdSlot.Builder()
                                .setRewardAmount(2000)
                                .setRewardName("")
                                .build()
                ).build();
        adNativeLoader.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                LogUtil.e(TAG, "gromore实时加载 激励视频广告-->onError  " + i + s);

                if (adStatusListener != null) {
                    adStatusListener.adFail();
                }
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {

            }

            @Override
            public void onRewardVideoCached() {

            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                LogUtil.d(TAG, "gromore实时加载 激励视频广告--> 4、onRewardVideoCached");

                mttRewardAd = ttRewardVideoAd;

                if (mttRewardAd != null && weakActivity != null && mttRewardAd.getMediationManager().isReady()) {
                    mttRewardAd.setRewardAdInteractionListener(mTTRewardedAdListener);
                    mttRewardAd.showRewardVideoAd(weakActivity.get());
                } else {
                    LogUtil.e(TAG, "gromore实时加载 onRewardVideoCached  onError  ");
                    if (adStatusListener != null) {
                        adStatusListener.adFail();
                    }
                }
            }
        });


    }

    private final TTRewardVideoAd.RewardAdInteractionListener mTTRewardedAdListener = new TTRewardVideoAd.RewardAdInteractionListener() {
        @Override
        public void onAdShow() {
            if (adStatusListener != null) {
                adStatusListener.adSuccess();
            }

            if (mttRewardAd != null) {
                if (mttRewardAd.getMediationManager().getShowEcpm() != null) {
                    nowAdcode = mttRewardAd.getMediationManager().getShowEcpm().getSlotId();
                    nowEcpm = mttRewardAd.getMediationManager().getShowEcpm().getEcpm();
                    nowAdSource = mttRewardAd.getMediationManager().getShowEcpm().getSdkName();
                }
            }
            LogUtil.d(TAG, "gromore实时加载 激励视频广告-->onAdShow");

            // 预加载
            if (weakActivity.get() != null) {
                LogUtil.d(TAG, "gromore实时加载 激励视频广告->onAdShow  触发预加载下次广告");
                AdManager.Companion.getInstance().preLoadAd(weakActivity.get(), true);
            }
        }

        @Override
        public void onAdVideoBarClick() {
        }

        @Override
        public void onAdClose() {
            LogUtil.d(TAG, "gromore实时加载 激励视频广告-->onAdClose   nowAdcode=" + nowAdcode);

            int adCount = AppPreferencesManager.INSTANCE.get().getInt(Constant.VEDIO_AD_COUNT, 0);
            if (adCount < 50) {
                AppPreferencesManager.INSTANCE.get().put(Constant.VEDIO_AD_COUNT, adCount+1);
            }

            HashMap<String, Object> vo = new HashMap<>();
            vo.put("adcode", nowAdcode);
            vo.put("ecpm", nowEcpm);
            vo.put("adSource", nowAdSource);
            vo.put("carrierType", carrierType);
            vo.put("adid", BuildConfig.GROMORE_VIDEO_CODE);
            if (adStatusListener != null) {
                adStatusListener.adClose(vo);
            }

            adStatusListener = null;
        }

        @Override
        public void onVideoComplete() {
            LogUtil.d(TAG, "gromore实时加载 激励视频广告-->onVideoComplete");
        }

        @Override
        public void onVideoError() {
            if (adStatusListener != null) {
                adStatusListener.adFail();
            }
            LogUtil.e(TAG, "gromore实时加载 激励视频广告-->onVideoError");
        }

        @Override
        public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {

        }

        @Override
        public void onRewardArrived(boolean b, int i, Bundle bundle) {

        }

        @Override
        public void onSkippedVideo() {

        }
    };


}
