package com.ym.admodule.fs

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.`fun`.xm.ad.FSThirdAd
import com.`fun`.xm.ad.adloader.FSRewardVideoAdLoader
import com.`fun`.xm.ad.adloader.FSSRFeedAdLoader
import com.`fun`.xm.ad.adloader.FSSplashAdLoader
import com.`fun`.xm.ad.callback.FSFeedAdCallBack
import com.`fun`.xm.ad.callback.FSSplashAdCallBack
import com.`fun`.xm.ad.fsadview.FSFeedAD
import com.`fun`.xm.ad.fsadview.FSRewardVideoView
import com.`fun`.xm.ad.fsadview.FSSplashAD
import com.`fun`.xm.ad.listener.FSRewardVideoADListener
import com.ym.admodule.config.AdInfo
import com.ym.admodule.config.ZXADCallback
import com.ym.admodule.listener.ZXADSplashListener
import com.ym.admodule.listener.ZXADVideoListener
import com.ym.modulecommon.module.NewsEntity
import com.ym.modulecommon.utils.AdEventUtils
import com.ym.modulecommon.utils.AdPreference
import com.ym.modulecommon.utils.LogUtils


/**
 * Created by erwin on 2021/4/9
 * 风行 广告
 *
 */
object FunShionUtils {


    private val TAG = "《风行》FunShionUtils"

    /**
     * 加载开屏广告
     */
    fun loadSplash(
        newsEntity: NewsEntity,
        activity: Activity,
        container: ViewGroup,
        skipview: TextView?,
        zxadListener: ZXADSplashListener,
        callback: ZXADCallback
    ) {
        skipview?.visibility = View.GONE
        var mFSSplashAD: FSSplashAD? = null
        var mFSAdLoader = FSSplashAdLoader(activity)
        mFSAdLoader.loadAD(newsEntity.codeId, AdPreference.getOaid(), object : FSSplashAdCallBack {
            override fun onCreate(fsadView: FSSplashAD) {
                LogUtils.e(TAG, "onCreate  ")
                container.addView(fsadView)
                mFSSplashAD = fsadView
            }

            override fun onCreateThirdAD(fsThirdAd: List<FSThirdAd>) {
            }

            override fun onClose() {
                if (mFSSplashAD != null) {
                    mFSSplashAD?.destroy()
                    mFSSplashAD = null
                }
                zxadListener.onAdSkip()
                AdEventUtils.onEventComplete(newsEntity,AdEventUtils.AD_MODEL_SPLAH)
            }

            override fun onZoomOut() {
            }

            override fun onAdsTimeUpdate(sec: Int) {
                LogUtils.e(TAG, "onAdsTimeUpdate:" + sec)
                mFSSplashAD?.setSkipViewContent("跳过 " + sec / 1000)
                if (sec / 1000 == 0) {
                    zxadListener.onAdSkip()
                }
            }

            override fun onADShow() {
                zxadListener.onAdShow()
                AdEventUtils.onEventShow(newsEntity)
            }

            override fun onClick() {
                AdEventUtils.onEventClick(newsEntity,AdEventUtils.AD_MODEL_SPLAH)
            }

            override fun onAdLoadSuccess() {
                callback.onAdSuccess()
                AdEventUtils.onEventPullSuccess(newsEntity, AdEventUtils.AD_MODEL_SPLAH)
            }

            override fun onAdLoadedFail(
                param1Int: Int,
                param1String: String
            ) {
                callback.onAdFail()
                LogUtils.e(TAG, "onAdLoadedFail code = $param1Int ; msg = $param1String")
                zxadListener.onError(param1String)
                AdEventUtils.onEventPullFail(newsEntity,AdEventUtils.AD_MODEL_SPLAH)
                AdEventUtils.onEvent(newsEntity,param1Int,param1String)
            }
        })
    }

    private val rewradArray = arrayListOf<FSRewardVideoView>()


    fun loadRewardVideo(
        activity: Activity,
        entity: NewsEntity,
        listener: ZXADVideoListener,
        callback: ZXADCallback
    ) {
        var mFsAdLoader = FSRewardVideoAdLoader(activity)

        mFsAdLoader.loadAD(entity.codeId,AdPreference.getOaid(),object :FSRewardVideoADListener<FSRewardVideoView>{
            override fun onAdLoadedFail(p0: Int, p1: String?) {
                LogUtils.e(TAG,"直接加载 onAdLoadedFail   ")
                callback?.onAdFail()
                listener.onError(p1)
                AdEventUtils.onEventPullFail(entity,AdEventUtils.AD_MODEL_REWARD)
                AdEventUtils.onEvent(entity,p0,p1)
            }

            override fun onLoadSuccess(fsADView: FSRewardVideoView?) {
                LogUtils.e(TAG,"直接加载 onLoadSuccess   ")
                if (fsADView != null) {
                    fsADView.showAD()
                    callback?.onAdSuccess()
                }
                listener.onSuccess()
                AdEventUtils.onEventPullSuccess(entity,AdEventUtils.AD_MODEL_REWARD)
            }

            override fun onLoadStart() {
            }

            override fun onADClicked() {
                LogUtils.e(TAG,"直接加载 onADClicked   ")
                listener.onAdVideoBarClick()
                AdEventUtils.onEventClick(entity,AdEventUtils.AD_MODEL_REWARD)

            }

            override fun onCreateThirdAD(p0: MutableList<FSThirdAd>?) {
            }

            override fun onADComplete() {
                LogUtils.e(TAG,"直接加载 onADComplete   ")
                listener.onAdClose(AdInfo(entity.codeId))
                AdEventUtils.onEventComplete(entity,AdEventUtils.AD_MODEL_REWARD)
            }

            override fun onADShow() {
                LogUtils.e(TAG,"直接加载 onADShow   ")
                listener.onAdShow()
                AdEventUtils.onEventShow(entity)
            }

        })


    }


    fun loadCacheRewardVideo(
        activity: Activity,
        entity: NewsEntity,
        callback: ZXADCallback
    ) {
        var mFsAdLoader = FSRewardVideoAdLoader(activity)

        mFsAdLoader.loadAD(entity.codeId,AdPreference.getOaid(),object :FSRewardVideoADListener<FSRewardVideoView>{
            override fun onAdLoadedFail(p0: Int, p1: String?) {
                LogUtils.e(TAG,"onAdLoadedFail   ")
                callback?.onAdFail()
                listener?.onError(p1)
                AdEventUtils.onEventPullFail(entity,AdEventUtils.AD_MODEL_REWARD)
                AdEventUtils.onEvent(entity,p0,p1)
            }

            override fun onLoadSuccess(fsADView: FSRewardVideoView?) {
                LogUtils.e(TAG,"onLoadSuccess   ")
                if (fsADView != null) {
                    rewradArray.add(fsADView)
                    callback?.onAdSuccess()
                }
                listener?.onSuccess()
                AdEventUtils.onEventPullSuccess(entity,AdEventUtils.AD_MODEL_REWARD)
            }

            override fun onLoadStart() {
            }

            override fun onADClicked() {
                LogUtils.e(TAG,"onADClicked   ")
                listener?.onAdVideoBarClick()
                AdEventUtils.onEventClick(entity,AdEventUtils.AD_MODEL_REWARD)

            }

            override fun onCreateThirdAD(p0: MutableList<FSThirdAd>?) {
            }

            override fun onADComplete() {
                LogUtils.e(TAG,"onADComplete   ")
                listener?.onAdClose(AdInfo(entity.codeId))
                AdEventUtils.onEventComplete(entity,AdEventUtils.AD_MODEL_REWARD)
            }

            override fun onADShow() {
                LogUtils.e(TAG,"onADShow   ")
                listener?.onAdShow()
                AdEventUtils.onEventShow(entity)
            }

        })
    }
    private var listener:ZXADVideoListener? = null

    fun playReward(activity: Activity, newsEntity: NewsEntity, listener: ZXADVideoListener,callback: ZXADCallback){
        if ( activity != null && newsEntity.codeId != null &&rewradArray != null &&rewradArray.isNotEmpty() && rewradArray[rewradArray.size - 1] != null) {
            val mTTad = rewradArray[rewradArray.size - 1]
            if (mTTad != null) {
                this.listener = listener
                mTTad.showAD()
            }
        }else{
            loadRewardVideo(activity,newsEntity,listener,callback)
        }
    }

}