package rasel.lunar.launcher.workers

import android.annotation.SuppressLint
import android.content.Context
import androidx.work.WorkerParameters
import com.google.gson.Gson
import org.json.JSONObject
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import rasel.lunar.launcher.model.RssData
import rasel.lunar.launcher.model.RssDataInterface
import rasel.lunar.launcher.model.getRssData
import rasel.lunar.launcher.model.others.Youtube
import rasel.lunar.launcher.utils.RssList

class YoutubeGetter : BaseGetter {
    var rssUrls = arrayListOf<String>()
    constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams) {

    }

    @SuppressLint("RestrictedApi")
    override fun realWork(): Result {
        rssUrls.clear()
        rssUrls.addAll(RssList.youtubeUrls)
        for (url in rssUrls) {
            temp.addAll(ytChannel(Jsoup.connect(url).userAgent(USAGT).get()))
        }
        return  Result.success().apply {
            WorkersDb.insertBulkData(temp)
        }
    }

    fun ytChannel(doc: Document) : ArrayList<RssData> {
        try {
            doc.getElementsByTag("script").forEach {
                if(it.html().contains("var ytInitialData", false)) {/**/
                    var ytInitialData = it.html().split("var ytInitialData = ")[1].split("</script>")[0].toString()
                    var tempJSONObject : JSONObject? = null
                    JSONObject(ytInitialData).apply{
                        tempJSONObject = this
                        val root = Gson().fromJson(tempJSONObject.toString(), Youtube::class.java)
                        (if ((root?.contents?.singleColumnBrowseResultsRenderer?.tabs?.size
                                ?: 0) > 0
                        ) {
                            root?.contents?.singleColumnBrowseResultsRenderer?.tabs?.forEach {
                                it.tabRenderer?.content?.sectionListRenderer?.contents?.forEach {
                                    it.shelfRenderer?.content?.verticalListRenderer?.items?.forEach {
                                        (it.compactVideoRenderer as? RssDataInterface)?.let {
                                            if(it.pubDate() >  limitDateTime) {
                                                temp.add(it.getRssData())
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            root?.contents?.twoColumnBrowseResultsRenderer?.tabs?.forEach {
                                it.tabRenderer?.content?.sectionListRenderer?.contents?.forEach {
                                    it.itemSectionRenderer?.contents?.forEach {
                                        it.shelfRenderer?.content?.horizontalListRenderer?.items?.forEach {
                                            (it.gridVideoRenderer as? RssDataInterface)?.let {
                                                if(it.pubDate() >limitDateTime) {
                                                    temp.add(it.getRssData())
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }).apply {
                        }
                    }
                }
            }
        } catch(e: Exception) {

        }
        return temp
    }
}