package com.player.musicoo.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.player.musicoo.R
import com.player.musicoo.activity.MoListDetailsActivity
import com.player.musicoo.activity.MoPlayDetailsActivity
import com.player.musicoo.activity.MoSingerDetailsActivity
import com.player.musicoo.innertube.Innertube

@SuppressLint("ViewConstructor")
class SearchResultOptimalView(context: Context, data: Innertube.SearchDataPage) :
    ModuleView(context) {

    init {
        contentView = inflate(getContext(), R.layout.search_result_optimal_layout, this)
        val title = contentView?.findViewById<TextView>(R.id.title)
        title?.text = data.title

        val name = contentView?.findViewById<TextView>(R.id.name)
        name?.text = data.searchResultList.firstOrNull()?.title

        val desc = contentView?.findViewById<TextView>(R.id.desc)
        desc?.text = data.searchResultList.firstOrNull()?.desc

        val image = contentView?.findViewById<ImageView>(R.id.image)
        val optimalBean = data.searchResultList.firstOrNull()
        val url = optimalBean?.thumbnail
        Glide.with(context)
            .load(url)
            .into(image!!)

        val playBtn = contentView?.findViewById<LinearLayout>(R.id.playBtn)
        playBtn?.setOnClickListener {
            if (optimalBean != null) {
                if (!optimalBean.videoId.isNullOrEmpty()) {
                    val intent = Intent(context, MoPlayDetailsActivity::class.java)
                    intent.putExtra(
                        MoPlayDetailsActivity.PLAY_DETAILS_VIDEO_ID,
                        optimalBean.videoId
                    )
                    intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_NAME, optimalBean.title)
                    intent.putExtra(MoPlayDetailsActivity.PLAY_DETAILS_DESC, optimalBean.desc)
                    context.startActivity(intent)
                } else if (!optimalBean.browseId.isNullOrEmpty()) {
                    when (optimalBean.pageType) {
                        "MUSIC_PAGE_TYPE_ALBUM" -> {
                            val intent = Intent(context, MoListDetailsActivity::class.java)
                            intent.putExtra(
                                MoListDetailsActivity.PLAY_LIST_PAGE_BROWSE_ID,
                                optimalBean.browseId
                            )
                            context.startActivity(intent)
                        }
                        else -> {
                            val intent = Intent(context, MoSingerDetailsActivity::class.java)
                            intent.putExtra(
                                MoSingerDetailsActivity.SINGER_DETAILS_PAGE_BROWSE_ID,
                                optimalBean.browseId
                            )
                            context.startActivity(intent)
                        }
                    }
                }
            }
        }
    }
}