package rasel.lunar.launcher.workers

import android.annotation.SuppressLint
import android.content.Context
import androidx.work.WorkerParameters
import org.jsoup.Jsoup
import rasel.lunar.launcher.model.DcInside
import rasel.lunar.launcher.model.RssData
import rasel.lunar.launcher.model.RssDataInterface
import rasel.lunar.launcher.model.getRssData

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

    fun parseDcLi(dc_li : org.jsoup.nodes.Element) : ArrayList<RssDataInterface>{
        var temp = arrayListOf<RssDataInterface>()
        if (dc_li.html().contains("<ul class=>") && dc_li.html().contains("con_list img")) {
            dc_li.child(0).getElementsByTag("li").forEach {
                parseDcLi(it)
            }
        } else {
            var link = if (dc_li.getElementsByTag("a").size > 0) dc_li.getElementsByTag("a").get(0)
                .attr("href") else ""
            var title =
                if (dc_li.getElementsByClass("box besttxt").size > 0) dc_li.getElementsByClass("box besttxt")
                    .get(0)
                    .text() else if (dc_li.getElementsByClass("tit").size > 0) dc_li.getElementsByClass(
                    "tit"
                ).get(0).text() else ""
            var thumbnail =
                if (dc_li.getElementsByTag("img").size > 0) dc_li.getElementsByTag("img").get(0)
                    .attr("src") else ""
            var desc =
                if (dc_li.getElementsByClass("box best_info").size > 0) dc_li.getElementsByClass("box best_info")
                    .get(0).text() else ""
            var dateTiem =
                if (dc_li.getElementsByClass("time").size > 0) dc_li.getElementsByClass("time")
                    .get(0).text() else ""
            link = link.replace("&amp;","&")
            thumbnail = thumbnail.replace("&amp;","&")
//            BLog.LOGE("DC_LI >>>> link >>>> ${link}")
//            BLog.LOGE("DC_LI >>>> title >>>> ${title}")
//            BLog.LOGE("DC_LI >>>> thumbnail >>>> ${thumbnail}")
//            BLog.LOGE("DC_LI >>>> desc >>>> ${desc}")
//            BLog.LOGE("DC_LI >>>> dateTiem >>>> ${dateTiem}")
            if (title.length > 0 && link.length > 0) {
                DcInside().apply {
                    this.link = link
                    this.title = title
                    this.thumbnail = thumbnail
                    this.desc = desc
                    this.dateTiem = dateTiem
                }.apply {
                    if (this.pubDate() > limitDateTime) {
                        temp.add(this.getRssData())
                    }
                }
            }
        }
        return temp
    }

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

        var tempArray = arrayListOf<RssData>()

        try {
            val testUrl2 = "https://www.dcinside.com/"
            Jsoup.connect(testUrl2)
                .userAgent(USAGT)
                .get().let { dc ->
//            BLog.LOGE("test ${testUrl2} >> ${this}")
                    dc.getElementsByTag("li").forEach { dc_li ->
                        if (dc_li.html().contains("main_log") == true) {
                            parseDcLi(dc_li).apply {
                                this.forEach {
                                    if (it.pubDate() > commicsDateTime) {
                                        tempArray.add(it.getRssData())
                                    }
                                }
                            }
                        }
                    }
                }


        } catch (e:Exception){e.printStackTrace()}
        return  Result.success().apply {
            WorkersDb.insertBulkData(tempArray)
        }
    }
}