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

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.composition.android.lib.ad.AdLoad
import com.composition.android.lib.ad.AdManager
import com.composition.android.lib.ad.basic.AdResult
import com.composition.android.lib.ad.basic.buildBannerAdViewHolder
import com.composition.android.lib.ad.bean.AdUnitBean
import com.composition.android.lib.ad.impl.NormalAdListener
import com.composition.android.lib.ad.util.adLogE
import com.convenient.android.common.base.viewbinding.BaseBindingActivity
import com.convenient.android.common.extension.setViewsClick
import com.convenient.android.common.utils.ToastUtil
import com.convenient.android.lib.R
import com.convenient.android.lib.databinding.ActivityAdBannerBinding
import com.convenient.android.lib.ui.sample.ad.model.Datas
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/6
 * description: 横幅广告示例
 */
class AdBannerActivity : BaseBindingActivity<ActivityAdBannerBinding>(ActivityAdBannerBinding::inflate) {

    private var bannerAdResult: AdResult.Success? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewsClick({
            when (it.id) {
                R.id.btn_load_banner -> {
                    loadBanner()
                }
                R.id.btn_show_banner -> {
                    lifecycleScope.launch {
                        delay(1000)
                        showBanner()
                    }
                }
                R.id.btn_load_and_show -> {
                    lifecycleScope.launch {
                        binding.llAdContent.removeAllViews()
                        delay(1000)
                        loadBanner(true)
                    }
                }
            }

        }, binding.btnLoadBanner, binding.btnShowBanner, binding.btnLoadAndShow)
    }

    private fun loadBanner(show: Boolean = false) {
        lifecycleScope.launch {
            AdLoad.loadAd(this@AdBannerActivity, Datas.BANNER)
                .collect {
                    if (it is AdResult.Success) {
                        bannerAdResult = it
                        ToastUtil.showLongToast(this@AdBannerActivity, "加载Banner广告成功")
                        if (show) {
                            showBanner()
                        }
                    } else {
                        adLogE(AdManager.TAG, it.msg)
                    }
                }
        }
    }

    private fun showBanner() {
        bannerAdResult?.let {
            AdLoad.getBannerAdView(this, it, buildBannerAdViewHolder {
                this.activity = this@AdBannerActivity
                this.adListener = object : NormalAdListener(){
                    override fun onAdDisLike() {
                        super.onAdDisLike()
                        binding.llAdContent.removeAllViews()
                        binding.llAdContent.isVisible = false
                    }
                }
            })?.let { adView ->
                binding.llAdContent.removeAllViews()
                binding.llAdContent.addView(adView)
                binding.llAdContent.isVisible = true
            }
        } ?: ToastUtil.showLongToast(this, "请先加载广告")
    }


}