package rasel.lunar.launcher.workers

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.net.toUri
import androidx.work.WorkerParameters
import org.jsoup.Jsoup
import rasel.lunar.launcher.model.RuliWeb
import rasel.lunar.launcher.model.getRssData
import rasel.lunar.launcher.utils.BLog

class RuliWebGetter : BaseGetter {
    companion object {
        val TAG = "RuliWebGetter"
    }
    constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams) {
    }

    fun parseRuli(ruli_tr : org.jsoup.nodes.Element) {
        var aTags = ruli_tr.getElementsByTag("a")
        var tagIdx = 0
        var pageLink = ""
        var thumbnailUrl = ""
        var title = ""
        var desc = ""
        var dateTimeTxt = ""
        if(ruli_tr.getElementsByTag("a").size > tagIdx) {
            pageLink = aTags.get(tagIdx).attr("href").replace("&amp;","&")
            thumbnailUrl = aTags.get(tagIdx).attr("style").split("(")[1].replace(");","")
        }
        tagIdx = 1
        if(ruli_tr.getElementsByTag("a").size > tagIdx) { title = aTags.get(tagIdx).text() }
        tagIdx = 2
        if(ruli_tr.getElementsByTag("a").size > tagIdx) { desc = aTags.get(tagIdx).text().plus(" / ") }
        tagIdx = 3
        if(ruli_tr.getElementsByTag("a").size > tagIdx) { desc = desc.plus(aTags.get(tagIdx).text()) }
        var timeClass  = ruli_tr.getElementsByClass("time")
        if(timeClass.size > 0) { dateTimeTxt = timeClass.get(0).text() }

//        BLog.LOGE("pageLink >>>> $pageLink ")
//        BLog.LOGE("thumbnailUrl >>>> $thumbnailUrl ")
//        BLog.LOGE("title >>>> $title ")
//        BLog.LOGE("desc >>>> $desc ")
//        BLog.LOGE("dateTimeTxt >>>> $dateTimeTxt ")
        if (title.length > 0 && pageLink.length > 0) {
            RuliWeb().let { ru ->
                ru.title = title
                ru.link = pageLink.replace(pageLink.toUri().query ?: "","")
                ru.desc = desc
                ru.thumbnail = thumbnailUrl
                ru.dateTiem = dateTimeTxt.replace("날짜","").trim()
                if (ru.pubDate() > limitDateTime) {
                    temp.add(ru.getRssData())
                }
            }
        }
    }

    @SuppressLint("RestrictedApi")
    override fun realWork(): Result {

        try {
            val testUrl2 = arrayListOf("https://bbs.ruliweb.com/best/humor_only","https://bbs.ruliweb.com/best/humor_only/now?m=humor_only&t=default&page=2")
            testUrl2.forEach { url ->
                Jsoup.connect(url)
                    .userAgent(USAGT)
                    .get().let { ruli ->
                        BLog.LOGE("test ${testUrl2} >> ${ruli.title()}")
                        ruli.getElementsByTag("tr").forEach { ruli_tr ->
                            parseRuli(ruli_tr)
                        }
                    }
            }
        } catch (e:Exception){e.printStackTrace()}
        return  Result.success().apply {
//            BLog.LOGE("Ruli temp >>>> ${temp.size}")
            WorkersDb.insertBulkData(temp)
        }
    }
}