package com.assimilate.alltrans.viewui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.Button
import androidx.activity.OnBackPressedCallback
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.assimilate.alltrans.MyApp
import com.assimilate.alltrans.common.FirebaseAnalyticsHelper
import com.assimilate.alltrans.common.LolAdWrapper
import com.assimilate.alltrans.databinding.ActivityWelBinding
import com.lol.apex.ok.google.adlibrary.base.listener.LolLoadError
import com.lol.apex.ok.google.adlibrary.base.listener.LolShowError

class WelActivity : AppCompatActivity(){
    private lateinit var binding: ActivityWelBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        enableEdgeToEdge()
        super.onCreate(savedInstanceState)
        binding = ActivityWelBinding.inflate(layoutInflater)
        setContentView(binding.root)
        //debugFirebase()

        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initSet()
    }

    private fun debugFirebase() {
        val crashButton = Button(this)
        crashButton.text = "Test Crash"
        crashButton.setOnClickListener {
            throw RuntimeException("Test Crash") // Force a crash
        }

        addContentView(
            crashButton, ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        )
    }

    private fun initSet() {
        backPressedCall()
        if (MyApp.app.isFirstLaunch) {
            adSplash(MyApp.Config.start_cold_int_auto)
        } else {
            adSplashHot(MyApp.Config.start_hot_int_auto)
        }
    }


    private fun backPressedCall() {
        val callback = object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
            }
        }
        onBackPressedDispatcher.addCallback(this, callback)
    }

    override fun onResume() {
        super.onResume()
        FirebaseAnalyticsHelper.launchPvEvent()
    }


    private fun toNextPage() {
        startActivity(Intent(this@WelActivity, MainActivity::class.java))
    }

    private fun adSplash(place: String) {
        // 加载广告
        LolAdWrapper.shared.loadAdIfNotCached(
            this,
            place,
            object : LolAdWrapper.LoLLoadListener {
                override fun loadFailed(error: LolLoadError?) {
                    // 处理加载失败
                    toNextPage()
                }

                override fun loaded() {
                    // 处理加载成功
                    // 如果需要立刻展示广告，可以调用展示方法
                    LolAdWrapper.shared.showAdIfCached(
                        this@WelActivity,
                        place,
                        object : LolAdWrapper.LolShowListener {
                            override fun shown() {
                                // 处理广告展示成功
                                Log.d("fcasc", place)
                            }

                            override fun showFailed(error: LolShowError?) {
                                // 处理广告展示失败
                                toNextPage()
                            }

                            override fun closed() {
                                // 处理广告关闭
                                toNextPage()
                            }
                        })
                }
            })

    }

    private fun adSplashHot(place: String) {
        // 加载广告
        LolAdWrapper.shared.loadAdIfNotCached(
            this,
            place,
            object : LolAdWrapper.LoLLoadListener {
                override fun loadFailed(error: LolLoadError?) {
                    // 处理加载失败
                    finish()
                }

                override fun loaded() {
                    // 处理加载成功
                    // 如果需要立刻展示广告，可以调用展示方法
                    LolAdWrapper.shared.showAdIfCached(
                        this@WelActivity,
                        place,
                        object : LolAdWrapper.LolShowListener {
                            override fun shown() {
                                // 处理广告展示成功
                            }

                            override fun showFailed(error: LolShowError?) {
                                // 处理广告展示失败
                                finish()
                            }

                            override fun closed() {
                                // 处理广告关闭
                                finish()
                            }
                        })
                }
            })

    }
}