package com.tgv.malware.ui.language

import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.tgv.malware.R
import com.tgv.malware.SpConstObject.appLanguageCountrySp
import com.tgv.malware.SpConstObject.appLanguageSp
import com.tgv.malware.base.BaseActivity
import com.tgv.malware.bean.LanguageBean
import com.tgv.malware.business.ads.AdsMgr
import com.tgv.malware.business.ads.AdsShowCallBack
import com.tgv.malware.databinding.ActivityLanguageBinding
import com.tgv.malware.ui.guide.GuideCleanActivity
import com.tgv.malware.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)
            insetsCallBack?.invoke(systemBars)
            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()
    }

}