package kr.co.vividnext.sodalive.live.room.profile

import android.annotation.SuppressLint
import android.view.LayoutInflater
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import kr.co.vividnext.sodalive.databinding.DialogLiveRoomProfileBinding
import kr.co.vividnext.sodalive.live.room.info.GetRoomInfoResponse

class LiveRoomProfileDialog(
    layoutInflater: LayoutInflater,
    private val activity: FragmentActivity,
    private val roomInfoLiveData: LiveData<GetRoomInfoResponse>,
    isStaff: () -> Boolean,
    onClickInviteSpeaker: (Long) -> Unit,
    onClickChangeListener: (Long) -> Unit,
    onClickKickOut: (Long) -> Unit,
    onClickNoChatting: (Long, String, String) -> Unit,
    onClickProfile: (Long) -> Unit
) {
    private val bottomSheetDialog: BottomSheetDialog = BottomSheetDialog(activity)
    private val dialogView = DialogLiveRoomProfileBinding.inflate(layoutInflater)
    private val adapter = LiveRoomProfileAdapter(
        isStaff = isStaff,
        onClickInviteSpeaker = onClickInviteSpeaker,
        onClickChangeListener = onClickChangeListener,
        onClickNoChatting = onClickNoChatting,
        kickOut = onClickKickOut,
        onClickProfile = onClickProfile
    )

    init {
        bottomSheetDialog.setContentView(dialogView.root)
        bottomSheetDialog.setCancelable(false)

        dialogView.ivClose.setOnClickListener { bottomSheetDialog.dismiss() }
        setupRecyclerView()
        bindData()
    }

    private fun setupRecyclerView() {
        dialogView.rvPeoples.isNestedScrollingEnabled = true
        dialogView.rvPeoples.layoutManager = LinearLayoutManager(
            activity,
            LinearLayoutManager.VERTICAL,
            false
        )

        dialogView.rvPeoples.adapter = adapter
    }

    @SuppressLint("SetTextI18n")
    private fun bindData() {
        roomInfoLiveData.observe(activity) {
            adapter.managerId = it.creatorId
            adapter.totalUserCount = it.totalAvailableParticipantsCount
            dialogView.tvParticipate.text = "${it.participantsCount}"
            dialogView.tvTotalPeoples.text = "/${it.totalAvailableParticipantsCount}"

            adapter.updateList(
                speakers = it.speakerList,
                listeners = it.listenerList,
                managers = it.managerList
            )
        }
    }

    fun show() {
        bottomSheetDialog.show()
    }
}
