package com.example.patrice.appli_03;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

// https://www.concretepage.com/android/android-start-stop-service-from-activity-example-using-handlerthread

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

//https://www.c-sharpcorner.com/UploadFile/1e5156/start-service-using-alarmmanager-in-android/

// http://androidsamplecodes4u.blogspot.fr/2012/08/start-service-with-alarm-manager-android.html

// https://www.sanfoundry.com/java-android-program-start-service-alarm-manager/

public class MyService extends Service
{
    private NotificationManager mNM;

    @Override
    public void onCreate()
    {
        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
        Log.v("*********", "PLOUF");
      //  mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     //   showNotification();

    }

    @Override
    public IBinder onBind(Intent intent)
    {
        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
        return null;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
        mNM.cancel(0);

    }
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

        Log.v("*********", "BINGO");
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        showNotification();




    }

    @Override
    public boolean onUnbind(Intent intent)
    {
        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
        return super.onUnbind(intent);
    }

    private void showNotification() {

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
        builder.setSmallIcon(R.drawable.ok);
        builder.setContentTitle("Time is up");
        builder.setContentText("SLIMS");

        builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, cancel_notification.class), PendingIntent.FLAG_UPDATE_CURRENT));

        builder.setAutoCancel(true);

        builder.build().flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;


        builder.setVibrate(new long[] { 0, 200, 100, 200 });
        final Notification notification = builder.build();

        mNM.notify(0, notification);
        //NOTIFICATION += 1;
    }

}