package com.adealink.weparty.setting.language

import android.content.Intent
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.locale.language.data.Language
import com.adealink.frame.locale.language.languageManager
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.App
import com.adealink.weparty.AppModule
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.setting.Setting
import com.adealink.weparty.setting.R
import com.adealink.weparty.setting.databinding.DialogLanguageSettingBinding
import com.adealink.weparty.setting.language.adapter.LanguageItemViewBinder
import com.adealink.weparty.setting.language.data.LanguageItemData
import com.adealink.weparty.setting.viewmodel.SettingViewModel
import com.adealink.weparty.setting.viewmodel.SettingViewModelFactory

@RouterUri(path = [Setting.Language.PATH], desc = "语言设置弹窗")
class LanguageSettingDialog : BottomDialogFragment(R.layout.dialog_language_setting),
    LanguageItemViewBinder.OnLanguageChooseListener {

    private val binding by viewBinding(DialogLanguageSettingBinding::bind)
    private val listAdapter by fastLazy { MultiTypeListAdapter<LanguageItemData>() }

    private val settingViewModel by activityViewModels<SettingViewModel> { SettingViewModelFactory() }
    private var currentLanguage: Language? = null

    override fun initViews() {
        super.initViews()
        binding.rvLanguages.adapter = listAdapter
        listAdapter.register(LanguageItemViewBinder(this))
        binding.rvLanguages.layoutManager =
            LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        initChoiceList()
    }

    private fun initChoiceList() {
        currentLanguage = App.instance.languageManager.getLanguage()
        val languageList = mutableListOf<LanguageItemData>()
        val supportLanguageList = App.instance.languageManager.getSupportLanguageList()
        supportLanguageList.forEachIndexed { index, language ->
            languageList.add(
                LanguageItemData(
                    language,
                    index < supportLanguageList.size - 1,
                    currentLanguage == language
                )
            )
        }
        listAdapter.submitList(languageList)
    }

    override fun onChoose(language: Language) {
        if (currentLanguage == language) {
            //当前语言没有发生变化
            dismiss()
            return
        }
        currentLanguage = language
        languageManager?.updateLanguage(language)
        settingViewModel.reportLanguage(language)
        startNewMain()
    }

    private fun startNewMain() {
        val act = activity ?: return
        Router.build(act, AppModule.Main.PATH)
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            .start()
    }
}