package solutions.tretter.mindmachine.ads

import android.app.Activity
import android.view.ViewGroup
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback

@Composable
fun AdBanner(modifier: Modifier = Modifier, enabled: Boolean) {
    if (!enabled) return

    val context = LocalContext.current

    AndroidView(
        modifier = modifier,
        factory = {
            AdView(context).apply {
                setAdSize(AdSize.BANNER)
                adUnitId = AdUnits.BANNER
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
                loadAd(AdRequest.Builder().build())
            }
        },
        update = { /* no-op */ }
    )
}

class InterstitialController(private val activity: Activity) {
    private var interstitial: InterstitialAd? = null
    private var loading: Boolean = false

    fun preload() {
        if (interstitial != null || loading) return
        loading = true
        InterstitialAd.load(
            activity,
            AdUnits.INTERSTITIAL,
            AdRequest.Builder().build(),
            object : InterstitialAdLoadCallback() {
                override fun onAdLoaded(ad: InterstitialAd) {
                    loading = false
                    interstitial = ad
                }

                override fun onAdFailedToLoad(error: LoadAdError) {
                    loading = false
                    interstitial = null
                }
            }
        )
    }

    fun show(onDone: () -> Unit) {
        val ad = interstitial
        if (ad == null) {
            onDone()
            return
        }

        ad.fullScreenContentCallback = object : FullScreenContentCallback() {
            override fun onAdDismissedFullScreenContent() {
                interstitial = null
                preload()
                onDone()
            }

            override fun onAdFailedToShowFullScreenContent(p0: com.google.android.gms.ads.AdError) {
                interstitial = null
                preload()
                onDone()
            }
        }

        ad.show(activity)
    }
}

@Composable
fun rememberInterstitialController(enabled: Boolean): InterstitialController? {
    if (!enabled) return null
    val activity = LocalContext.current as? Activity ?: return null

    val controller = remember(activity) { InterstitialController(activity) }

    LaunchedEffect(controller) {
        controller.preload()
    }

    return controller
}
