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

import android.content.Context
import com.composition.android.ad.csj.util.buildCSJBannerAdSize
import com.composition.android.ad.gromore.util.buildGroMoreAdImageSize
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.composition.android.lib.ad.interfaces.AdSize
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.AppLovinMax,
            Advertisers.CSJ,
            Advertisers.GroMore,
            Advertisers.CUSTOM,
        )
    }

    var bgShowAppOpen = false

    val BANNER = "banner"
    val NATIVE = "native"
    val NATIVE_TEMPLATE = "NativeTemplate"
    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 = AdSize.Builder.create().buildCSJBannerAdSize(600F, 90F)),
            AdUnitBean(NATIVE_TEMPLATE, Advertisers.CSJ.name, AdType.NATIVE_TEMPLATE.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))
        )

    val GroMoreAdUnitList: List<AdUnitBean> =
        listOf(
            AdUnitBean(BANNER, Advertisers.GroMore.name, AdType.BANNER.name, "102138574", adSize = AdSize.Builder.create().buildGroMoreAdImageSize(600, 90)),
            AdUnitBean(INTERSTITIAL, Advertisers.GroMore.name, AdType.INTERSTITIAL.name, "102138039"),
            AdUnitBean(APP_OPEN, Advertisers.GroMore.name, AdType.APP_OPEN.name, "102137355"),
            AdUnitBean(REWARDED_VIDEO, Advertisers.GroMore.name, AdType.REWARDED_VIDEO.name, "102137844"),
            AdUnitBean(NATIVE_TEMPLATE, Advertisers.GroMore.name, AdType.NATIVE_TEMPLATE.name, "102140510")
        )


    val AppLovinMaxAdUnitList: List<AdUnitBean> =
        listOf(
            AdUnitBean(BANNER, Advertisers.AppLovinMax.name, AdType.BANNER.name, "63703f4a44a542c6"),
            AdUnitBean(NATIVE_TEMPLATE, Advertisers.AppLovinMax.name, AdType.NATIVE_TEMPLATE.name, "653e223426fd9778"),
            AdUnitBean(INTERSTITIAL, Advertisers.AppLovinMax.name, AdType.INTERSTITIAL.name, "209850248dc00c9d", requestAdConfig = AdUnitLoadConfig(2, 200000)),
            AdUnitBean(REWARDED_VIDEO, Advertisers.AppLovinMax.name, AdType.REWARDED_VIDEO.name, "cffda14986a7bf4b"),
        )



    suspend fun getNativeADSampleLists(context: Context, adSlotName: String): 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.ITEM_NORMAL, media = it)
        }
        list.addAll(files)


        val adList = mutableListOf<AdResult.Success>()
        for (i in 0..3) {
            val adResult = AdLoad.loadAd(context, adSlotName).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)
            val viewType = if (success.adBean.getAdType() == AdType.NATIVE) NativeAdSampleListAdapter.ITEM_NATIVE_AD else NativeAdSampleListAdapter.ITEM_NATIVE_TEMPLATE_AD
            list.add(randomNum, NativeListBean(viewType = viewType, success = success))
        }

        return list
    }

}