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.FullUpdateCheckerCancellable
import app.furmeet.events.system.schedule.managers.RemoteAssetsHolder
import java.util.concurrent.Future

class ScheduleUpdaterJobService : JobService() {
    private lateinit var fucc: FullUpdateCheckerCancellable
    private lateinit var killer: Future<*>

    override fun onStartJob(jobParameters: JobParameters): Boolean {
        RemoteAssetsHolder.ensureIsBuilt(this)
        resourcesAliased = resources
        fucc = FullUpdateCheckerCancellable(baseContext)
        fucc.run {
            NotificationFirer().fire(baseContext)
            jobFinished(jobParameters, true)
        }
        killer = doAsync {
            Thread.sleep(25000)
            fucc.stop()
            jobFinished(jobParameters, true)
        }
        return true
    }

    override fun onStopJob(jobParameters: JobParameters): Boolean {
        fucc.stop()
        killer.cancel(true)
        jobFinished(jobParameters, true)
        return true
    }

}