package com.player.musicoo.innertube.requests

import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.models.ContinuationResponse
import com.player.musicoo.innertube.models.NextResponse
import com.player.musicoo.innertube.models.bodies.ContinuationBody
import com.player.musicoo.innertube.models.bodies.NextBody
import com.player.musicoo.innertube.utils.from
import com.player.musicoo.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


    }