package melody.offline.music.innertube.requests

import melody.offline.music.innertube.Innertube
import melody.offline.music.innertube.models.BrowseResponse
import melody.offline.music.innertube.models.MusicShelfRenderer
import melody.offline.music.innertube.models.bodies.BrowseBody
import melody.offline.music.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>()

        //6月24日发现，数据结构发生变化。

//        val musicDetailHeaderRenderer = response
//            .header
//            ?.musicDetailHeaderRenderer

        val musicResponsiveHeaderRenderer = response
            .contents
            ?.twoColumnBrowseResultsRenderer
            ?.tabs
            ?.firstOrNull()
            ?.tabRenderer
            ?.content
            ?.sectionListRenderer
            ?.contents
            ?.firstOrNull()
            ?.musicResponsiveHeaderRenderer

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

        val sectionListRendererContents = response
            .contents
            ?.twoColumnBrowseResultsRenderer
            ?.secondaryContents
            ?.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 ?: musicResponsiveHeaderRenderer
                        ?.straplineTextOne
                        ?.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,
                    backupThumbnailUrl = musicResponsiveHeaderRenderer
                        ?.thumbnail
                        ?.musicThumbnailRenderer
                        ?.thumbnail
                        ?.thumbnails
                        ?.let { it.getOrNull(3) ?: it.getOrNull(2) ?: it.getOrNull(1) ?: it.getOrNull(0) }
                        ?.url
                )

                list.add(bean)
            }
        }


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