package com.example.alarm09;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;



import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity 
{
	
	TimePicker myTimePicker;
	Button buttonstartSetDialog;
	Button buttonCancelAlarm;
	TextView textAlarmPrompt;
	
	TimePicker timePicker;
	CheckBox optRepeat;
	
	final static int RQS_1 = 1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        timePicker = (TimePicker)findViewById(R.id.picker);
	        optRepeat = (CheckBox)findViewById(R.id.optrepeat);
	        textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);
	        
	        buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
	        buttonstartSetDialog.setOnClickListener(new OnClickListener()
	        {
					@Override
					public void onClick(View v) 
					{
						Calendar calNow = Calendar.getInstance();
						Calendar calSet = (Calendar) calNow.clone();
		
						calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
						calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
						calSet.set(Calendar.SECOND, 0);
						calSet.set(Calendar.MILLISECOND, 0);
						
						if(calSet.compareTo(calNow) <= 0)
						{
							//Today Set time passed, count to tomorrow
							calSet.add(Calendar.DATE, 1);
						}
						
						setAlarm(calSet, optRepeat.isChecked());
					}
				});
	
	        buttonCancelAlarm = (Button)findViewById(R.id.cancel);
	        buttonCancelAlarm.setOnClickListener(new OnClickListener()
	        {
					@Override
					public void onClick(View arg0)
					{
						cancelAlarm();
					}
			});
        
    }

	private void setAlarm(Calendar targetCal, boolean repeat){
		
		/*Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
		AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
		alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

		Intent i;
		PendingIntent pi;				
		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		i = new Intent(this, AlarmReceiver.class);
		pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
		*/
		
		Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
		
		if(repeat)
		{
            long firstTime = SystemClock.elapsedRealtime();
            firstTime += 5*1000;
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  firstTime, 5*1000, sender);
			
			textAlarmPrompt.setText(
					"\n\n***\n"
					+ "Alarm is set@ " + targetCal.getTime() + "\n"
					+ "Repeat every 5 minutes\n"
					+ "***\n");
		}
		else
		{
			/*am.set(AlarmManager.RTC_WAKEUP, 
					targetCal.getTimeInMillis(),  
					PendingIntent);
			*/
			textAlarmPrompt.setText(
					"\n\n***\n"
					+ "Alarm is set@ " + targetCal.getTime() + "\n"
					+ "One shot\n"
					+ "***\n");
		}

	}
	
	private void cancelAlarm()
	{

		textAlarmPrompt.setText(
				"\n\n***\n"
				+ "Alarm Cancelled! \n"
				+ "***\n");
		
		//Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
		//PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
		//AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
		//armManager.cancel(pendingIntent);
		
		Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this,  0, intent, 0);
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.cancel(sender);

	}
	
//	Intent intent = new Intent(load.this, AlarmReceiver.class);
//    final int _id = (int) System.currentTimeMillis();
//    PendingIntent appIntent = PendingIntent.getBroadcast(this, _id, intent,PendingIntent.FLAG_ONE_SHOT);
	
	
//	String myAlarmId = ...;

//	Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
//	intent.setData(Uri.parse("myalarms://" + myAlarmId));
	
	
//	for (int id = 0; id < 3; id++) {
//        // Define pendingintent
//        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id,ntent, 0); 
        // set() schedules an alarm 
//        alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
	
	
//	requestCode
	
	
/*
	AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
	ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

	for(i = 0; i < 10; ++i)
	{
	   Intent intent = new Intent(context, OnAlarmReceiver.class);
	   // Loop counter `i` is used as a `requestCode`
	   PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
	   // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
	   mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
	                SystemClock.elapsedRealtime() + 60000 * i, 
	                pendingIntent); 

	   intentArray.add(pendingIntent);
	}
*/	

	//  android set multiple alarms simultaneosuly	
	// Set multiple alarms
/*	
	public static int getAlarmId(Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        int alarmId = preferences.getInt("ALARM", 1);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("ALARM", alarmId + 1).apply();
        return alarmId;
}
*/
	
	// http://divine4android.blogspot.fr/2013/09/android-set-multiple-alarms.html
	// http://www.concretepage.com/android/android-alarm-clock-tutorial-to-schedule-and-cancel-alarmmanager-pendingintent-and-wakefulbroadcastreceiver-example
	// http://android-er.blogspot.fr/2012/05/create-alarm-set-on-specified-time.html
	
	
}