package com.base.pdfreaderallpdfreader.ui.language

import android.graphics.Color
import androidx.activity.addCallback
import androidx.core.view.updatePadding
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.base.BaseActivity
import com.base.pdfreaderallpdfreader.bean.ConstObject
import com.base.pdfreaderallpdfreader.bean.ConstObject.appLanguageSp
import com.base.pdfreaderallpdfreader.bean.ConstObject.changeLanguageSp
import com.base.pdfreaderallpdfreader.bean.ConstObject.haveSelectLanguage
import com.base.pdfreaderallpdfreader.bean.LanguageBean
import com.base.pdfreaderallpdfreader.databinding.ActivityLanguageBinding
import com.base.pdfreaderallpdfreader.utils.BarUtils
import java.util.Locale

class LanguageActivity : BaseActivity<ActivityLanguageBinding>() {

    override val binding: ActivityLanguageBinding by lazy {
        ActivityLanguageBinding.inflate(layoutInflater)
    }
    private lateinit var adapter: LanguageAdapter
    private var from: String = ""


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.WHITE)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        from = intent.extras?.getString("from", "") ?: ""
        adapter = LanguageAdapter()
        binding.rv.adapter = adapter

//        com.base.pdfreaderallpdfreader.ads.admob.AdmobNativeUtils.showNativeAd(this, binding.flAd)
        initData()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (from == "Set") {
                finish()
            } else {
//                startActivity(Intent(this@LanguageActivity, PermissionActivity::class.java))
                finish()
            }
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.ivRight.setOnClickListener {
            val item = adapter.items.find { it.isSelect }
            val selectLanguage = item?.language ?: Locale.ENGLISH.language
            if (changeLanguageSp != selectLanguage) {
                changeLanguageSp = selectLanguage
            }
            ConstObject.selectFollowLanguage = item?.isFollowSystem == true

            onBackPressedDispatcher.onBackPressed()
        }
    }

    private fun initData() {
        val list = listOf(
            LanguageBean(
                this.getString(R.string.follow_system),
                Locale.getDefault().language,
                Locale.getDefault().country
            ).apply { isFollowSystem = true },

            LanguageBean(LanguageBean.English, Locale.ENGLISH.language, Locale.ENGLISH.country),
            LanguageBean(LanguageBean.简体中文, Locale.SIMPLIFIED_CHINESE.language, Locale.SIMPLIFIED_CHINESE.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),

            )
        if (ConstObject.selectFollowLanguage || !ConstObject.haveSelectLanguage) {
            list.first().isSelect = true
        } else {
            list.findLast { it.language == appLanguageSp }?.isSelect = true
        }
        adapter.submitList(list)

        haveSelectLanguage = true
    }

}