package com.ym.admodule.sh;

import android.content.Context;
import android.util.Log;

import androidx.annotation.Nullable;

import com.tencent.ep.shanhuad.BuildConfig;
import com.tz.sdk.coral.ad.CoralAD;
import com.tz.sdk.coral.callback.CoralVideoListener;
import com.tz.sdk.core.ad.ADError;
import com.tz.sdk.core.ad.ADEvent;
import com.tz.sdk.core.ad.ADSource;
import com.tz.sdk.core.ad.ADType;
import com.tz.sdk.core.loader.ADLoader;
import com.ym.admodule.config.AdInfo;
import com.ym.admodule.config.ZXADCallback;
import com.ym.admodule.listener.ZXADVideoListener;
import com.ym.modulecommon.module.NewsEntity;
import com.ym.modulecommon.utils.AdEventUtils;
import com.ym.modulecommon.utils.LogUtils;

import java.util.HashMap;
import java.util.List;

/**
 * 视频
 * Created by wanghl on 2020/6/18
 */
public final class CoralRewardVideo extends CoralBase {
    private String TAG = "AdManager";
    private NewsEntity data;
    private ZXADVideoListener listener;
    private ZXADCallback callback;

    public CoralRewardVideo(Context context, int taskType) {
        super(context, taskType);
    }

    public CoralRewardVideo build(NewsEntity data,ZXADVideoListener listener,
                          ZXADCallback callback) {
        this.data = data;
        this.listener = listener;
        this.callback = callback;
        return this;
    }

    @Override
    public void pull() {
        super.pull();
        new ADLoader(mContext)
                .get(ADType.REWARD_VIDEO)
                .from(ADSource.CORAL)
                .count(1)
                .reward(true)
                .with(new HashMap<String, Object>() {
                    {
                        put(CoralAD.Key.TASK_TYPE, mTaskType);
                        put(CoralAD.Key.ACCOUNT_ID, mAccountId);
                        put(CoralAD.Key.LOGIN_KEY, mLoginKey);
                    }
                })
                .load(new CoralVideoListener() {
                    @Override
                    public void onAdLoaded(List<CoralAD> adList) {
                        if (adList != null && adList.size() > 0) {
                            whenAdLoaded(adList.get(0));
                            callback.onAdSuccess();
                            listener.onSuccess();
                           LogUtils.d(TAG,"珊瑚 激励 onAdLoaded");
                        } else {
                            callback.onAdFail();
                           LogUtils.d(TAG,"珊瑚 激励视频类(" + mTaskType + ")广告返回数量0");
                            whenAdError("珊瑚 激励视频类(" + mTaskType + ")广告返回数量0");
                        }
                    }

                    @Override
                    public void onAdFailed(ADError adError) {
                        super.onAdFailed(adError);
                        if (BuildConfig.DEBUG) {
                           LogUtils.d("CoralRewardVideo", adError.toString());
                        }
                        whenAdError(adError.toString());
                        callback.onAdFail();
                        listener.onError(adError.getDescription()+"");
                       LogUtils.d(TAG,"珊瑚 激励 onAdFailed" + adError.toString());
                    }

                    @Override
                    public boolean onAdShow(@Nullable CoralAD ad) {
                        whenAdShow();
                        listener.onAdShow();
                        AdEventUtils.INSTANCE.onEventShow(data);
                        AdEventUtils.INSTANCE.onEventUM("ad_reward_video_show","珊瑚 激励视频广告");
                       LogUtils.d(TAG,"珊瑚 激励 onAdShow");
                        return super.onAdShow(ad);
                    }

                    @Override
                    public boolean onAdClicked(@Nullable CoralAD ad) {
                        whenAdClicked();
                        listener.onAdVideoBarClick();
                        AdEventUtils.INSTANCE.onEventClick(data, AdEventUtils.INSTANCE.getAD_MODEL_REWARD());
                        AdEventUtils.INSTANCE.onEventUM("ad_reward_video_click","珊瑚 激励视频广告");
                       LogUtils.d(TAG,"珊瑚 激励 onAdClicked");
                        return super.onAdClicked(ad);
                    }

                    @Override
                    public boolean onAppDownloading(@Nullable CoralAD ad, @Nullable String downloadUrl) {
                        whenAppDownloadEvent(ADEvent.Download_Start, "激励视频");
                        return super.onAppDownloading(ad, downloadUrl);
                    }

                    @Override
                    public boolean onAppDownloaded(@Nullable CoralAD ad, @Nullable String downloadUrl, @Nullable String localFile) {
                        whenAppDownloadEvent(ADEvent.Download_Success, "激励视频");
                        return super.onAppDownloaded(ad, downloadUrl, localFile);
                    }

                    @Override
                    public boolean onAppInstalled(@Nullable CoralAD ad, @Nullable String downloadUrl, @Nullable String localFile) {
                        whenAppDownloadEvent(ADEvent.Install_Success, "激励视频");
                        return super.onAppInstalled(ad, downloadUrl, localFile);
                    }

                    @Override
                    public boolean onAppActivated(CoralAD ad, String downloadUrl, String localFile) {
                        whenAppDownloadEvent(ADEvent.Activated, "激励视频");
                        return super.onAppActivated(ad, downloadUrl, localFile);
                    }

                    @Override
                    public boolean onVideoFinished(CoralAD coralAD, String s) {
                        return super.onVideoFinished(coralAD, s);
                    }

                    @Override
                    public boolean onVideoClosed(CoralAD coralAD, String s) {
                        listener.onAdClose(new AdInfo(""));
                       LogUtils.d(TAG,"珊瑚 激励 onVideoClosed");
                        return super.onVideoClosed(coralAD, s);
                    }
                });
    }

    @Override
    protected void showAd(CoralAD ad) {
//        ad.playVideo();
    }
}
