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

import android.annotation.SuppressLint
import android.content.Context
import android.content.ContextWrapper
import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.composition.android.lib.ad.AdLoad
import com.composition.android.lib.ad.basic.AdResult
import com.convenient.android.common.utils.ToastUtil
import com.convenient.android.lib.ui.sample.ad.AdMainActivity
import com.convenient.android.lib.ui.sample.ad.model.Datas
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/9/6
 * description:
 */

@Composable
fun AdInterstitialPage() {

    val composableScope = rememberCoroutineScope()
    val context = LocalContext.current
    val activity = LocalContext.current.getActivity<AdMainActivity>()

    var adResult: AdResult.Success? = null

    Column {
        val modifier = Modifier.fillMaxWidth()
        FilledTonalButton(modifier = modifier, onClick = {
            loadInterstitial(context, composableScope) {
                adResult = it
            }
        }) {
            Text(text = "加载插屏广告")
        }

        FilledTonalButton(modifier = modifier, onClick = {
            adResult?.let {
                if (activity != null) {
                    showInterstitial(activity, it)
                }

            }
        }) {
            Text(text = "展示插屏广告")
        }


        FilledTonalButton(modifier = modifier, onClick = {
            loadInterstitial(context = context, composableScope){
                adResult = it
                adResult?.let {
                    if (activity != null) {
                        showInterstitial(activity, it)
                    }
                }
            }
        }) {
            Text(text = "加载并展示插屏广告")
        }
    }

}



@SuppressLint("CoroutineCreationDuringComposition")
private fun loadInterstitial(context: Context, composable: CoroutineScope, result: (AdResult.Success) -> Unit) {
    composable.launch {
        AdLoad.loadAd(context, Datas.INTERSTITIAL)
            .collect {
                if (it is AdResult.Success) {
                    result.invoke(it)
                    ToastUtil.showLongToast(context, "插屏广告加载成功")
                }
            }
    }
}


private fun showInterstitial(activity: ComponentActivity, adResult: AdResult.Success) {
//    AdLoad.showInterstitialAd(activity, success = adResult) {
//
//    }

}


inline fun <reified Activity : ComponentActivity> Context.getActivity(): Activity? {
    return when (this) {
        is Activity -> this
        else -> {
            var context = this
            while (context is ContextWrapper) {
                context = context.baseContext
                if (context is Activity) return context
            }
            null
        }
    }
}