package com.example.patrice.appli_03;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.Calendar;

// https://stackoverflow.com/questions/14443448/android-alarmmanager-used-to-call-a-service-to-notify-the-user

public class MainActivity extends AppCompatActivity
{


    private AlarmManager alarmManager = null;

    private PendingIntent pendingIntent = null;

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

        Local_Data obj = new Local_Data();

        obj.setNumber(10);
        int num = obj.getNumber();



        //Intent intent = new Intent(MainActivity.this, MyService.class);

        //startService(intent);
/*
        Intent myIntent = new Intent(MainActivity.this, MyService.class);

        pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();

        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.add(Calendar.SECOND, 10);

        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

        Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
*/

       // startService(new Intent(this, MyService.class));
       // Intent intent = new Intent(getApplicationContext(), MyService.class);

        Intent intent = new Intent(this, MyService.class);

        startService(intent);

        //pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        PendingIntent pendingIntent = PendingIntent .getService(this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        // Start service every 20 seconds
        long alarmStartTime = System.currentTimeMillis();
        long alarmExecuteInterval = 60*1000;

        alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime,  alarmExecuteInterval, pendingIntent);


/*
        findViewById(R.id.stop_alarm_button).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //NotificationManager notifManager= (NotificationManager) MainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
                //notifManager.cancelAll();
                cancelNotification(MainActivity.this, 0);
            }
        });
*/
    }
/*
    public static void cancelNotification(Context ctx, int notifyId)
    {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
        nMgr.cancel(notifyId);
    }
    */
}


/*   How to launch activity only once when app is opened for first time?

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
    if(!previouslyStarted) {
        SharedPreferences.Editor edit = prefs.edit();
        edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
        edit.commit();
        showHelp();
    }
*/
