package melody.offline.music.innertube.requests

import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import melody.offline.music.innertube.Innertube
import melody.offline.music.innertube.models.NextResponse
import melody.offline.music.innertube.models.bodies.NextBody
import melody.offline.music.innertube.utils.runCatchingNonCancellable


suspend fun Innertube.moNextPage(
    videoId: String,
    playlistId: String? = null,
    params: String? = null,
    playlistSetVideoId: String? = null
) =
    runCatchingNonCancellable {
        val response = client.post(next) {
            setBody(
                NextBody(
                    videoId = videoId,
                    playlistId = playlistId,
                    playlistSetVideoId = playlistSetVideoId,
                    params = params
                )
            )
        }.body<NextResponse>()

        val tabs = response
            .contents
            ?.singleColumnMusicWatchNextResultsRenderer
            ?.tabbedRenderer
            ?.watchNextTabbedResultsRenderer
            ?.tabs

        val playlistPanelRenderer = tabs
            ?.getOrNull(0)
            ?.tabRenderer
            ?.content
            ?.musicQueueRenderer
            ?.content
            ?.playlistPanelRenderer

        val endpoint = playlistPanelRenderer
            ?.contents
            ?.lastOrNull()
            ?.automixPreviewVideoRenderer
            ?.content
            ?.automixPlaylistVideoRenderer
            ?.navigationEndpoint
            ?.watchPlaylistEndpoint


    }