package com.base.scanqrclear.ui.language

import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.scanqrclear.R
import com.base.scanqrclear.ads.AdsMgr
import com.base.scanqrclear.ads.AdsShowCallBack
import com.base.scanqrclear.base.BaseActivity
import com.base.scanqrclear.bean.ConstObject.appLanguageCountrySp
import com.base.scanqrclear.bean.ConstObject.appLanguageSp
import com.base.scanqrclear.bean.LanguageBean
import com.base.scanqrclear.databinding.ActivityLanguageBinding
import com.base.scanqrclear.ui.guide.GuideActivity
import com.base.scanqrclear.utils.BarUtils
import com.base.scanqrclear.utils.LogEx
import com.gyf.immersionbar.ktx.immersionBar
import java.util.Locale

class LanguageActivity : BaseActivity<ActivityLanguageBinding>(ActivityLanguageBinding::inflate) {

    private var isGuide: Boolean = false
    private var adapter: LanguageAdapter? = null


    override fun initView() {
        super.initView()
        binding.clTop.updatePadding(top = BarUtils.getStatusBarHeight())
        isGuide = intent.extras?.getBoolean("isGuide") ?: isGuide
        intent.extras?.clear()
        adapter = LanguageAdapter {

        }
        binding.rv.adapter = adapter
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit)

//        if (!isGuide) {
//            AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
//                override fun close(where: Int) {
//
//                }
//
//                override fun failed(where: Int) {
//                }
//
//                override fun googleFailed(where: Int) {
//                }
//            })
//        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (isGuide) {
                startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
                finishToMainTop()
            } else {
                finishToMainTop()
            }
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.flQueren.setOnClickListener {
            val selectLanguageBean = adapter?.items?.findLast { it.isSelect }
            selectLanguageBean ?: return@setOnClickListener
            LogEx.logDebug(TAG, "selectLanguageBean=$selectLanguageBean")
            appLanguageSp = selectLanguageBean.language
            appLanguageCountrySp = selectLanguageBean.country


            AdsMgr.showInsert(this, true, object : AdsShowCallBack() {
                override fun close(where: Int) {
                    if (isGuide) {
                        startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
                    }
                    finishToMainTop()
                }

                override fun failed(where: Int) {
                    if (isGuide) {
                        startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
                    }
                    finishToMainTop()
                }

                override fun googleFailed(where: Int) {
                    if (isGuide) {
                        startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
                    }
                    finishToMainTop()
                }
            })
//                startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
//                finish()
        }
    }

    override fun onResumeOneShoot() {
        super.onResumeOneShoot()
        val flag = changeLanguage()
        if (flag) return

        initData()
    }

    private fun initData() {
        val languageBeanList = listOf(
            LanguageBean(LanguageBean.English, Locale.ENGLISH.language, Locale.ENGLISH.country),
            LanguageBean(LanguageBean.简体中文, Locale.SIMPLIFIED_CHINESE.language, Locale.SIMPLIFIED_CHINESE.country),
//            LanguageBean(LanguageBean.繁體中文, Locale.TRADITIONAL_CHINESE.language, Locale.TRADITIONAL_CHINESE.country),
            LanguageBean(LanguageBean.日本語, Locale.JAPAN.language, Locale.JAPAN.country),
            LanguageBean(LanguageBean.한국인, Locale.KOREAN.language, Locale.KOREAN.country),
            LanguageBean(LanguageBean.Deutsch, Locale.GERMAN.language, Locale.GERMAN.country),
            LanguageBean(
                LanguageBean.Español, Locale("es", "").language,
                Locale("es", "").country
            )
        )
        languageBeanList.findLast { it.language == appLanguageSp && it.country == appLanguageCountrySp }?.isSelect = true

        adapter?.submitList(languageBeanList)
    }

    override fun configSystemBar() {
        immersionBar {
            statusBarColor("#FFFFFFFF")
            statusBarDarkFont(true)
        }
    }
}