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.RssDataType
import bums.lunatic.launcher.model.getRssData
import bums.lunatic.launcher.utils.RssList

class NewsFeedsGetter : BaseGetter {
    companion object {
        val FEDDS_WORK_TAG = "NewsFeedsGetter"
    }
    var feddsUrls = arrayListOf<String>()
    constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams) {
    }

    @SuppressLint("RestrictedApi")
    override fun realWork(): Result {
        RssDataType.NEWSFEED.isOn {
            feddsUrls.clear()
            feddsUrls.addAll(RssList.newsFeeds)
            feddsUrls.addAll(RssList.getFeedUrls())

            for (url in feddsUrls) {
                for (it in RssFeedsParser.getFeeds(url)) {
                    if (it.pubDate() >= limitDateTime) {
                        temp.add(it.getRssData())
                    }
                }
            }

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

//        temp.forEach { synchronized(rssSet){
//            rssSet.put(it.originPage(), it)
//        }  }.run {
//            rssSetTouchCount -= 1
//            Result.success() }
    }


}