package com.ym.library.utils

import android.app.Activity
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.FrameLayout
import com.ym.admodule.config.AdManager
import com.ym.admodule.config.ZXADSizeConfig
import com.ym.admodule.listener.ZXADVideoListener
import com.ym.library.listener.IAdVideoListener
import com.ym.library.module.StatusEntity
import com.ym.library.net.BaseObserver
import com.ym.library.net.GameApiClient
import com.ym.library.net.RxSchedulers

object AdUtils {
    val TAG = "AdUtils"
    var activity: Activity? = null
    var handler = object : Handler() {
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when(msg?.what) {
                0 -> {
                    if (activity != null) {
                        AdManager.loadNextCacheRewardVideoAd(ADConfig.AD_REWARD_VIDIO, activity!!)
                    }
                }
            }
        }
    }

    fun playRewardAd(activity: Activity, actionName: String, listener: IAdVideoListener) {
        this.activity = activity
        AdManager.playRewardAd(
            ADConfig.AD_REWARD_VIDIO,
            actionName,
            activity,
            object : ZXADVideoListener {
                override fun onSuccess() {}

                override fun onAdClose() {
                    listener?.onAdClose()
                    //预加载
//                    loadNextCacheRewardVideoAd(activity)
                    //看视频上报
                    getVideoAcceleratorsReceive();
                    handler?.removeCallbacksAndMessages(null)
                }

                override fun onAdShow() {
                    Utils.showToast(activity, "看完视频可获得奖励")
                    handler?.sendEmptyMessageDelayed(0, 2000)
                }

                override fun onAdVideoBarClick() {
                    Log.d(TAG, "onAdVideoBarClick:")
                }

                override fun onError(errorMsg: String?) {
                    listener?.onError(errorMsg)
                    Log.d(TAG, "onError:" + errorMsg)
                }
            })
    }


    fun showFeedAd(activity: Activity, actionName: String, view: FrameLayout) {
        AdManager.showFeedAd(
            ADConfig.AD_IMG_FADE,
            actionName,
            ZXADSizeConfig(
//                                Utils.px2dip(PhoneUtils.getScreenWidth(activity).toFloat()) - 40, 280
                Utils.px2dip(PhoneUtils.getScreenWidth(activity).toFloat()) - 40,
                ((Utils.px2dip(activity.windowManager?.defaultDisplay?.width!!.toFloat()) - 40) * 0.66).toInt()
            ),
            activity,
            view
        )
//        try {
//            view.postDelayed(object :Runnable {
//                override fun run() {
//                    if(activity?.isFinishing){
//                        return
//                    }
//
//                    activity?.runOnUiThread() {
//
//                    }
//                }
//            },0)
//        } catch (e: Exception) {
//        }
    }

    //预加载激励
    fun loadNextCacheRewardVideoAd(activity: Activity) {
        AdManager.loadNextCacheRewardVideoAd(ADConfig.AD_REWARD_VIDIO, activity)
    }

    //预加载信息流
    fun loadFeedCacheAdToCache(activity: Activity) {
        AdManager.loadFeedAdToCache(
            ADConfig.AD_IMG_FADE, activity, ZXADSizeConfig(
//                Utils.px2dip(PhoneUtils.getScreenWidth(activity).toFloat()) - 40, 280
                Utils.px2dip(PhoneUtils.getScreenWidth(activity).toFloat()) - 40,
                ((Utils.px2dip(activity.windowManager?.defaultDisplay?.width!!.toFloat()) - 40) * 0.66).toInt()
            )
        )
    }

    //看视频领上报
    fun getVideoAcceleratorsReceive() {
        GameApiClient.gameApi.adVideoReport()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<StatusEntity>() {
                override fun onSuccess(result: StatusEntity?) {
                    Log.d("wxw", "看视频上报 Success")
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.d("wxw", "看视频上报 error")
                }
            })
    }
}