package com.base.scanqr.ui.language

import android.content.Intent
import androidx.activity.addCallback
import androidx.core.view.updatePadding
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.main.MainActivity
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
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finishToMainTop()
        }
        binding.flQueren.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) {
                startActivity(Intent(this, MainActivity::class.java).apply {
//                    putExtra("isGuide", true)
                })
            }
            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),
        )
        languageBeanList.findLast { it.language == appLanguageSp && it.country == appLanguageCountrySp }?.isSelect = true

        adapter?.submitList(languageBeanList)
    }

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