package com.player.musicoo.activity

import android.view.View
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ktx.immersionBar
import com.player.musicoo.databinding.ActivitySingerDetailsBinding
import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.requests.moSingerListPage
import com.player.musicoo.util.LogTag.LogD
import com.player.musicoo.view.SingerDetailsOtherView
import com.player.musicoo.view.SingerDetailsSongView
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select

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

    enum class Request {
        TryAgain,
    }

    companion object {
        const val SINGER_DETAILS_PAGE_BROWSE_ID = "singer_details_page_browse_id"
    }

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

    override suspend fun main() {
        binding = ActivitySingerDetailsBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initImmersionBar()
        browseId = intent.getStringExtra(SINGER_DETAILS_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)
        }
        binding.singerDescExpand.setOnClickListener {
            showSongDescriptionDialog(binding.singerDesc.text.toString())
        }
    }

    private suspend fun initData(browseId: String) {
        showLoadingUi()
        Innertube.moSingerListPage(browseId)
            ?.onSuccess {
                showDataUi()
                Glide.with(this)
                    .load(it.thumbnail)
                    .into(binding.singerImg)
                binding.singerName.text = it.title
                if (it.description.isNullOrEmpty()) {
                    binding.singerDesc.visibility = View.GONE
                    binding.singerDescExpand.visibility = View.GONE
                } else {
                    binding.singerDesc.visibility = View.VISIBLE
                    binding.singerDescExpand.visibility = View.VISIBLE
                    binding.singerDesc.text = extractTextBeforeNewline(it.description)
                }
                if (it.list != null) {
                    for (bean: Innertube.SingerDetailsListPage in it.list) {
                        if (bean.contents?.musicShelfContentList != null && bean.contents.musicShelfContentList.isNotEmpty()) {
                            binding.contentLayout.addView(
                                SingerDetailsSongView(
                                    this@MoSingerDetailsActivity,
                                    bean
                                )
                            )
                        } else if (bean.contents?.musicCarouselShelfContentList != null && bean.contents.musicCarouselShelfContentList.isNotEmpty()) {
                            binding.contentLayout.addView(
                                SingerDetailsOtherView(
                                    this@MoSingerDetailsActivity,
                                    bean
                                )
                            )
                        }
                    }
                }
            }?.onFailure {
                showNoContentUi()
                LogD(TAG, "moSingerListPage 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
    }
}