package com.remax.bill.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.remax.bill.BuildConfig
import com.remax.bill.R
import com.remax.bill.ads.AdException
import com.remax.bill.ads.AdMobManager
import com.remax.bill.ads.AdResult
import com.remax.bill.ads.FullScreenNativeAdController
import com.remax.bill.ads.ext.AdShowExt
import com.remax.bill.ads.log.AdLogger
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

/**
 * 全屏原生广告Activity
 * 展示全屏的原生广告内容，通常用于应用启动或重要操作前
 */
class FullScreenNativeAdActivity : AppCompatActivity() {

    companion object {
        private const val TAG = "FullScreenNativeAdActivity"

        /**
         * 启动全屏原生广告Activity
         * @param activity 启动Activity
         * @return AdResult<Unit> 广告显示结果
         */
        suspend fun start(activity: Activity, showInterstitial: Boolean = true): AdResult<Unit> {
            return suspendCancellableCoroutine { continuation ->

                val intent = Intent(activity, FullScreenNativeAdActivity::class.java)
                intent.putExtra("showInterstitial", showInterstitial)
                activity.startActivity(intent)
                activity.overridePendingTransition(
                    android.R.anim.fade_in,
                    android.R.anim.fade_out
                )

                // 存储continuation以便在Activity中调用
                FullScreenNativeAdActivity.continuation = continuation
            }
        }

        // 用于存储continuation的变量
        @Volatile
        private var continuation: kotlinx.coroutines.CancellableContinuation<AdResult<Unit>>? = null

        /**
         * 设置结果并恢复continuation
         */
        fun setResult(result: AdResult<Unit>) {
            continuation?.let { cont ->
                if (cont.isActive) {
                    cont.resume(result)
                }
            }
            continuation = null
        }
    }

    private val fullScreenNativeController = AdMobManager.Controllers.fullScreenNative
    private val interstitialController = AdMobManager.Controllers.interstitial
    private val isShowInterstitial: Boolean
        get() = intent.getBooleanExtra("showInterstitial", true)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        window.apply {
            addFlags(android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

            @Suppress("DEPRECATION")
            navigationBarColor = Color.TRANSPARENT
        }
        setContentView(R.layout.activity_full_screen_native_ad)
        loadAndShowFullScreenNativeAd()
        if (isShowInterstitial) {
            showInterstitialAdAndNavigate {}
        }
    }

    /**
     * 加载并显示全屏原生广告
     */
    private fun loadAndShowFullScreenNativeAd() {
        lifecycleScope.launch {
            try {
                when (val result = fullScreenNativeController.showAdInContainer(
                    context = this@FullScreenNativeAdActivity,
                    container = findViewById<ViewGroup>(R.id.adContainer),
                    lifecycleOwner = this@FullScreenNativeAdActivity,
                    adUnitId = BuildConfig.ADMOB_FULL_NATIVE_ID
                )) {
                    is AdResult.Success -> {
                        findViewById<View>(R.id.rl_top_buttons).apply {
                            isVisible = true
                            findViewById<View>(R.id.btn_close).setOnClickListener {
                                FullScreenNativeAdController.getInstance().closeEvent( adUnitId = BuildConfig.ADMOB_FULL_NATIVE_ID)
                                closeAdAndFinish()
                            }
                        }
                        AdLogger.d("全屏原生广告页面加载成功")
                        // 广告加载成功，展示页面，等待用户关闭时回调结果
                        // 不在这里设置结果，而是在页面关闭时设置
                    }

                    is AdResult.Failure -> {
                        // 广告加载失败，立即返回失败结果
                        setResult(result)
                        closeAdAndFinish()
                    }

                    AdResult.Loading -> {
                        // 广告正在加载中，等待结果
                        AdLogger.d("全屏原生广告正在加载中")
                    }
                }
            } catch (e: Exception) {
                // 异常情况，立即返回失败结果
                AdLogger.e("全屏原生广告页面加载失败:${e.message}")
                setResult(
                    AdResult.Failure(
                        AdException(
                            code = -2,
                            message = "全屏原生广告加载异常: ${e.message}",
                            cause = e
                        )
                    )
                )
                closeAdAndFinish()
            }
        }
    }

    private fun showInterstitialAdAndNavigate(call: () -> Unit) {
        lifecycleScope.launch {
            try {
                // 直接显示广告（自动处理加载）
                when (val result = AdShowExt.showInterstitialAd(
                    this@FullScreenNativeAdActivity, ignoreFullNative = true
                )) {
                    is AdResult.Success -> {
                        call.invoke()
                    }

                    is AdResult.Failure -> {
                        call.invoke()
                    }

                    AdResult.Loading -> {

                    }
                }

            } catch (e: Exception) {

            }
        }
    }

    /**
     * 关闭广告并结束Activity
     */
    private fun closeAdAndFinish() {
        // 如果还没有设置结果（说明是用户主动关闭），设置成功结果
        if (continuation != null) {
            setResult(AdResult.Success(Unit))
        }
        finish()
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
    }

    override fun onDestroy() {
        super.onDestroy()
        // 如果Activity被销毁但还没有设置结果，设置失败结果
        if (continuation != null) {
            setResult(
                AdResult.Failure(
                    AdException(
                        code = -3,
                        message = "Activity被销毁"
                    )
                )
            )
        }
    }

    override fun onBackPressed() {
        // 禁用返回键，只能通过广告关闭按钮关闭
    }
} 