package com.player.musicoo.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ktx.immersionBar
import com.player.musicoo.adapter.DetailsListAdapter
import com.player.musicoo.databinding.ActivityDetailsBinding
import com.player.musicoo.fragment.MoHomeFragment
import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.requests.moPlaylistPage
import com.player.musicoo.util.LogTag.LogD
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select

class MoListDetailsActivity : MoBaseActivity() {
    private val requests: Channel<Request> = Channel(Channel.UNLIMITED)

    enum class Request {
        TryAgain,
    }

    companion object {
        const val PLAY_LIST_PAGE_BROWSE_ID = "play_list_page_browse_id"
    }

    private lateinit var binding: ActivityDetailsBinding
    private var browseId: String? = null

    override suspend fun main() {
        binding = ActivityDetailsBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initImmersionBar()
        browseId = intent.getStringExtra(PLAY_LIST_PAGE_BROWSE_ID)
        if (browseId.isNullOrEmpty() || browseId == "null") {
            finish()
            return
        }
        initView()
        LogD(TAG, "browseId->${browseId}")
        initData(browseId!!)
        onReceive()
    }

    private fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(false)
            statusBarView(binding.view)
        }
    }

    private suspend fun onReceive() {
        while (isActive) {
            select<Unit> {
                requests.onReceive {
                    when (it) {
                        Request.TryAgain -> {
                            initData(browseId!!)
                        }
                    }
                }
            }
        }
    }

    private fun initView() {
        binding.backBtn.setOnClickListener {
            finish()
        }
        binding.tryAgainBtn.setOnClickListener {
            requests.trySend(Request.TryAgain)
        }
    }

    private suspend fun initData(browseId: String) {
        showLoadingUi()
        Innertube.moPlaylistPage(browseId)
            ?.onSuccess {
                showDataUi()
                Glide.with(this)
                    .load(it.thumbnail)
                    .into(binding.imageView)

                binding.title.text = it.title
                binding.subtitle.text = it.subtitle
                binding.secondSubtitle.text = it.secondSubtitle

                val adapter = DetailsListAdapter(this, it.moPlaylistOrAlbumListBean)
                binding.rv.layoutManager =
                    LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
                binding.rv.adapter = adapter

            }?.onFailure {
                showNoContentUi()
                LogD(TAG, "moPlaylistPage onFailure->${it}")
            }
    }

    private fun showDataUi() {
        binding.loadingLayout.visibility = View.GONE
        binding.noContentLayout.visibility = View.GONE
    }

    private fun showLoadingUi() {
        binding.loadingLayout.visibility = View.VISIBLE
        binding.noContentLayout.visibility = View.GONE
    }

    private fun showNoContentUi() {
        binding.loadingLayout.visibility = View.GONE
        binding.noContentLayout.visibility = View.VISIBLE
    }
}