package com.module.backstage.fragment


import android.widget.RadioGroup
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.launcher.ARouter

import com.module.backstage.R
import com.module.backstage.databinding.BackstageFragmentLanguageBinding
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.base.utils.LanguageUtil
import com.module.backstage.vm.LanguageFragmentVM
import com.quyunshuo.androidbaseframemvvm.base.utils.LanguageUtilsObject
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.base.utils.multilanguages.LocaleContract
import com.quyunshuo.androidbaseframemvvm.base.utils.multilanguages.MultiLanguages
import com.quyunshuo.androidbaseframemvvm.common.constant.RouteUrl
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import dagger.hilt.android.AndroidEntryPoint
import java.util.Locale

/**
 * 语言设置Fragment
 *
 * @author Qu Yunshuo
 * @since 2021/8/1 11:46 下午
 */
@AndroidEntryPoint
class LanguageFragment : BaseFragment<BackstageFragmentLanguageBinding, LanguageFragmentVM>(),
    RadioGroup.OnCheckedChangeListener {
    var languageType: Int = 1 //默认选择的
    var languageValue: String = LanguageUtil.CHINESE

    override val mViewModel: LanguageFragmentVM by viewModels()
    override fun createVB() = BackstageFragmentLanguageBinding.inflate(layoutInflater)


    override fun BackstageFragmentLanguageBinding.initView() {
        languageType = SpUtils.getInt(MMKVName.LANGUAGE_TYPE, 0)!!
        mViewModel.setSelectLanguage(mBinding, languageType)

        flLanguage.setOnCheckedChangeListener(this@LanguageFragment)
        btnOk.setOnClickListener {
            if (languageType!=SpUtils.getInt(MMKVName.LANGUAGE_TYPE, 0)!!){
                SpUtils.putInt(MMKVName.LANGUAGE_TYPE, languageType)
                set(languageValue)
                ToastUtil.switchToastStyleToSuccess("更新成功")
                ARouter.getInstance().build(RouteUrl.Main.MainActivity).navigation()
                activity?.finish()
            }


        }

    }

    override fun initObserve() {
    }

    override fun initRequestData() {
        mViewModel.getBanners()
        mViewModel.getArticleData()
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.rb_chinese -> {
                languageType = 0
                languageValue = LanguageUtil.CHINESE
            }

            R.id.rb_english -> {
                languageType = 1
                languageValue = LanguageUtil.ENGLISH
            }

            R.id.rb_german -> {
                languageType = 2
                languageValue = LanguageUtil.GERMAN
            }

            R.id.rb_russia -> {
                languageType = 3
                languageValue = LanguageUtil.RUSSIA
            }

            R.id.rb_japanese -> {
                languageType = 4
                languageValue = LanguageUtil.JAPANESE
            }

            R.id.rb_Spanish -> {
                languageType = 5
                languageValue = LanguageUtil.SPANISH
            }

            R.id.rb_french -> {
                languageType = 6
                languageValue = LanguageUtil.FRENCH
            }

            R.id.rb_bulgaria -> {
                languageType = 7
                languageValue = LanguageUtil.BULGARIA
            }

            R.id.rb_danmai -> {
                languageType = 8
                languageValue = LanguageUtil.DanMai
            }

            R.id.rb_fenlan -> {
                languageType = 9
                languageValue = LanguageUtil.FenLan
            }

            R.id.rb_helan -> {
                languageType = 10
                languageValue = LanguageUtil.HeLan
            }

            R.id.rb_jieke -> {
                languageType = 11
                languageValue = LanguageUtil.JieKe
            }

            R.id.rb_luomaniya -> {
                languageType = 12
                languageValue = LanguageUtil.RouMaNiYa
            }

            R.id.rb_nuowei -> {
                languageType = 13
                languageValue = LanguageUtil.NuoWei
            }

            R.id.rb_putaoya -> {
                languageType = 14
                languageValue = LanguageUtil.PuTaoYa
            }

            R.id.rb_ruishi -> {
                languageType = 15
                languageValue = LanguageUtil.RuiShi
            }

            R.id.rb_sifaluoke -> {
                languageType = 16
                languageValue = LanguageUtil.SiFaLuoKe
            }

            R.id.rb_xiongyali -> {
                languageType = 17
                languageValue = LanguageUtil.XiongYaLi
            }

            R.id.rb_yidali -> {
                languageType = 18
                languageValue = LanguageUtil.YiDaLi
            }

            R.id.rb_taiwang -> {
                languageType = 19
                languageValue = LanguageUtil.TaiWang
            }

            R.id.rb_hany -> {
                languageType = 20
                languageValue = LanguageUtil.hangguo
            }

            R.id.rb_bolan -> {
                languageType = 21
                languageValue = LanguageUtil.bolan
            }

            R.id.rb_wukelan -> {
                languageType = 22
                languageValue = LanguageUtil.wukelan
            }

            R.id.rb_ymny -> {
                languageType = 23
                languageValue = LanguageUtil.ymny
            }

            R.id.rb_alb -> {
                languageType = 24
                languageValue = LanguageUtil.alb
            }

            R.id.rb_tg -> {
                languageType = 25
                languageValue = LanguageUtil.tg
            }

            R.id.rb_ysl -> {
                languageType = 26
                languageValue = LanguageUtil.ysl
            }

            R.id.rb_brazil2 -> {
                languageType = 27
                languageValue = LanguageUtil.brazil
            }

            R.id.rb_gljy -> {
                languageType = 28
                languageValue = LanguageUtil.gljy
            }

            R.id.rb_brazil3 -> {
                languageType = 29
                languageValue = LanguageUtil.brazi2
            }

            R.id.rb_turkey -> {
                languageType = 30
                languageValue = LanguageUtil.turkey
            }

            R.id.rb_latvia -> {
                languageType = 31
                languageValue = LanguageUtil.latvia
            }

            R.id.rb_portugal_ra -> {
                languageType = 32
                languageValue = LanguageUtil.ricardoAmaro
            }

            R.id.rb_eesti -> {
                languageType = 33
                languageValue = LanguageUtil.eesti
            }

            R.id.rb_mgy -> {
                languageType = 34
                languageValue = LanguageUtil.mgy
            }

            R.id.rb_bsy -> {
                languageType = 35
                languageValue = LanguageUtil.bsy
            }

            R.id.rb_slwny -> {
                languageType = 36
                languageValue = LanguageUtil.slwny
            }

            R.id.rb_kldy -> {
                languageType = 37
                languageValue = LanguageUtil.kldy
            }

            R.id.rb_bsny -> {
                languageType = 38
                languageValue = LanguageUtil.bsny
            }

            else -> {
                languageType = 1
                languageValue = LanguageUtil.CHINESE
            }
        }
    }


    /**
     * 设置语言
     *
     * @param lauType
     */
    private fun set(lauType: String) {
        // 本地语言设置
        val myLocale = Locale(lauType)
        val res = resources
        val dm = res.displayMetrics
        val conf = res.configuration
        conf.locale = myLocale
        res.updateConfiguration(conf, dm)
    }

}