package app.furmeet.events.system.notification

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

class FavoriteUploaderJobService : JobService() {
    lateinit var fut1: Future<*>
    lateinit var fut2: Future<*>
    override fun onStartJob(jobParameters: JobParameters): Boolean {
        RemoteAssetsHolder.ensureIsBuilt(this)
        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
    }
}