package com.base.pdfviewerscannerwhite.ui.set

import android.content.Intent
import androidx.activity.addCallback
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.ads.admob.AdmobNativeUtils
import com.base.pdfviewerscannerwhite.bean.ConstObject
import com.base.pdfviewerscannerwhite.bean.ConstObject.appLanguageSp
import com.base.pdfviewerscannerwhite.bean.ConstObject.changeLanguageSp
import com.base.pdfviewerscannerwhite.bean.ConstObject.haveSelectLanguage
import com.base.pdfviewerscannerwhite.bean.LanguageBean
import com.base.pdfviewerscannerwhite.databinding.ActivitySetLanguageBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.ui.permission.PermissionActivity
import com.base.pdfviewerscannerwhite.utils.LanguageUtils.getSystemLanguageDefault
import java.util.Locale

class SetLanguageActivity : BaseActivity<ActivitySetLanguageBinding>() {

    override val binding: ActivitySetLanguageBinding by lazy {
        ActivitySetLanguageBinding.inflate(layoutInflater)
    }
    private val TAG = "SetLanguageActivity"
    private lateinit var adapter: LanguageAdapter
    private var from: String = ""
    override fun initView() {

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

        AdmobNativeUtils.showNativeAd(this, binding.flAd)
        initData()
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            if (from == "Set") {
                finish()
            } else {
                startActivity(Intent(this@SetLanguageActivity, PermissionActivity::class.java))
                finish()
            }
        }
        binding.flFanhui.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvOk.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), getSystemLanguageDefault()).apply { isFollowSystem = true },
            LanguageBean(LanguageBean.English, Locale.ENGLISH.language),
            LanguageBean(LanguageBean.简体中文, Locale.PRC.language),
            LanguageBean(LanguageBean.português, Locale("pt", "BR").language),
            LanguageBean(LanguageBean.Español, Locale("es").language),
            LanguageBean(LanguageBean.Français, Locale.FRENCH.language),
            LanguageBean(LanguageBean.Deutsch, Locale("de").language),
            LanguageBean(LanguageBean.ไทย, Locale("th", "TH").language),
            LanguageBean(LanguageBean.Bahasa_Indonesia, Locale("in", "ID").language),
            LanguageBean(LanguageBean.Melayu, Locale("ms", "MY").language),
            LanguageBean(LanguageBean.Tiếng_Việt, Locale("vi", "VN").language),
            LanguageBean(LanguageBean.日本語, Locale("ja", "JP").language),
            LanguageBean(LanguageBean.Italiano, Locale("it", "IT").language),
            LanguageBean(LanguageBean.한국인, Locale("ko", "KR").language),
            LanguageBean(LanguageBean.Nederlands, Locale("nl", "NL").language),
            LanguageBean(LanguageBean.繁體中文, Locale("zh", "TW").language),
            LanguageBean(LanguageBean.Türk, Locale("tr", "TR").language),
        )
        if (ConstObject.selectFollowLanguage || !ConstObject.haveSelectLanguage) {
            list.first().isSelect = true
        } else {
            list.findLast { it.language == appLanguageSp }?.isSelect = true
        }
        adapter.submitList(list)

        haveSelectLanguage = true
    }

}