package rasel.lunar.launcher.workers

import android.content.Context
import androidx.work.WorkerParameters
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import rasel.lunar.launcher.model.Arca
import rasel.lunar.launcher.model.RssDataInterface
import rasel.lunar.launcher.model.getRssData
import rasel.lunar.launcher.model.getT
import rasel.lunar.launcher.utils.beforeDay
import java.util.Date

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

    }

    override fun realWork(): Result {
        try {
            val urls = arrayListOf(
                "https://arca.live/b/singbung?mode=best",
//                "https://arca.live/b/headline",
//                "https://arca.live/b/live",
                "https://arca.live/b/namuhotnow",
                "https://arca.live/b/society",
//                "https://arca.live/b/replay",
//                "https://arca.live/b/breaking"
            )
            urls.forEach {
                Jsoup.connect(it)
                    .userAgent(USAGT)
                    .get().let { arca ->
//                        BLog.LOGE("url >> ${it} >> ${arca}")
                        arca.getElementsByClass("vrow hybrid").forEach { araca_li ->
                            if (araca_li.html().contains("title ") == true) {
                                parseArcaLi(araca_li).apply {
                                    this.forEach {
                                        if (it.pubDate() > commicsDateTime) {
                                            temp.add(it.getRssData())
                                        }
                                    }
                                }
                            }
                        }
                    }
            }
//            Jsoup.connect("https://projrctjav.com").userAgent(USAGT).get().let { projectj ->
//                BLog.LOGE("projectj >>>>> ${projectj}")
//            }
        } catch (e:Exception){e.printStackTrace()}

        return Result.success().apply {
            WorkersDb.insertBulkData(temp)
        }
    }

    private fun parseArcaLi(aracaLi: Element) : ArrayList<RssDataInterface> {
        var tempArray = arrayListOf<RssDataInterface>()
//        BLog.LOGE("aracaLi >>> ${aracaLi}")
        var title = aracaLi.getElementsByClass("title hybrid-title").getT()
        var desc = aracaLi.getElementsByClass("badge").getT()
        desc.plus(aracaLi.getElementsByClass("user-info ").getT())
        var dateTime = aracaLi.getElementsByTag("time").attr("datetime")
        var tumbnail =  aracaLi.getElementsByTag("img").attr("src")
        var link = "https://arca.live".plus(if(aracaLi.getElementsByClass("title hybrid-title").size > 0) aracaLi.getElementsByClass("title hybrid-title").get(0).attr("href") else if(aracaLi.getElementsByTag("a").size > 0) aracaLi.getElementsByTag("a").get(0).attr("href") else "")
        if (title.length > 0 && link.length > 20) {
            Arca().apply {
                this.link = link
                this.title = title
                if (tumbnail.length > 0) {
                    this.thumbnail = "https:".plus(tumbnail)
//
//                    BLog.LOGE("Arca thumbnail >>> ${thumbnail}")
                }
                this.desc = desc
                this.dateTiem = dateTime
            }.apply {
//                BLog.LOGE("parseArcaLi >>>> ${this}")
                if(this.pubDate() > beforeDay(Date(),3)) {
                    tempArray.add(this)
                }
            }
        }
        return tempArray
    }


}