package me.tatarka.support.internal;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;

import java.util.ArrayList;
import java.util.List;

import me.tatarka.support.job.JobInfo;
import me.tatarka.support.job.JobScheduler;
import me.tatarka.support.os.PersistableBundle;

/** @hide */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class JobSchedulerLollipopDelegate extends JobScheduler {
    private Context context;
    private android.app.job.JobScheduler jobScheduler;

    private static JobSchedulerLollipopDelegate INSTANCE;
    public static synchronized JobSchedulerLollipopDelegate getLollipopInstance(Context context) {
        if (INSTANCE == null) INSTANCE = new JobSchedulerLollipopDelegate(context);
        return INSTANCE;
    }

    private JobSchedulerLollipopDelegate(Context context) {
        this.context = context.getApplicationContext();
        jobScheduler = (android.app.job.JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    }

    @Override
    public void cancel(int jobid) {
        jobScheduler.cancel(jobid);
    }

    @Override
    public void cancelAll() {
        jobScheduler.cancelAll();
    }

    @Override
    public List<JobInfo> getAllPendingJobs() {
        List<android.app.job.JobInfo> jobs = jobScheduler.getAllPendingJobs();
        List<JobInfo> result = new ArrayList<JobInfo>(jobs.size());
        for (android.app.job.JobInfo job : jobs) {
            result.add(convertFromJobInfo(job));
        }
        return result;
    }

    @Override
    public int schedule(JobInfo job) {
        return jobScheduler.schedule(convertJobInfo(job));
    }

    private static android.app.job.JobInfo convertJobInfo(JobInfo job) {
        android.app.job.JobInfo.Builder builder = new android.app.job.JobInfo.Builder(job.getId(), job.getService());

        builder.setExtras((android.os.PersistableBundle) job.getExtras().getRealBundle());
        builder.setRequiresCharging(job.isRequireCharging());
        builder.setRequiresDeviceIdle(job.isRequireDeviceIdle());
        builder.setRequiredNetworkType(job.getNetworkType());

        if (job.getMinLatencyMillis() != 0) {
            builder.setMinimumLatency(job.getMinLatencyMillis());
        }

        if (job.getMaxExecutionDelayMillis() != 0) {
            builder.setOverrideDeadline(job.getMaxExecutionDelayMillis());
        }

        if (job.isPeriodic()) {
            builder.setPeriodic(job.getIntervalMillis());
        }

        builder.setPersisted(job.isPersisted());
        builder.setBackoffCriteria(job.getInitialBackoffMillis(), job.getBackoffPolicy());

        return builder.build();
    }

    private static JobInfo convertFromJobInfo(android.app.job.JobInfo job) {
        JobInfo.Builder builder = new JobInfo.Builder(job.getId(), job.getService());
        builder.setExtras(new PersistableBundle(job.getExtras()));
        builder.setRequiresCharging(job.isRequireCharging());
        builder.setRequiresDeviceIdle(job.isRequireDeviceIdle());
        builder.setRequiredNetworkType(job.getNetworkType());

        if (job.getMinLatencyMillis() != 0) {
            builder.setMinimumLatency(job.getMinLatencyMillis());
        }

        if (job.getMaxExecutionDelayMillis() != 0) {
            builder.setOverrideDeadline(job.getMaxExecutionDelayMillis());
        }

        if (job.isPeriodic()) {
            builder.setPeriodic(job.getIntervalMillis());
        }

        builder.setPersisted(job.isPersisted());
        builder.setBackoffCriteria(job.getInitialBackoffMillis(), job.getBackoffPolicy());

        return builder.build();
    }
}

@@ -2130,33 +2130,93 @@
 tNetworkType());
-%0A
+ %0A        builder.setPersisted(job.isPersisted());  %0A        
 %0A        if (job
@@ -2234,32 +2234,53 @@
 ncyMillis() != 0
+ && !job.isPeriodic()
 ) %7B%0A            
@@ -2385,32 +2385,53 @@
 layMillis() != 0
+ && !job.isPeriodic()
 ) %7B%0A            
@@ -2617,49 +2617,46 @@
     
-builder.setPersisted(job.isPersisted());%0A
+if (!job.isRequireDeviceIdle()) %7B%0A    
     
@@ -2733,32 +2733,42 @@
 ackoffPolicy());
+%0A        %7D
 %0A%0A        return
@@ -2951,24 +2951,33 @@
 Service());%0A
+        %0A
         buil
@@ -3216,24 +3216,73 @@
 workType());
+%0A        builder.setPersisted(job.isPersisted());
 %0A%0A        if
@@ -3305,32 +3305,53 @@
 ncyMillis() != 0
+ && !job.isPeriodic()
 ) %7B%0A            
@@ -3464,16 +3464,37 @@
 s() != 0
+ && !job.isPeriodic()
 ) %7B%0A    
@@ -3688,49 +3688,46 @@
     
-builder.setPersisted(job.isPersisted());%0A
+if (!job.isRequireDeviceIdle()) %7B%0A    
     
@@ -3808,24 +3808,34 @@
 ffPolicy());
+%0A        %7D
 %0A%0A        re

Fix IllegalArgumentException