package com.base.scanqr.ui.language

import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.scanqr.R
import com.base.scanqr.ads.AdsMgr
import com.base.scanqr.ads.AdsShowCallBack
import com.base.scanqr.base.BaseActivity
import com.base.scanqr.bean.ConstObject.appLanguageCountrySp
import com.base.scanqr.bean.ConstObject.appLanguageSp
import com.base.scanqr.bean.LanguageBean
import com.base.scanqr.databinding.ActivityLanguageBinding
import com.base.scanqr.ui.guide.GuideActivity
import com.base.scanqr.utils.BarUtils
import com.base.scanqr.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
        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))
                finish()
            } else {
                finish()
            }
        }
        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

            if (isGuide) {
//                AdsMgr.showInsert(this, false, object : AdsShowCallBack() {
//                    override fun close(where: Int) {
//                        startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
//                        finish()
//                    }
//
//                    override fun failed(where: Int) {
//                        startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
//                        finish()
//                    }
//
//                    override fun googleFailed(where: Int) {
//                        startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
//                        finish()
//                    }
//                })
                startActivity(Intent(this@LanguageActivity, GuideActivity::class.java))
                finish()
            } else {
                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)
        }
    }
}