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.Context
import melody.offline.music.innertube.models.SectionListRenderer
import melody.offline.music.innertube.models.bodies.BrowseBody
import melody.offline.music.innertube.utils.runCatchingNonCancellable
import melody.offline.music.util.LogTag
import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.client.request.setBody

//
suspend fun Innertube.homePage(visitorData: String? = null): Result<Innertube.BaseHomePage>? =
    runCatchingNonCancellable {

        LogTag.LogD(TAG, "visitorData->${visitorData}")
        val defaultWeb = Context(
            client = Context.Client(
                clientName = "WEB_REMIX",
                clientVersion = "1.20220918",
                platform = "DESKTOP",
                visitorData = visitorData
            )
        )

        val response = client.post(browse) {
            setBody(BrowseBody(defaultWeb, "FEmusic_home"))
        }.body<BrowseResponse>()

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

        val contents = sectionListRenderer?.contents

        val visitorDataNew= response.responseContext?.visitorData
//        if(!visitorDataNew.isNullOrEmpty()){
//            AppStore(App.app).myVisitorData = visitorDataNew
//        }

        val continuations = sectionListRenderer?.continuations
        val nextContinuationData = continuations
            ?.firstOrNull()
            ?.nextContinuationData

        val cToken = nextContinuationData?.continuation
        val itct = nextContinuationData?.clickTrackingParams


        val searchList: MutableList<Innertube.HomePage> = mutableListOf()

        if (contents != null) {
            for (content: SectionListRenderer.Content in contents) {
                val text = content.musicCarouselShelfRenderer
                    ?.header
                    ?.musicCarouselShelfBasicHeaderRenderer
                    ?.title
                    ?.runs
                    ?.firstOrNull()
                    ?.text

                val homePage =
                    content.musicCarouselShelfRenderer?.contents?.let {
                        Innertube.HomePage(
                            text,
                            it
                        )
                    }
                if (homePage != null) {
                    searchList.add(homePage)
                }
            }
        }
        val baseHomePage =
            Innertube.BaseHomePage(
                visitorDataNew,
                cToken,
                cToken.toString(),
                itct.toString(),
                searchList
            )

        baseHomePage
    }