package melody.offline.music.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import melody.offline.music.activity.MoListDetailsActivity
import melody.offline.music.activity.MoPlayDetailsActivity
import melody.offline.music.activity.MoSingerDetailsActivity
import melody.offline.music.databinding.SearchResultOtherItemBinding
import melody.offline.music.innertube.Innertube
import melody.offline.music.util.LogTag

class SearchResultOtherAdapter(
    private val context: Context,
    private val list: List<Innertube.SearchDataPage.SearchResult>,
) :
    RecyclerView.Adapter<SearchResultOtherAdapter.ViewHolder>() {

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position]
        holder.bind(bean)

        holder.itemView.setOnClickListener {
            if (itemClickListener != null) {
                itemClickListener?.onItemClick(position)
            }
            LogTag.LogD(
                LogTag.VO_ACT_LOG,
                "SearchResultOtherAdapter bean.pageType->${bean.pageType}"
            )
            if (!bean.videoId.isNullOrEmpty()) {
                val intent = Intent(context, MoPlayDetailsActivity::class.java)
                intent.putExtra(
                    MoPlayDetailsActivity.PLAY_DETAILS_VIDEO_ID,
                    bean.videoId
                )
                intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, bean.title)
                intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, bean.desc)
                context.startActivity(intent)
            } else if (!bean.browseId.isNullOrEmpty()) {
                when (bean.pageType) {
                    "MUSIC_PAGE_TYPE_ARTIST" -> {
                        val intent = Intent(context, MoSingerDetailsActivity::class.java)
                        intent.putExtra(
                            MoSingerDetailsActivity.SINGER_DETAILS_PAGE_BROWSE_ID,
                            bean.browseId
                        )
                        context.startActivity(intent)
                    }

                    else -> {
                        val intent = Intent(context, MoListDetailsActivity::class.java)
                        intent.putExtra(
                            MoListDetailsActivity.PLAY_LIST_PAGE_BROWSE_ID,
                            bean.browseId
                        )
                        context.startActivity(intent)
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int = list.size

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

        @SuppressLint("SetTextI18n")
        fun bind(bean: Innertube.SearchDataPage.SearchResult) {

            binding.apply {
                Glide.with(context)
                    .load(bean.thumbnail)
                    .into(image)
                title.text = bean.title
                name.text = bean.desc
            }
        }
    }

    private var itemClickListener: OnItemClickListener? = null

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

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