package com.player.musicoo.innertube.requests

import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.models.BrowseResponse
import com.player.musicoo.innertube.models.MusicShelfRenderer
import com.player.musicoo.innertube.models.bodies.BrowseBody
import com.player.musicoo.innertube.utils.runCatchingNonCancellable
import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.client.request.setBody

suspend fun Innertube.moPlaylistPage(browseId: String): Result<Innertube.MoPlaylistOrAlbumPage>? =
    runCatchingNonCancellable {
        val response = client.post(browse) {
            setBody(BrowseBody(browseId = browseId))
        }.body<BrowseResponse>()


        val musicDetailHeaderRenderer = response
            .header
            ?.musicDetailHeaderRenderer

        val sectionListRendererContents = response
            .contents
            ?.singleColumnBrowseResultsRenderer
            ?.tabs
            ?.firstOrNull()
            ?.tabRenderer
            ?.content
            ?.sectionListRenderer
            ?.contents

        val musicShelfRenderer = sectionListRendererContents
            ?.firstOrNull()
            ?.musicShelfRenderer

        val musicCarouselShelfRenderer = sectionListRendererContents
            ?.getOrNull(1)
            ?.musicCarouselShelfRenderer

        val list: MutableList<Innertube.MoPlaylistOrAlbumPage.MoPlaylistOrAlbumListBean> =
            mutableListOf()
        list.clear()
        val contents = musicShelfRenderer?.contents
        if (contents != null) {
            for (content: MusicShelfRenderer.Content in contents) {
                val runs0 = content.musicResponsiveListItemRenderer
                    ?.flexColumns
                    ?.getOrNull(0)
                    ?.musicResponsiveListItemFlexColumnRenderer
                    ?.text
                    ?.runs
                    ?.firstOrNull()
                val watchEndpoint = runs0?.navigationEndpoint?.watchEndpoint
                val runs1 = content.musicResponsiveListItemRenderer
                    ?.flexColumns
                    ?.getOrNull(1)
                    ?.musicResponsiveListItemFlexColumnRenderer
                    ?.text
                    ?.runs
                    ?.firstOrNull()
                val browseEndpoint = runs1?.navigationEndpoint?.browseEndpoint

                val runs2 = content.musicResponsiveListItemRenderer
                    ?.flexColumns
                    ?.getOrNull(2)
                    ?.musicResponsiveListItemFlexColumnRenderer
                    ?.text
                    ?.runs
                    ?.firstOrNull()

                val thumbnailUrl = content.musicResponsiveListItemRenderer
                    ?.thumbnail
                    ?.musicThumbnailRenderer
                    ?.thumbnail
                    ?.thumbnails
                    ?.firstOrNull()
                    ?.url


                val bean = Innertube.MoPlaylistOrAlbumPage.MoPlaylistOrAlbumListBean(
                    title = runs0?.text,
                    name = runs1?.text ?: musicDetailHeaderRenderer
                        ?.subtitle
                        ?.runs
                        ?.map { it.text }
                        ?.joinToString(""),
                    desc = runs2?.text,
                    timeText = content.musicResponsiveListItemRenderer
                        ?.fixedColumns?.firstOrNull()
                        ?.musicResponsiveListItemFlexColumnRenderer
                        ?.text
                        ?.runs
                        ?.firstOrNull()
                        ?.text,
                    browseId = browseEndpoint?.browseId.toString(),
                    videoId = watchEndpoint?.videoId,
                    playlistId = watchEndpoint?.playlistId,
                    playlistSetVideoId = watchEndpoint?.playlistSetVideoId,
                    params = watchEndpoint?.params,
                    musicVideoType = watchEndpoint?.type,
                    pageType = browseEndpoint?.type,
                    thumbnailUrl = thumbnailUrl
                )

                list.add(bean)
            }
        }


        Innertube.MoPlaylistOrAlbumPage(
            title = musicDetailHeaderRenderer
                ?.title
                ?.runs
                ?.firstOrNull()
                ?.text,
            subtitle = musicDetailHeaderRenderer
                ?.subtitle
                ?.runs
                ?.map { it.text }
                ?.joinToString(""),
            thumbnail = musicDetailHeaderRenderer
                ?.thumbnail
                ?.musicThumbnailRenderer
                ?.thumbnail
                ?.thumbnails
                ?.let { it.getOrNull(3) ?: it.getOrNull(2) ?: it.getOrNull(1) ?: it.getOrNull(0) }
                ?.url,
            secondSubtitle = musicDetailHeaderRenderer
                ?.secondSubtitle
                ?.runs
                ?.map { it.text }
                ?.joinToString(""),
            description = musicDetailHeaderRenderer
                ?.description
                ?.runs
                ?.firstOrNull()
                ?.text,
            moPlaylistOrAlbumListBean = list
        )
    }