package com.base.locationsharewhite.ui.set

import android.content.Intent
import android.graphics.Color
import androidx.core.view.updatePadding
import com.base.locationsharewhite.bean.ConstObject
import com.base.locationsharewhite.bean.ConstObject.appLanguageSp
import com.base.locationsharewhite.bean.LanguageBean
import com.base.locationsharewhite.databinding.ActivityLanguageBinding
import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.ui.howuse.HowUseActivity
import com.base.locationsharewhite.utils.BarUtils
import java.util.Locale

class LanguageActivity : BaseActivity<ActivityLanguageBinding>() {
    private val TAG = "LanguageActivity"

    private lateinit var adapter: LanguageAdapter

    override val binding: ActivityLanguageBinding by lazy {
        ActivityLanguageBinding.inflate(layoutInflater)
    }
    var isGuide: Boolean = false
    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
        isGuide = intent.extras?.getBoolean("isGuide") ?: false

        adapter = LanguageAdapter(click = {

        })
        binding.rv.adapter = adapter
        initData()
    }

    private fun initData() {
        val languageBeanList = listOf(
            LanguageBean(LanguageBean.English, Locale.ENGLISH.language, Locale.ENGLISH.country),
            LanguageBean(LanguageBean.简体中文, Locale.CHINA.language, Locale.CHINA.country),
            LanguageBean(LanguageBean.português, Locale("pt", "BR").language, Locale("pt", "BR").country),
            LanguageBean(LanguageBean.Español, Locale("es").language, Locale("es").country),
            LanguageBean(LanguageBean.Français, Locale.FRENCH.language, Locale.FRENCH.country),
            LanguageBean(LanguageBean.Deutsch, Locale.GERMAN.language, Locale.GERMAN.country),
            LanguageBean(LanguageBean.ไทย, Locale("th", "TH").language, Locale("th", "TH").country),
            LanguageBean(LanguageBean.Bahasa_Indonesia, Locale("in", "ID").language, Locale("in", "ID").country),
            LanguageBean(LanguageBean.Melayu, Locale("ms", "MY").language, Locale("ms", "MY").country),
            LanguageBean(LanguageBean.Tiếng_Việt, Locale("vi", "VN").language, Locale("vi", "VN").country),
            LanguageBean(LanguageBean.日本語, Locale.JAPAN.language, Locale.JAPAN.country),
            LanguageBean(LanguageBean.Italiano, Locale.ITALY.language, Locale.ITALY.country),
            LanguageBean(LanguageBean.한국인, Locale.KOREAN.language, Locale.KOREAN.country),
            LanguageBean(LanguageBean.Nederlands, Locale("nl", "NL").language, Locale("nl", "NL").country),
            LanguageBean(LanguageBean.繁體中文, Locale.TRADITIONAL_CHINESE.language, Locale.TRADITIONAL_CHINESE.country),
            LanguageBean(LanguageBean.Türk, Locale("tr", "TR").language, Locale("tr", "TR").country),
        )
        val spLC = appLanguageSp.split("_")
        if (spLC.size == 2) {
            //匹配语言和国家
            languageBeanList.findLast { it.language == spLC[0] && it.country == spLC[1] }?.isSelect = true
        } else {
            //匹配语言
            languageBeanList.findLast { it.language == spLC[0] }?.isSelect = true
        }

        adapter.submitList(languageBeanList)
    }


    override fun initListener() {
        super.initListener()
        binding.tvNext.setOnClickListener {
            val selectLanguageBean = adapter.items.findLast { it.isSelect }
            selectLanguageBean ?: return@setOnClickListener

            ConstObject.changeLanguageSp = selectLanguageBean.language + "_" + selectLanguageBean.country

            if (isGuide) {
                startActivity(Intent(this, HowUseActivity::class.java))
            }
            finish()
        }
    }

    override fun onResume() {
        super.onResume()

        val isRecreate = updateAppLanguage(MyApplication.setLanguage, TAG) {
            MyApplication.setLanguage = it
        }
        if (isRecreate) {
            return
        }
    }

}