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

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomListProfileBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomProfileHeaderBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomProfileManagerBinding
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomProfileMasterBinding
import kr.co.vividnext.sodalive.live.room.info.LiveRoomMember

class LiveRoomProfileAdapter(
    private val isStaff: () -> Boolean,
    private val onClickInviteSpeaker: (Long) -> Unit,
    private val onClickChangeListener: (Long) -> Unit,
    private val onClickNoChatting: (Long, String, String) -> Unit,
    private val kickOut: (Long) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : RecyclerView.Adapter<LiveRoomProfileViewHolder>() {

    private val items = mutableListOf<LiveRoomProfileItem>()
    var managerId: Long = -1
    var totalUserCount: Int = 0

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(
        speakers: List<LiveRoomMember>,
        listeners: List<LiveRoomMember>,
        managers: List<LiveRoomMember>
    ) {
        val items = mutableListOf<LiveRoomProfileItem>()
        speakers.forEach {
            if (it.id == managerId) {
                val item = LiveRoomProfileItemMaster(
                    nickname = it.nickname,
                    profileUrl = it.profileImage
                )
                item.id = it.id

                items.add(0, item)
            } else {
                val item = LiveRoomProfileItemUser(
                    nickname = it.nickname,
                    profileUrl = it.profileImage,
                    role = it.role
                )

                item.id = it.id
                items.add(item)
            }
        }

        items.add(
            1,
            LiveRoomProfileItemSpeakerTitle(
                "스피커",
                speakerCount = speakers.size - 1,
                totalUserCount = totalUserCount
            )
        )

        items.add(
            1,
            LiveRoomProfileItemManagerTitle(
                "스탭",
                managerCount = managers.size
            )
        )

        managers.forEachIndexed { index, manager ->
            val item = LiveRoomProfileItemManager(
                nickname = manager.nickname,
                profileUrl = manager.profileImage
            )

            item.id = manager.id
            items.add(index = index + 2, item)
        }

        items.add(LiveRoomProfileItemListenerTitle("리스너"))

        listeners.forEach {
            val item = LiveRoomProfileItemUser(
                nickname = it.nickname,
                profileUrl = it.profileImage,
                role = it.role
            )

            item.id = it.id
            items.add(item)
        }

        this.items.run {
            clear()
            addAll(items)
            notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiveRoomProfileViewHolder {
        when (viewType) {
            LiveRoomProfileItemType.SPEAKER_TITLE.ordinal -> {
                return LiveRoomProfileSpeakerTitleViewHolder(
                    ItemLiveRoomProfileHeaderBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            LiveRoomProfileItemType.MANAGER_TITLE.ordinal -> {
                return LiveRoomProfileManagerTitleViewHolder(
                    ItemLiveRoomProfileHeaderBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            LiveRoomProfileItemType.LISTENER_TITLE.ordinal -> {
                return LiveRoomProfileListenerTitleViewHolder(
                    ItemLiveRoomProfileHeaderBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    )
                )
            }

            LiveRoomProfileItemType.MASTER.ordinal -> {
                return LiveRoomProfileMasterViewHolder(
                    ItemLiveRoomProfileMasterBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    ),
                    managerId,
                    onClickProfile = onClickProfile
                )
            }

            LiveRoomProfileItemType.MANAGER.ordinal -> {
                return LiveRoomProfileManagerViewHolder(
                    ItemLiveRoomProfileManagerBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    ),
                    onClickProfile = onClickProfile
                )
            }

            else -> {
                return LiveRoomProfileUserViewHolder(
                    ItemLiveRoomListProfileBinding.inflate(
                        LayoutInflater.from(parent.context),
                        parent,
                        false
                    ),
                    isStaff = isStaff,
                    managerId = managerId,
                    onClickInviteSpeaker = onClickInviteSpeaker,
                    onClickChangeListener = onClickChangeListener,
                    onClickNoChatting = onClickNoChatting,
                    kickOut = kickOut,
                    onClickProfile = onClickProfile
                )
            }
        }
    }

    override fun onBindViewHolder(holder: LiveRoomProfileViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemViewType(position: Int): Int {
        return items[position].type.ordinal
    }

    override fun getItemCount(): Int = items.size
}

abstract class LiveRoomProfileViewHolder(binding: ViewBinding) :
    RecyclerView.ViewHolder(binding.root) {
    abstract fun bind(item: LiveRoomProfileItem)
}

class LiveRoomProfileSpeakerTitleViewHolder(
    private val binding: ItemLiveRoomProfileHeaderBinding,
) : LiveRoomProfileViewHolder(binding) {
    override fun bind(item: LiveRoomProfileItem) = item.bind(binding)
}

class LiveRoomProfileListenerTitleViewHolder(
    private val binding: ItemLiveRoomProfileHeaderBinding,
) : LiveRoomProfileViewHolder(binding) {
    override fun bind(item: LiveRoomProfileItem) = item.bind(binding)
}

class LiveRoomProfileManagerTitleViewHolder(
    private val binding: ItemLiveRoomProfileHeaderBinding,
) : LiveRoomProfileViewHolder(binding) {
    override fun bind(item: LiveRoomProfileItem) = item.bind(binding)
}

class LiveRoomProfileMasterViewHolder(
    private val binding: ItemLiveRoomProfileMasterBinding,
    private val managerId: Long,
    private val onClickProfile: (Long) -> Unit
) : LiveRoomProfileViewHolder(binding) {
    override fun bind(item: LiveRoomProfileItem) {
        item.onClickProfile = onClickProfile
        item.managerId = managerId
        item.bind(binding)
    }
}

class LiveRoomProfileManagerViewHolder(
    private val binding: ItemLiveRoomProfileManagerBinding,
    private val onClickProfile: (Long) -> Unit
) : LiveRoomProfileViewHolder(binding) {
    override fun bind(item: LiveRoomProfileItem) {
        item.onClickProfile = onClickProfile
        item.bind(binding)
    }
}

class LiveRoomProfileUserViewHolder(
    private val binding: ItemLiveRoomListProfileBinding,
    private val isStaff: () -> Boolean,
    private val managerId: Long,
    private val onClickInviteSpeaker: (Long) -> Unit,
    private val onClickChangeListener: (Long) -> Unit,
    private val onClickNoChatting: (Long, String, String) -> Unit,
    private val kickOut: (Long) -> Unit,
    private val onClickProfile: (Long) -> Unit
) : LiveRoomProfileViewHolder(binding) {
    override fun bind(item: LiveRoomProfileItem) {
        item.isStaff = isStaff
        item.managerId = managerId
        item.onClickInviteSpeaker = onClickInviteSpeaker
        item.onClickChangeListener = onClickChangeListener
        item.onClickNoChatting = onClickNoChatting
        item.kickOut = kickOut
        item.onClickProfile = onClickProfile
        item.bind(binding)
    }
}
