package com.base.appzxhy.ui.language

import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.base.appzxhy.R
import com.base.appzxhy.SpConstObject.appLanguageCountrySp
import com.base.appzxhy.SpConstObject.appLanguageSp
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.bean.LanguageBean
import com.base.appzxhy.business.ads.AdsMgr
import com.base.appzxhy.business.ads.AdsShowCallBack
import com.base.appzxhy.databinding.ActivityLanguageBinding
import com.base.appzxhy.ui.guide.GuideCleanActivity
import com.base.appzxhy.utils.LogEx
import java.util.Locale

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

    private val adapter = LanguageAdapter()

    private val list: List<LanguageBean> by lazy {
        val s = listOf(
            LanguageBean(LanguageBean.English, Locale.ENGLISH.language, Locale.ENGLISH.country),
            LanguageBean(LanguageBean.Français, Locale.FRENCH.language, Locale.FRENCH.country),
            LanguageBean(LanguageBean.Deutsch, Locale.GERMAN.language, Locale.GERMAN.country),
            LanguageBean(LanguageBean.Español, Locale("es", "").language, Locale("es", "").country),
            LanguageBean(LanguageBean.português, Locale("pt", "").language, Locale("pt", "").country),
            LanguageBean(LanguageBean.日本語, Locale.JAPAN.language, Locale.JAPAN.country),
            LanguageBean(LanguageBean.한국인, Locale.KOREAN.language, Locale.KOREAN.country),
            //一般来说不匹配地区,除非是中文
            LanguageBean(LanguageBean.简体中文, Locale.SIMPLIFIED_CHINESE.language, Locale.SIMPLIFIED_CHINESE.country),
            LanguageBean(LanguageBean.繁體中文, Locale.TRADITIONAL_CHINESE.language, Locale.TRADITIONAL_CHINESE.country),
        )

        val findBean = s.findLast { it.language == appLanguageSp && it.country == appLanguageCountrySp }
        if (findBean != null) {
            findBean.isSelect = true
        } else {
            s.first().isSelect = true
        }
        s
    }

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            binding.flTop.setPadding(0, systemBars.top, 0, 0)
            insets
        }
    }

    override fun initView() {
        super.initView()
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_native_custom)
        binding.rv.adapter = adapter
        adapter.submitList(list)
    }

    override fun initListener() {
        super.initListener()

        adapter.click = {}

        binding.llNext.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, showCallBack = object : AdsShowCallBack() {
                override fun next() {
                    goToAc(GuideCleanActivity::class.java)
                    finish()
                }
            })

        }
    }

    override fun handleBackCallBack() {
        binding.llNext.callOnClick()
    }

}