package com.example.patrice.notification_01;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

// http://www.tutos-android.com/notifications-android

public class MainActivity extends AppCompatActivity
{
    private Button addNotificationBtn;
    private Button deleteNotificationBtn;

    private int NOTIFICATION_ID = 1;
    private  int REQUEST_CODE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addNotificationBtn = (Button) findViewById(R.id.add_notification);
        addNotificationBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                Toast.makeText(getBaseContext(), "Ajout d'une notification", Toast.LENGTH_SHORT).show();

                createNotification();
            }
        });

        deleteNotificationBtn = (Button) findViewById(R.id.delete_notification);
        deleteNotificationBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                Toast.makeText(getBaseContext(), "Suppression d'une notification", Toast.LENGTH_SHORT).show();

                final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.cancel(NOTIFICATION_ID);
            }
        });
    }

    private final void createNotification()
    {
        final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final Intent launchNotifiactionIntent = new Intent(this, MainActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(this,  REQUEST_CODE, launchNotifiactionIntent, PendingIntent.FLAG_ONE_SHOT);

        Notification.Builder builder = new Notification.Builder(this)
                .setWhen(System.currentTimeMillis())
                .setTicker("notificationTitle")
                .setSmallIcon(R.drawable.notification)
                //.setColor(Color.BLUE)
                .setContentTitle("title")
                .setContentText("desc")
                .setContentIntent(pendingIntent);

        mNotification.notify(NOTIFICATION_ID, builder.build());
    }
}