package com.example.patrice.boot_01;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.SystemClock;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

// http://koor.fr/Java/Android/BootCompleted.wp

// https://www.intertech.com/Blog/android-bootable-background-processes/

// http://www.androidbegin.com/tutorial/android-custom-notification-tutorial/

// https://examples.javacodegeeks.com/android/core/ui/notifications/android-notifications-example/

// http://www.theappguruz.com/blog/easy-way-to-send-local-notification-to-user-in-android

// http://www.e-nature.ch/tech/tag/autostart-notificationmanager/

// http://droidmentor.com/schedule-notifications-using-alarmmanager/

// http://stacktips.com/tutorials/android/repeat-alarm-example-in-android

// https://qiita.com/datsnet/items/fc2d13ca60c004136fb7

// http://androcode.es/2012/11/android-services-34-implementacion-de-services-y-notificationmanager/  +++++

// http://www.vogella.com/tutorials/AndroidNotifications/article.html

// https://www.dev2qa.com/android-one-time-repeat-alarm-example/

// http://www.androhub.com/android-alarmmanager/

// https://github.com/ptyagicodecamp/RepeatingLocalNotifications/tree/master/app/src/main/java/org/pcc/repeatinglocalnotifications/notification

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity
{

    private Button deleteNotificationBtn;

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

        //notification(getApplicationContext(), "title", "message", 0);

       // createNotification();

        deleteNotificationBtn = (Button) findViewById(R.id.delete_notification);
        deleteNotificationBtn.setOnClickListener(new View.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(1);

                finish();
            }
        });

        alerte();

    }


    public void alerte()
    {

        /*try
        {

            Intent intent = new Intent(this, notification_display.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am =  (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),  60 * 1000 ,pendingIntent);

        }

        catch (Exception e) {}*/
        int i = 60;

        Intent intent = new Intent(this, notification_display.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
       // alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);

      //  manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

       // alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + (i * 1000), 10000 , pendingIntent);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (i * 1000), pendingIntent);

        Toast.makeText(this, "Alarm repeating in " + i + " seconds",Toast.LENGTH_LONG).show();

       // prevoir setRepeating

        // pe pas afficher la fenetre de l'activity



    }


    public void notification(Context context, String title, String message, int id_notif )
    {

        // String title = "bingoé";
        // String message = "patoche";

        // Context context = getApplicationContext();

        NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.replay)
                .setContentText(message)
               // .setColor(ContextCompat.getColor(context, android.R.color.black))
                 .setColor(101)

                //.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setSound(alarmSound);
        //  notificationManagerCompat.notify(notifId, builder.build());

        notificationManagerCompat.notify(id_notif, builder.build());

    }

    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,  100, 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")
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

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