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.buildAdNativeViewHolder
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.readyGo
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.ActivityAdNativeBinding
import com.convenient.android.lib.databinding.ViewAdNativeBinding
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 AdNativeActivity : BaseBindingActivity<ActivityAdNativeBinding>(ActivityAdNativeBinding::inflate) {

    private var adResult: AdResult.Success? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewsClick({
            when (it.id) {
                R.id.btn_load_native_in_recyclerview->{
                    readyGo(AdNativeListActivity::class.java){
                        it.putExtra("ad_slot_name", Datas.NATIVE)
                    }
                }
                R.id.btn_load_native -> {
                    loadNativeAd { success ->
                        adResult = success
                    }
                }
                R.id.btn_show_native -> {
                    lifecycleScope.launch {
                        delay(1000)
                        showNativeAd()
                    }
                }
                R.id.btn_load_and_show -> {
                    lifecycleScope.launch {
                        binding.llAdContent.removeAllViews()
                        delay(1000)
                        loadNativeAd {
                            adResult = it
                            showNativeAd()
                        }
                    }
                }
            }

        }, binding.btnLoadNative, binding.btnShowNative, binding.btnLoadAndShow,binding.btnLoadNativeInRecyclerview)
    }

    private fun loadNativeAd(success: (AdResult.Success) -> Unit) {
        lifecycleScope.launch {
            AdLoad.loadAd(this@AdNativeActivity, Datas.NATIVE)
                .collect {
                    if (it is AdResult.Success) {
                        ToastUtil.showLongToast(this@AdNativeActivity, "加载原生广告成功")
                        success.invoke(it)
                    } else {
                        adLogE(AdManager.TAG, it.msg)
                    }
                }
        }
    }

    private fun showNativeAd() {
        adResult?.let {

            val nativeViewBinding = ViewAdNativeBinding.inflate(layoutInflater)
            val viewHolder = buildAdNativeViewHolder {
                this.customLayoutRootView = nativeViewBinding.root
                this.titleView = nativeViewBinding.tvAdTitle
                this.titleDescView = nativeViewBinding.tvAdTitleDesc
                this.iconView = nativeViewBinding.ivAdIcon
                this.contentMediaViewGroup = nativeViewBinding.flAdMediaContent
                this.callActionButtonView = nativeViewBinding.btnCallToAction
                this.dislikeView
                this.activity= this@AdNativeActivity
                this.adListener = object : NormalAdListener(){
                    override fun onAdDisLike() {
                        super.onAdDisLike()
                        binding.llAdContent.isVisible = false
                        binding.llAdContent.removeAllViews()
                    }
                }
            }

            AdLoad.getNativeAdView(this, it, viewHolder)?.let {
                binding.llAdContent.removeAllViews()
                binding.llAdContent.addView(it)
                binding.llAdContent.isVisible = true
            }

        } ?: ToastUtil.showLongToast(this, "请先加载广告")
    }


}