package com.example.patrice.job_scheduler_02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    Chronometer chronometer;
    Button btnStartJob, btnCancelJobs;

    JobScheduler jobScheduler;
    private static final int MYJOBID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        chronometer = (Chronometer)findViewById(R.id.chronometer);
        btnStartJob = (Button)findViewById(R.id.startjob);
        btnCancelJobs =  (Button)findViewById(R.id.canceljobs);

        jobScheduler = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);

        btnStartJob.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                chronometer.setBase(SystemClock.elapsedRealtime());
                chronometer.start();


                ComponentName jobService =
                        new ComponentName(getPackageName(), MyJobService.class.getName());
                JobInfo jobInfo =  new JobInfo.Builder(MYJOBID, jobService).setPeriodic(10000).build();
    /*
     * setPeriodic(long intervalMillis)
     * Specify that this job should recur with the provided interval,
     * not more than once per period.
     */

                int jobId = jobScheduler.schedule(jobInfo);
                if(jobScheduler.schedule(jobInfo)>0){
                    Toast.makeText(MainActivity.this,
                            "Successfully scheduled job: " + jobId,
                            Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,
                            "RESULT_FAILURE: " + jobId,
                            Toast.LENGTH_SHORT).show();
                }

            }});

        btnCancelJobs.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                chronometer.stop();

                List<JobInfo> allPendingJobs = jobScheduler.getAllPendingJobs();
                String s = "";
                for(JobInfo j : allPendingJobs){
                    int jId = j.getId();
                    jobScheduler.cancel(jId);
                    s += "jobScheduler.cancel(" + jId + " )";
                }
                Toast.makeText(MainActivity.this,
                        s,
                        Toast.LENGTH_SHORT).show();

                //or
                //jobScheduler.cancelAll();


            }});
    }


}