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

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.composition.android.lib.ad.AdLoad
import com.composition.android.lib.ad.basic.AdResult
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.ActivityAdAppOpenBinding
import com.convenient.android.lib.ui.sample.ad.model.Datas
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/9
 * description:
 */
class AdAppOpenActivity : BaseBindingActivity<ActivityAdAppOpenBinding>(ActivityAdAppOpenBinding::inflate) {

    private val status = MutableStateFlow(Datas.bgShowAppOpen)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            status.collect{
                binding.btnOpenBgAppOpen.text = getString(R.string.app_open_1, if (it) getString(R.string.open) else getString(R.string.close))
            }
        }
        setViewsClick({
            when (it.id) {
                R.id.btn_load_app_open -> {
                    lifecycleScope.launch {
                        AdLoad.loadAd(this@AdAppOpenActivity, Datas.APP_OPEN)
                            .collect{
                                if (it is AdResult.Success){
                                    AdLoad.addToCache(it)
                                    ToastUtil.showLongToast(this@AdAppOpenActivity, "开屏广告加载成功")
                                }
                            }
                    }
                }
                R.id.btn_show_app_open -> {
                    readyGo(AppWelcomeActivity::class.java){
                        it.putExtra(AppWelcomeActivity.ONLY_SHOW, true)
                    }
                }
                R.id.btn_open_bg_app_open->{
                    Datas.bgShowAppOpen = Datas.bgShowAppOpen.not()
                    status.value = Datas.bgShowAppOpen
                }
                else -> {}
            }
        }, binding.btnLoadAppOpen, binding.btnShowAppOpen, binding.btnOpenBgAppOpen)
    }


}