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.Clien
import rasel.lunar.launcher.model.getHref
import rasel.lunar.launcher.model.getRssData
import rasel.lunar.launcher.model.getT
import rasel.lunar.launcher.utils.BLog

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

    fun parseClien(div_clien : org.jsoup.nodes.Element) {
//        BLog.LOGE("div_clien >>>> ${div_clien}")
//
//        BLog.LOGE("div_clien >>>> ${div_clien.getElementsByClass("subject_fixed").getT()}")
//        BLog.LOGE("div_clien >>>> ${div_clien.getElementsByClass("shortname fixed").getT()}")
//        BLog.LOGE("div_clien >>>> ${div_clien.getElementsByClass("list_subject").getHref()}")
//        BLog.LOGE("div_clien >>>> ${div_clien.getElementsByClass("timestamp").getT()}")

        val title  = div_clien.getElementsByClass("subject_fixed").getT()
        val desc  = div_clien.getElementsByClass("shortname fixed").getT()
        val link  = div_clien.getElementsByClass("list_subject").getHref()
        val timeStamp  = div_clien.getElementsByClass("timestamp").getT()
        if (title.length > 0 && timeStamp.length > 0) {
            Clien().let { c ->
                c.title = title
                c.link = "https://www.clien.net".plus(link)
                c.desc = desc
                c.dateTiem = timeStamp
                if (c.pubDate() > limitDateTime) {
                    temp.add(c.getRssData())
                }
            }
        }
    //        var desc = tq_tr.getElementsByClass("cate").getT()
//        var title = tq_tr.getElementsByClass("title").getT()
//        var pageLink = tq_tr.getElementsByTag("a").getHref()
//        var dateTime = tq_tr.getElementsByClass("time").getT()

//        BLog.LOGE("${TAG} :::: desc >>> $desc")
//        BLog.LOGE("${TAG} :::: title >>> $title")
//        BLog.LOGE("${TAG} :::: pageLink >>> $pageLink")
//        BLog.LOGE("${TAG} :::: dateTime >>> $dateTime")

//        if (title.length > 0 && pageLink.length > 0) {
//            TheQoo().let { tq ->
//                tq.title = title
//                tq.link = "https://theqoo.net".plus(pageLink)
//                tq.dateTiem = dateTime
//                tq.desc = desc
//                if (tq.pubDate() > limitDateTime) {
//                    temp.add(tq.getRssData())
//                }
//            }
//        }


    }

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

        try {
            val testUrl2 = arrayListOf("https://www.clien.net/service/group/community")
            testUrl2.forEach { url ->
                Jsoup.connect(url)
                    .userAgent(USAGT)
                    .get().let { ruli ->
                        BLog.LOGE("test ${url} >> ${ruli.title()}")
                        ruli.getElementsByClass("list_item symph_row  ").forEach { ruli_tr ->
                            parseClien(ruli_tr)
                        }
                    }
            }
        } catch (e:Exception){e.printStackTrace()}
        return  Result.success().apply {
            BLog.LOGE("ClienGetter temp >>>> ${temp.size}")
            WorkersDb.insertBulkData(temp)
        }
    }
}