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

import android.view.View
import androidx.annotation.Keep
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import coil.load
import coil.transform.CircleCropTransformation
import com.google.gson.annotations.SerializedName
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
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.extensions.dpToPx
import kr.co.vividnext.sodalive.live.room.info.LiveRoomMemberRole

enum class LiveRoomProfileItemType {
    @SerializedName("MASTER")
    MASTER,

    @SerializedName("MANAGER")
    MANAGER,

    @SerializedName("SPEAKER_TITLE")
    SPEAKER_TITLE,

    @SerializedName("LISTENER_TITLE")
    LISTENER_TITLE,

    @SerializedName("MANAGER_TITLE")
    MANAGER_TITLE,

    @SerializedName("USER")
    USER
}

abstract class LiveRoomProfileItem {
    open var id: Long = 0L
    open var type: LiveRoomProfileItemType = LiveRoomProfileItemType.USER
    open var isStaff: () -> Boolean = { false }
    open var managerId = 0L
    open var onClickInviteSpeaker: (Long) -> Unit = {}
    open var onClickChangeListener: (Long) -> Unit = {}
    open var onClickNoChatting: (Long, String, String) -> Unit = { _, _, _ -> }
    open var kickOut: (Long) -> Unit = {}
    open var onClickProfile: (Long) -> Unit = {}
    abstract fun bind(binding: ViewBinding)
}

@Keep
data class LiveRoomProfileItemSpeakerTitle(
    val title: String,
    val speakerCount: Int,
    val totalUserCount: Int
) : LiveRoomProfileItem() {
    override var type = LiveRoomProfileItemType.SPEAKER_TITLE
    override fun bind(binding: ViewBinding) {
        val itemBinding = binding as ItemLiveRoomProfileHeaderBinding
        itemBinding.tvTitle.text = title
        itemBinding.tvSpeakerCount.text = "$speakerCount"
        itemBinding.tvSpeakerTotalCount.text = if (totalUserCount > 5) {
            "/5"
        } else {
            "/${totalUserCount - 1}"
        }
        itemBinding.tvSpeakerCount.visibility = View.VISIBLE
        itemBinding.tvSpeakerTotalCount.visibility = View.VISIBLE

        val lp = itemBinding.llRoot.layoutParams as RecyclerView.LayoutParams
        lp.topMargin = 14f.dpToPx().toInt()
        lp.bottomMargin = 14f.dpToPx().toInt()
        itemBinding.llRoot.layoutParams = lp
    }
}

@Keep
data class LiveRoomProfileItemListenerTitle(
    val title: String
) : LiveRoomProfileItem() {
    override var type = LiveRoomProfileItemType.LISTENER_TITLE
    override fun bind(binding: ViewBinding) {
        val itemBinding = binding as ItemLiveRoomProfileHeaderBinding
        itemBinding.tvTitle.text = title
        itemBinding.tvSpeakerCount.visibility = View.GONE
        itemBinding.tvSpeakerTotalCount.visibility = View.GONE

        val lp = itemBinding.llRoot.layoutParams as RecyclerView.LayoutParams
        lp.topMargin = 20f.dpToPx().toInt()
        lp.bottomMargin = 14f.dpToPx().toInt()
        itemBinding.llRoot.layoutParams = lp
    }
}

@Keep
data class LiveRoomProfileItemManagerTitle(
    val title: String,
    val managerCount: Int
) : LiveRoomProfileItem() {
    override var type = LiveRoomProfileItemType.MANAGER_TITLE
    override fun bind(binding: ViewBinding) {
        val itemBinding = binding as ItemLiveRoomProfileHeaderBinding
        itemBinding.tvTitle.text = title
        itemBinding.tvSpeakerCount.text = "$managerCount"
        itemBinding.tvSpeakerCount.visibility = View.VISIBLE
        itemBinding.tvSpeakerTotalCount.visibility = View.GONE

        val lp = itemBinding.llRoot.layoutParams as RecyclerView.LayoutParams
        lp.topMargin = 14f.dpToPx().toInt()
        lp.bottomMargin = 14f.dpToPx().toInt()
        itemBinding.llRoot.layoutParams = lp
    }
}

@Keep
data class LiveRoomProfileItemMaster(
    val nickname: String,
    val profileUrl: String
) : LiveRoomProfileItem() {
    override var type = LiveRoomProfileItemType.MASTER
    override fun bind(binding: ViewBinding) {
        val itemBinding = binding as ItemLiveRoomProfileMasterBinding
        itemBinding.tvNickname.text = nickname
        itemBinding.ivProfile.load(profileUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(CircleCropTransformation())
        }
        if (id != SharedPreferenceManager.userId) {
            itemBinding.ivProfile.setOnClickListener { onClickProfile(id) }
        }
    }
}

@Keep
data class LiveRoomProfileItemManager(
    val nickname: String,
    val profileUrl: String
) : LiveRoomProfileItem() {
    override var type = LiveRoomProfileItemType.MANAGER
    override fun bind(binding: ViewBinding) {
        val itemBinding = binding as ItemLiveRoomProfileManagerBinding
        itemBinding.tvNickname.text = nickname
        itemBinding.ivProfile.load(profileUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(CircleCropTransformation())
        }
        if (id != SharedPreferenceManager.userId) {
            itemBinding.ivProfile.setOnClickListener {
                onClickProfile(id)
            }
        }
    }
}

@Keep
data class LiveRoomProfileItemUser(
    val nickname: String,
    val profileUrl: String,
    val role: LiveRoomMemberRole
) : LiveRoomProfileItem() {
    override var type = LiveRoomProfileItemType.USER
    override fun bind(binding: ViewBinding) {
        val itemBinding = binding as ItemLiveRoomListProfileBinding
        itemBinding.tvNickname.text = nickname
        itemBinding.ivProfile.load(profileUrl) {
            crossfade(true)
            placeholder(R.drawable.ic_place_holder)
            transformations(CircleCropTransformation())
        }

        if (id != SharedPreferenceManager.userId) {
            itemBinding.ivProfile.setOnClickListener { onClickProfile(id) }
        }

        if (id == SharedPreferenceManager.userId && role == LiveRoomMemberRole.SPEAKER) {
            itemBinding.llControlButtons.visibility = View.VISIBLE
            itemBinding.tvChangeAudience.visibility = View.VISIBLE
            itemBinding.tvInviteSpeaker.visibility = View.GONE
            itemBinding.ivKickOut.visibility = View.GONE

            itemBinding.tvChangeAudience.setOnClickListener { onClickChangeListener(id) }
            itemBinding.tvInviteSpeaker.setOnClickListener { }
        } else if (managerId == SharedPreferenceManager.userId || isStaff()) {
            itemBinding.llControlButtons.visibility = View.VISIBLE
            itemBinding.ivKickOut.visibility = View.VISIBLE

            if (role == LiveRoomMemberRole.SPEAKER) {
                itemBinding.tvChangeAudience.visibility = View.VISIBLE
                itemBinding.tvInviteSpeaker.visibility = View.GONE

                itemBinding.tvChangeAudience.setOnClickListener { onClickChangeListener(id) }
                itemBinding.tvInviteSpeaker.setOnClickListener { }
            } else {
                itemBinding.tvChangeAudience.visibility = View.GONE
                itemBinding.tvInviteSpeaker.visibility = View.VISIBLE

                itemBinding.tvChangeAudience.setOnClickListener { }
                itemBinding.tvInviteSpeaker.setOnClickListener { onClickInviteSpeaker(id) }
            }

            itemBinding.ivKickOut.setOnClickListener { kickOut(id) }
        } else {
            itemBinding.llControlButtons.visibility = View.GONE
        }

        if (managerId == SharedPreferenceManager.userId) {
            itemBinding.tvNoChatting.visibility = View.VISIBLE
            itemBinding.tvNoChatting.setOnClickListener {
                onClickNoChatting(
                    id,
                    nickname,
                    profileUrl
                )
            }
        } else {
            itemBinding.tvNoChatting.visibility = View.GONE
        }
    }
}
