package com.adealink.weparty.im.session.dialog

import android.widget.CompoundButton
import androidx.fragment.app.activityViewModels
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.im.R
import com.adealink.weparty.im.databinding.DialogSessionSettingBinding
import com.adealink.weparty.im.session.viewmodel.SessionSettingViewModel
import com.adealink.weparty.im.viewmodel.IMViewModelFactory
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.setting.Setting
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo
import com.adealink.weparty.R as APP_R

class SessionSettingDialog : BottomDialogFragment(R.layout.dialog_session_setting) {

    private val binding by viewBinding(DialogSessionSettingBinding::bind)

    private val sessionSettingViewModel by activityViewModels<SessionSettingViewModel> { IMViewModelFactory() }

    private val profileViewModel by fastLazy { ProfileModule.getProfileViewModel(requireActivity()) }
    private val blackViewModel by fastLazy { ProfileModule.getBlackViewModel(requireActivity()) }

    private var chatInfo: ChatInfo? = null
    fun setChatInfo(chatInfo: ChatInfo?): SessionSettingDialog {
        this.chatInfo = chatInfo
        return this
    }

    private val uid: String?
        get() {
            return chatInfo?.id
        }


    override fun initViews() {
        super.initViews()
        binding.vMuteSwitch.setOnCheckedChangeListener(object :
            CompoundButton.OnCheckedChangeListener {
            override fun onCheckedChanged(
                buttonView: CompoundButton?,
                isChecked: Boolean
            ) {
                sessionSettingViewModel.setSessionMute(isChecked)
            }
        })
        binding.clMark.onClick {
            showEditUserNoteDialog()
        }
        binding.clReport.onClick {
            goReport()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        sessionSettingViewModel.isSessionMuteLD.observe(viewLifecycleOwner) {
            updateMute(it)
        }
        blackViewModel?.isBlackLD?.observe(viewLifecycleOwner) {
            val uid = this@SessionSettingDialog.uid ?: return@observe
            val isBlack = it[uid] ?: false
            updateBlack(isBlack)
        }
    }

    override fun loadData() {
        super.loadData()
        uid?.let {
            blackViewModel?.isBlack(it)
        }
    }

    private fun updateMute(mute: Boolean) {
        binding.vMuteSwitch.isChecked = mute
    }

    private fun updateBlack(isBlack: Boolean) {
        if (isBlack) {
            binding.tvBlack.text = getCompatString(APP_R.string.common_remove_black)
            binding.clBlack.onClick {
                uid?.let { uid ->
                    blackViewModel?.unBlack(uid)
                }
            }
        } else {
            binding.tvBlack.text = getCompatString(APP_R.string.common_add_black)
            binding.clBlack.onClick {
                uid?.let { uid ->
                    blackViewModel?.black(uid)
                }
            }
        }
    }

    private fun goReport() {
        val act = activity ?: return
        Router.build(act, Setting.Report.PATH)
            .putExtra(Setting.Report.EXTRA_UID, uid)
            .start()
    }

    private fun showEditUserNoteDialog() {
        val userName = profileViewModel?.userInfoLD?.value
        val note = profileViewModel?.userNoteLD?.value
        EditNoteDialog().apply {
            setUserHint(userName?.nickName)
            setUserNote(note)
        }.show(childFragmentManager, "EditNoteDialog")
    }
}