package com.zxhl.cms.utils

import android.app.Activity
import android.content.Context
import android.os.Bundle
import com.bytedance.sdk.openadsdk.*
import com.zxhl.cms.ad.upload.Utils.AdUpload
import com.zxhl.cms.ad.upload.model.NewsEntity
import com.zxhl.cms.net.ApiClient.adApi
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.uc.PayVerifyEntity


/**
 * Created by gaoleichao on 2018/11/22.
 */
object AdDataSupport {

    private var mTTAdNative: TTAdNative? = null
    private var context: Context? = null
    fun init(context: Context) {
        this.context = context
        mTTAdNative = TTAdSdk.getAdManager().createAdNative(context)//baseContext建议为activity
    }

    /**
     * 动动-----穿山甲大图
     * val codeId = Constant.SdkKey.daily_active_bigimage
     */
    fun requestByteDailyActiveBigImgFeed(
        codeId: String,
        expressViewWidth: Float,
        expressViewHeight: Float,
        callBack: TTAdNative.NativeExpressAdListener
    ) {
//        val data = getBigAdData(codeId)
//        if (data != null) {
//            val list: MutableList<TTNativeExpressAd> = ArrayList()
//            list.add(data)
//            callBack?.onNativeExpressAdLoad(list)
//            return
//        }

        val adSlot = AdSlot.Builder()
            .setCodeId(codeId)
            .setSupportDeepLink(false)
            .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
            .setImageAcceptedSize(640, 320)
            .setAdCount(2)
            .build()
        //调用feed广告异步请求接口
//        mTTAdNative?.loadFeedAd(adSlot,callBack)
        mTTAdNative?.loadNativeExpressAd(adSlot, object : TTAdNative.NativeExpressAdListener {
            override fun onNativeExpressAdLoad(p0: MutableList<TTNativeExpressAd>?) {
                if (p0 != null && p0.size > 0) {
                    callBack.onNativeExpressAdLoad(p0)
                } else {
                    callBack?.onError(1000, "没有拉取到广告")
                }
            }

            override fun onError(p0: Int, p1: String?) {
                callBack?.onError(p0, p1)
            }
        })
    }

    fun requestByteRewardVideoAd(
        data: NewsEntity,
        mActivity: Activity,
        mAdCallback: AdCallback<String>
    ) {
        val adSlot = AdSlot.Builder()
            .setCodeId(data.codeId)
            .setMediaExtra("media_extra") //附加参数
            .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
            .build()

        mTTAdNative?.loadRewardVideoAd(adSlot, object : TTAdNative.RewardVideoAdListener {
            override fun onError(p0: Int, p1: String?) {
//                Utils.showToast(AppContext.get(),"加载失败")
                mAdCallback?.onResult(-1, "失败")
            }

            override fun onRewardVideoAdLoad(p0: TTRewardVideoAd?) {
//                Utils.showToast(AppContext.get(),"广告加载成功")
            }

            override fun onRewardVideoCached() {
            }

            override fun onRewardVideoCached(ad: TTRewardVideoAd?) {
//                Utils.showToast(AppContext.get(),"广告缓存加载成功")

                ad?.setRewardAdInteractionListener(object :
                    TTRewardVideoAd.RewardAdInteractionListener {
                    override fun onAdShow() {
                        AdUpload.onAdImpression(data)
                    }

                    override fun onAdVideoBarClick() {
                        AdUpload.onAdClick(data)
                    }

                    override fun onAdClose() {
//                        Utils.showToast(AppContext.get(),"关闭广告")
                        mAdCallback?.onResult(0, "成功")
                    }

                    override fun onVideoComplete() {

                    }

                    override fun onVideoError() {
                        mAdCallback?.onResult(-1, "失败")
                    }

                    override fun onRewardVerify(
                        p0: Boolean,
                        p1: Int,
                        p2: String?,
                        p3: Int,
                        p4: String?
                    ) {
                    }

                    override fun onRewardArrived(p0: Boolean, p1: Int, p2: Bundle?) {

                    }

                    override fun onSkippedVideo() {

                    }
                })
                ad?.showRewardVideoAd(
                    mActivity,
                    TTAdConstant.RitScenes.CUSTOMIZE_SCENES,
                    "scenes_test"
                );
            }
        })

    }

    fun requestRewardVidedo(adCallback: AdCallback<String>) {
        val slotId = "video"
        adApi.getAds(slotId)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<NewsEntity>>() {
                override fun onSuccess(result: List<NewsEntity>?) {
                    if (result != null && result.size > 0) {
                        requestByteRewardVideoAd(result[0]!!, context as Activity, adCallback)
                    } else {
                        adCallback.onResult(-1, "失败")
                    }
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String) {
                    adCallback.onResult(-1, "失败")
                }
            })
    }
}