/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.pocket.stories.update

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mozilla.components.service.pocket.stories.PocketStoriesUseCases

/**
 * WorkManager Worker used for downloading and persisting locally a new list of Pocket recommended stories.
 */
internal class RefreshPocketWorker(
    private val context: Context,
    params: WorkerParameters
) : CoroutineWorker(context, params) {

    override suspend fun doWork(): Result {
        return withContext(Dispatchers.IO) {
            if (PocketStoriesUseCases().RefreshPocketStories(context).invoke()
            ) {
                Result.success()
            } else {
                Result.retry()
            }
        }
    }

    internal companion object {
        const val REFRESH_WORK_TAG =
            "mozilla.components.feature.pocket.recommendations.refresh.work.tag"
    }
}
