package com.ym.admodule.news

import android.app.Activity
import android.content.Intent
import com.ym.admodule.config.AdCache
import com.ym.admodule.config.AdInfo
import com.ym.admodule.config.ZXADCallback
import com.ym.admodule.csj.TTADUtils
import com.ym.admodule.ks.KSADUtils
import com.ym.admodule.listener.ZXADVideoListener
import com.ym.modulecommon.module.NewsEntity
import com.ym.modulecommon.net.BaseObserver
import com.ym.modulecommon.net.RxSchedulers
import com.ym.modulecommon.net.ZXNewsClient
import com.ym.modulecommon.news.AdNewsActivity
import com.ym.modulecommon.news.entity.AdNewsInfomationEntity
import com.ym.modulecommon.utils.LogUtils

/**
 * Created by erwin on 2021/1/19
 *
 */
object NewsUtils {

    fun showReward(
        activity: Activity,
        listener: ZXADVideoListener,
        callback: ZXADCallback
    ) {
        if (activity != null && arrayList.isNotEmpty()&&  arrayList[arrayList.size - 1] != null) {
            val adNewsInfomationEntity = arrayList[arrayList.size - 1]
            AdNewsActivity.create(activity, adNewsInfomationEntity,object : NewsListener {
                override fun onSuccess() {
                    LogUtils.e("新闻赚  ","新闻赚   onSuccess")
                    listener.onSuccess()
                }
                override fun onError(errorMsg: String?) {
                    LogUtils.e("新闻赚  ","新闻赚   onError")
                    listener.onError(errorMsg)
                }

                override fun onAdShow() {
                    LogUtils.e("新闻赚  ","新闻赚   onAdShow")
                    listener.onAdShow()
                }
                override fun onAdVideoBarClick() {
                    LogUtils.e("新闻赚  ","新闻赚   onAdVideoBarClick")
                    listener.onAdVideoBarClick()
                }
                override fun onAdClose() {
                    LogUtils.e("新闻赚  ","新闻赚   onAdClose")
                    listener.onAdClose(AdInfo(""))
                }

            })
        } else {
            LogUtils.e("新闻赚  ","新闻赚   onAdFail")
            callback.onAdFail()
        }


    }


    private var arrayList = arrayListOf<AdNewsInfomationEntity>()

    //与加载新闻赚
    fun loadReward(
        callback: ZXADCallback
    ) {

        ZXNewsClient.adApi.getInfomation()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AdNewsInfomationEntity?>() {
                override fun onSuccess(result: AdNewsInfomationEntity?) {
                    if (result != null) {
                        if (arrayList.size > AdCache.interNewNumber) {
                            arrayList.removeAt(0)
                        }
                        arrayList.add(result)
                        callback.onAdSuccess()
                    }else{
                        callback.onAdFail()
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    callback.onAdFail()
                }
            })



    }
}