package com.centralcloud.oncall.work

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.centralcloud.oncall.BuildConfig
import com.centralcloud.oncall.db.Repository
import com.centralcloud.oncall.msg.ApiService
import com.centralcloud.oncall.msg.NotificationDispatcher
import com.centralcloud.oncall.msg.Poller
import com.centralcloud.oncall.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
    // IMPORTANT:
    //   Every time the worker is changed, the periodic work has to be REPLACEd.
    //   This is facilitated in the MainActivity using the VERSION below.

    init {
        Log.init(ctx) // Init in all entrypoints
    }

    override suspend fun doWork(): Result {
        return withContext(Dispatchers.IO) {
            Log.d(TAG, "Polling for new notifications")
            val repository = Repository.getInstance(applicationContext)
            val dispatcher = NotificationDispatcher(applicationContext, repository)
            val api = ApiService(applicationContext)
            val poller = Poller(api, repository)

            val baseUrl = inputData.getString(INPUT_DATA_BASE_URL)
            val topic = inputData.getString(INPUT_DATA_TOPIC)
            val subscriptions = if (baseUrl != null && topic != null) {
                val subscription = repository.getSubscription(baseUrl, topic) ?: return@withContext Result.success()
                listOf(subscription)
            } else {
                repository.getSubscriptions()
            }

            subscriptions.forEach{ subscription ->
                try {
                    val newNotifications = poller.poll(subscription)
                    newNotifications.forEach { notification ->
                        dispatcher.dispatch(subscription, notification)
                    }
                } catch (e: Exception) {
                    Log.e(TAG, "Failed checking messages: ${e.message}", e)
                }
            }
            Log.d(TAG, "Finished polling for new notifications")
            return@withContext Result.success()
        }
    }

    companion object {
        const val VERSION =  BuildConfig.VERSION_CODE
        const val TAG = "OncallPollWorker"
        const val WORK_NAME_PERIODIC_ALL = "OncallPollWorkerPeriodic" // Do not change
        const val WORK_NAME_ONCE_SINGE_PREFIX = "OncallPollWorkerSingle" // e.g. OncallPollWorkerSingle_https://oncall.hugo.dk_mytopic
        const val INPUT_DATA_BASE_URL = "baseUrl"
        const val INPUT_DATA_TOPIC = "topic"
    }
}
