package kr.co.vividnext.sodalive.message.voice

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ItemVoiceMessageBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.message.GetVoiceMessageResponse
import kr.co.vividnext.sodalive.message.MessageBox

class VoiceMessageAdapter(
    private val onPlay: (GetVoiceMessageResponse.VoiceMessageItem) -> Int,
    private val onStop: () -> Unit,
    private val onStartSeekbar: (SeekBar, TextView) -> Unit,
    private val onItemDelete: (Long) -> Unit,
    private val onItemKeep: (Long, Boolean) -> Unit,
    private val onItemReply: (Long, String, String) -> Unit
) : RecyclerView.Adapter<VoiceMessageAdapter.ViewHolder>() {

    private var openPlayerItemPosition = -1
    private var isPlaying = false

    private var messageBox = MessageBox.RECEIVE

    fun setMessageBox(messageBox: MessageBox) {
        this.messageBox = messageBox
    }

    inner class ViewHolder(
        private val binding: ItemVoiceMessageBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("NotifyDataSetChanged")
        fun bind(item: GetVoiceMessageResponse.VoiceMessageItem, position: Int) {
            if (openPlayerItemPosition == position) {
                binding.llPlayer.visibility = View.VISIBLE
                if (isPlaying) {
                    onStartSeekbar(binding.seekbar, binding.tvTotalDuration)
                }
            } else {
                binding.llPlayer.visibility = View.GONE
            }

            if (isPlaying) {
                binding.ivPlayOrStop.setImageResource(R.drawable.btn_bar_stop)
            } else {
                binding.ivPlayOrStop.setImageResource(R.drawable.btn_bar_play)
            }

            if (SharedPreferenceManager.nickname == item.recipientNickname) {
                binding.ivProfile.load(item.senderProfileImageUrl) {
                    crossfade(true)
                    placeholder(R.drawable.ic_place_holder)
                    transformations(RoundedCornersTransformation(23.4f.dpToPx()))
                }

                binding.tvNickname.text = item.senderNickname
            } else {
                binding.ivProfile.load(item.recipientProfileImageUrl) {
                    crossfade(true)
                    placeholder(R.drawable.ic_place_holder)
                    transformations(RoundedCornersTransformation(23.4f.dpToPx()))
                }

                binding.tvNickname.text = item.recipientNickname
            }

            binding.tvDate.text = item.date
            binding.root.setOnClickListener {
                if (isPlaying) {
                    onStop()
                    isPlaying = false
                }

                openPlayerItemPosition = if (
                    openPlayerItemPosition == position &&
                    binding.llPlayer.visibility == View.VISIBLE
                ) {
                    -1
                } else {
                    position
                }
                notifyDataSetChanged()
            }

            binding.llPlayer.setOnClickListener {}
            binding.seekbar.isEnabled = false

            binding.ivPlayOrStop.setOnClickListener {
                if (isPlaying) {
                    isPlaying = false
                    onStop()
                    binding.ivPlayOrStop.setImageResource(R.drawable.btn_bar_play)
                } else {
                    val totalDuration = onPlay(item).toLong()
                    if (totalDuration > 0) {
                        isPlaying = true
                        onStartSeekbar(binding.seekbar, binding.tvTotalDuration)
                        binding.ivPlayOrStop.setImageResource(R.drawable.btn_bar_stop)
                    }
                }
            }

            if (messageBox == MessageBox.RECEIVE) {
                binding.ivKeep.visibility = View.VISIBLE
                binding.ivReply.visibility = View.VISIBLE
                binding.ivDelete.visibility = View.GONE

                binding.ivKeep.setOnClickListener { onItemKeep(item.messageId, item.isKept) }
                binding.ivReply.setOnClickListener {
                    onItemReply(
                        item.senderId,
                        item.senderNickname,
                        item.senderProfileImageUrl
                    )
                }
            } else {
                binding.ivKeep.visibility = View.GONE
                binding.ivReply.visibility = View.GONE
                binding.ivDelete.visibility = View.VISIBLE

                binding.ivDelete.setOnClickListener { onItemDelete(item.messageId) }
            }
        }
    }

    val items = mutableListOf<GetVoiceMessageResponse.VoiceMessageItem>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        ItemVoiceMessageBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

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

    override fun getItemCount() = items.size

    @SuppressLint("NotifyDataSetChanged")
    fun voicePlayComplete() {
        isPlaying = false
        notifyDataSetChanged()
    }
}
