package com.player.musicoo.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.gyf.immersionbar.ktx.immersionBar
import com.player.musicoo.databinding.FragmentMoHomeBinding
import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.models.MusicCarouselShelfRenderer
import com.player.musicoo.innertube.requests.homePage
import com.player.musicoo.innertube.requests.homePageMore
import com.player.musicoo.util.LogTag.LogD
import com.player.musicoo.view.MusicResponsiveListView
import com.player.musicoo.view.MusicTowRowListView
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select

class MoHomeFragment : MoBaseFragment<FragmentMoHomeBinding>() {

    private val requests: Channel<Request> = Channel(Channel.UNLIMITED)

    enum class Request {
        TryAgain,
    }

    override val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMoHomeBinding
        get() = FragmentMoHomeBinding::inflate

    override suspend fun onViewCreated() {
        initView()
        initData()
        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()
                        }
                    }
                }
            }
        }
    }

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

    private suspend fun initData() {
        showLoadingUi()
        Innertube.homePage()?.onSuccess {
            showDataUi()
            if (it.homePage.isNotEmpty()) {
                for (home: Innertube.HomePage in it.homePage) {
                    for (content: MusicCarouselShelfRenderer.Content in home.contents) {
                        if (content.musicResponsiveListItemRenderer != null) {
                            binding.contentLayout.addView(
                                MusicResponsiveListView(
                                    requireActivity(),
                                    home
                                )
                            )
                            break
                        }
                        if (content.musicTwoRowItemRenderer != null) {
                            binding.contentLayout.addView(
                                MusicTowRowListView(
                                    requireActivity(),
                                    home
                                )
                            )
                            break
                        }
                    }
                }
                initHomeDataMore(it)
            } else {
                LogD(TAG, "homePage size 0")
                showNoContentUi()
            }
        }?.onFailure {
            showNoContentUi()
            LogD(TAG, "homePage onFailure->${it}")
        }
    }

    private suspend fun initHomeDataMore(baseHomePage: Innertube.BaseHomePage) {
        if (baseHomePage.cToken?.isNotEmpty() == true) {
            Innertube.homePageMore(baseHomePage)?.onSuccess {
                for (home: Innertube.HomePage in it.homePage) {
                    for (content: MusicCarouselShelfRenderer.Content in home.contents) {
                        if (content.musicResponsiveListItemRenderer != null) {
                            binding.contentLayout.addView(
                                MusicResponsiveListView(
                                    requireActivity(),
                                    home
                                )
                            )
                            break
                        }
                        if (content.musicTwoRowItemRenderer != null) {
                            binding.contentLayout.addView(
                                MusicTowRowListView(
                                    requireActivity(),
                                    home
                                )
                            )
                            break
                        }
                    }
                }
                initHomeDataMore(it)
            }?.onFailure {
                LogD(TAG, "initHomeDataMore onFailure ->${it}")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        initImmersionBar()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            initImmersionBar()
        }
    }

    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
    }

}