package bums.lunatic.launcher.workers

import android.annotation.SuppressLint
import android.content.Context
import androidx.work.WorkerParameters
import bums.lunatic.launcher.home.adapters.RssFeedsParser
import bums.lunatic.launcher.model.RssData
import bums.lunatic.launcher.model.RssDataType
import bums.lunatic.launcher.model.getRssData
import bums.lunatic.launcher.utils.RssList.feedJsons
import bums.lunatic.launcher.utils.RssList.feedJsons_nsfw

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

    }

    @SuppressLint("RestrictedApi")
    override fun realWork(): Result {
        val temp = arrayListOf<RssData>()
        RssDataType.REDDIT.isOn { for (url in feedJsons) {
            for (it in RssFeedsParser.getReddit(url,false)) {
                if (it.pubDate() >= limitDateTime) {
                    temp.add(it.getRssData())
                }
            }
        } }

        RssDataType.REDDIT_NSFW.isOn { for (url in feedJsons_nsfw) {
            for (it in RssFeedsParser.getReddit(url,true)) {
                if (it.pubDate() >= limitDateTime) {
                    temp.add(it.getRssData())
                }
            }
        } }

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