package app.furmeet.events.brasilfurfest.notification

import android.app.job.JobParameters
import android.app.job.JobService
import app.furmeet.events.brasilfurfest.helper.resourcesAliased
import app.furmeet.events.brasilfurfest.schedule.managers.ScheduleManager
import app.furmeet.events.brasilfurfest.schedule.managers.UpdateChecker
import org.jetbrains.anko.doAsync
import java.util.concurrent.Future

class FavoriteUploaderJobService : JobService() {
    lateinit var fut1: Future<Unit>
    lateinit var fut2: Future<Unit>
    override fun onStartJob(jobParameters: JobParameters): Boolean {
        resourcesAliased = resources
        fut2 = doAsync { }
        fut1 = doAsync {
            val uc = UpdateChecker(baseContext)
            val sm = ScheduleManager(baseContext)
            fut2 = uc.sendUploadableFavorites(
                    sm.attendeeFavorites.uploadables,
                    sm.attendeeFavorites.registrationTier.level,
                    sm.attendeeFavorites.conventionId,
                    sm.uniqueIdentifier
            ) {
                jobFinished(jobParameters, true)
            }
        }
        return true
    }

    override fun onStopJob(jobParameters: JobParameters): Boolean {
        fut1.cancel(true)
        fut2.cancel(true)
        jobFinished(jobParameters, true)
        return true
    }
}