package com.convenient.android.lib.ui.sample.ad.model

import android.content.Context
import com.composition.android.ad.csj.impl.CSJBannerAdSize
import com.composition.android.lib.ad.AdLoad
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.AdType
import com.composition.android.lib.ad.basic.Advertisers
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.bean.AdUnitLoadConfig
import com.convenient.android.common.media.MediaBean
import com.convenient.android.lib.ui.sample.ad.adapter.NativeAdSampleListAdapter
import kotlinx.coroutines.flow.firstOrNull
import kotlin.random.Random

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/5
 * description:
 */
object Datas {

    fun getAdvertisers(): List<Advertisers> {
        return listOf(
            Advertisers.Admob,
            Advertisers.AppLovin,
            Advertisers.CSJ,
            Advertisers.GroMore,
            Advertisers.CUSTOM,
        )
    }

    var bgShowAppOpen = false

    val BANNER = "banner"
    val NATIVE = "native"
    val INTERSTITIAL = "interstitial"
    val REWARDED_VIDEO = "rewarded_video"
    val REWARDED_INTERSTITIAL = "rewarded_interstitial"
    val APP_OPEN = "app_open"


    val AdmobAdUnitList: List<AdUnitBean> =
        listOf(
            AdUnitBean(BANNER, Advertisers.Admob.name, AdType.BANNER.name, "ca-app-pub-3940256099942544/6300978111"),
            AdUnitBean(NATIVE, Advertisers.Admob.name, AdType.NATIVE.name, "ca-app-pub-3940256099942544/2247696110"),
            AdUnitBean(INTERSTITIAL, Advertisers.Admob.name, AdType.INTERSTITIAL.name, "ca-app-pub-3940256099942544/1033173712"),
            AdUnitBean(REWARDED_VIDEO, Advertisers.Admob.name, AdType.REWARDED_VIDEO.name, "ca-app-pub-3940256099942544/5224354917"),
            AdUnitBean(REWARDED_INTERSTITIAL, Advertisers.Admob.name, AdType.REWARDED_INTERSTITIAL.name, "ca-app-pub-3940256099942544/5354046379"),
            AdUnitBean(APP_OPEN, Advertisers.Admob.name, AdType.APP_OPEN.name, "ca-app-pub-3940256099942544/3419835294", requestAdConfig = AdUnitLoadConfig(adLoadRetryCount = 1, adLoadTimeOut = 5000))
        )


    val CSJAdUnitList : List<AdUnitBean> =
        listOf(
            AdUnitBean(BANNER, Advertisers.CSJ.name, AdType.BANNER.name, "949726232", adSize = CSJBannerAdSize(600F, 90F)),
            AdUnitBean(NATIVE, Advertisers.CSJ.name, AdType.NATIVE.name, "949726254"),
            AdUnitBean(INTERSTITIAL, Advertisers.CSJ.name, AdType.INTERSTITIAL.name, "949726267"),
            AdUnitBean(REWARDED_VIDEO, Advertisers.CSJ.name, AdType.REWARDED_VIDEO.name, "949726565"),
            AdUnitBean(APP_OPEN, Advertisers.CSJ.name, AdType.APP_OPEN.name, "887922330", requestAdConfig = AdUnitLoadConfig(adLoadRetryCount = 1, adLoadTimeOut = 5000))
        )


    suspend fun getNativeADSampleLists(context: Context): List<NativeListBean> {

        val list = mutableListOf<NativeListBean>()

        var files = (0..100).map {
            MediaBean(name = "测试:${it}", lastModified = System.currentTimeMillis(), isFile = true, extension = "png", parentPath = "", mediaPath = "")
        }.map {
            NativeListBean(viewType = NativeAdSampleListAdapter.NORMAL_ITEM, media = it)
        }
        list.addAll(files)


        val adList = mutableListOf<AdResult.Success>()
        for (i in 0..4) {
            val adResult = AdLoad.loadAd(context, NATIVE).firstOrNull()
            if (adResult is AdResult.Success) {
                adList.add(adResult)
            }
        }

        for (success in adList) {
            val rand = Random(System.nanoTime())
            val randomNum = (0..list.size).random(rand)
            list.add(randomNum, NativeListBean(viewType = NativeAdSampleListAdapter.AD_ITEM, success = success))
        }

        return list
    }

}