package com.example.patrice.rerservation01;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class alarme01 extends AppCompatActivity
{
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Name = "nameKey";
    public static final String Phone = "phoneKey";
    public static final String Email = "emailKey";

    private static final String PREFS_AGE = "PREFS_AGE";
    private static final String PREFS_NAME = "PREFS_NAME";

    SharedPreferences sharedpreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm01_layout);

/*
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);


        SharedPreferences sharedpreferences = null;


        SharedPreferences.Editor editor = sharedpreferences.edit();

        editor.putString(Name, "n");
        editor.putString(Phone, "ph");
        editor.putString(Email, "e");
        editor.commit();
*/
        sharedpreferences = getBaseContext().getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);

        if (sharedpreferences.contains(PREFS_AGE) && sharedpreferences.contains(PREFS_NAME))
        {
            int age = sharedpreferences.getInt(PREFS_AGE, 0);
            String name = sharedpreferences.getString(PREFS_NAME, null);
            Toast.makeText(this, "Age: " + age + " name: " + name, Toast.LENGTH_SHORT).show();
        }

        else
        {
            sharedpreferences
                    .edit()
                    .putInt(PREFS_AGE, 24)
                    .putString(PREFS_NAME, "florent")
                    .apply();

            // ou bien

            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString(PREFS_NAME, "test value");
            editor.commit();

            Toast.makeText(this, "Sauvegardé, relancez l'application pour voir le résultat", Toast.LENGTH_SHORT).show();
        }

        String ts = getCurrentTimeStamp();

        int time = (int) (System.currentTimeMillis());
        Timestamp tsTemp = new Timestamp(time);
        String ts2 =  tsTemp.toString();

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        long ts03 = timestamp.getTime();

        int jour = 28;
        int mois = 0;
        int annee = 2018;

        Calendar cal_date = Calendar.getInstance();
        cal_date.set(Calendar.YEAR, annee);
        cal_date.set(Calendar.DAY_OF_MONTH, jour);
        cal_date.set(Calendar.MONTH, mois);
        cal_date.set(Calendar.HOUR_OF_DAY, 0);
        cal_date.set(Calendar.MINUTE, 0);
        cal_date.set(Calendar.SECOND, 0);
        cal_date.set(Calendar.MILLISECOND, 0);
        Date Date_date =  cal_date.getTime();
        long timestamp_millis = cal_date.getTimeInMillis();

        long timestamp_secs = timestamp_millis / 1000;


      //  save_preferences();

      //  get_preferences();

    }

/*
    public void save_preferences()
    {
        String n = "toto";
        String e = "lulu";
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.putString(Email, e);
        editor.commit();
    }
/*
    public void get_preferences()
    {
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Name))
        {
           String val1 = sharedpreferences.getString(Name, "");
        }

        if (sharedpreferences.contains(Email))
        {
            String val2 = sharedpreferences.getString(Email, "");

        }
    }
/*
    public void clear_preferences()
    {

        editor.remove("name"); // will delete key name
        editor.remove("email"); // will delete key email

        editor.commit(); // commit changes


        editor.clear();
        editor.commit(); // commit changes

    }
*/

    public String getCurrentTimeStamp()
    {
        try
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentDateTime = dateFormat.format(new Date()); // Find todays date

            return currentDateTime;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public void sendNotification(View view)
    {

        //Get an instance of NotificationManager//

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.notifications) // notification_icon
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");


        // Gets an instance of the NotificationManager service//

        NotificationManager mNotificationManager =  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // When you issue multiple notifications about the same type of event,
        // it’s best practice for your app to try to update an existing notification
        // with this new information, rather than immediately creating a new notification.
        // If you want to update this notification at a later date, you need to assign it an ID.
        // You can then use this ID whenever you issue a subsequent notification.
        // If the previous notification is still visible, the system will update this existing notification,
        // rather than create a new one. In this example, the notification’s ID is 001//

//        NotificationManager.notify().
                mNotificationManager.notify(001, mBuilder.build());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_alarm01, menu);
        return true;
    }


    //https://gist.github.com/BrandonSmith/6679223
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.seconds5:
                scheduleNotification(getNotification("5 second delay"), 5000);
                return true;
            case R.id.seconds10:
                scheduleNotification(getNotification("10 second delay"), 10000);
                return true;
            case R.id.seconds30:
                scheduleNotification(getNotification("30 second delay"), 30000);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void scheduleNotification(Notification notification, int delay)
    {

        Intent notificationIntent = new Intent(this, NotificationPublisher.class);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        long futureInMillis = SystemClock.elapsedRealtime() + delay;
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
    }

    private Notification getNotification(String content)
    {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Scheduled Notification");
        builder.setContentText(content);
        builder.setSmallIcon(R.drawable.notifications);
        return builder.build();
    }


}