package melody.offline.music.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import melody.offline.music.R
import melody.offline.music.activity.PlayDetailsActivity
import melody.offline.music.bean.Audio
import melody.offline.music.databinding.ParentsVoiceLayoutBinding
import melody.offline.music.util.containsContent
import melody.offline.music.util.convertMillisToMinutesAndSecondsString
import melody.offline.music.util.getAudioDurationFromAssets

class ParentsVoiceAdapter(
    private val context: Context,
    private val pdfList: List<Audio>,
) :
    RecyclerView.Adapter<ParentsVoiceAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ParentsVoiceLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val audio = pdfList[position]
        holder.bind(audio)
        holder.itemView.setOnClickListener {
            val intent = Intent(context, PlayDetailsActivity::class.java);
            intent.putExtra(PlayDetailsActivity.KEY_DETAILS_AUDIO, audio)
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int = pdfList.size

    inner class ViewHolder(private val binding: ParentsVoiceLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(audio: Audio) {
            binding.apply {
                image.setImageResource(R.mipmap.app_logo)
                name.text = audio.name
                name.requestFocus()
                if (containsContent(audio.file)) {
                    desc.text = convertMillisToMinutesAndSecondsString(audio.duration)
                } else {
                    val s = getAudioDurationFromAssets(context, audio.file)
                    desc.text = convertMillisToMinutesAndSecondsString(s)
                }

                if (melody.offline.music.App.currentPlayingAudio != null) {
                    if (melody.offline.music.App.currentPlayingAudio?.file == audio.file) {
                        playingLayout.visibility = View.VISIBLE
                        name.setTextColor(context.getColor(R.color.green))
                        desc.setTextColor(context.getColor(R.color.green))
                    } else {
                        playingLayout.visibility = View.GONE
                        name.setTextColor(context.getColor(R.color.white))
                        desc.setTextColor(context.getColor(R.color.white))
                    }
                }
            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        itemClickListener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }
}