package com.androidbegin.buttonclicktutorial;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public class RepeatService extends IntentService 
{
	
	private static final String TAG = "RepeatService";
	
	public static final String POPULATE = "POPULATE";
	public static final String CREATE = "CREATE";
	public static final String CANCEL = "CANCEL";
	public static final String REPEAT = "REPEAT";
	
	
	private IntentFilter matcher;
	
	
	long alarmId;
	String option;
	long time; 
	long interval;
	
	//long time; 
	//long alarmId;
	long delai;
	//long interval;
	
	String titre;
	String option_delai;
	String option_vibrate;
	String option_ringtone;
	
	DB_alarm db;
	private SQLiteDatabase dataBase;

	
	
	public RepeatService() 
	{
		super(TAG);
		matcher = new IntentFilter();
		matcher.addAction(POPULATE);
		matcher.addAction(CREATE);
		matcher.addAction(CANCEL);
		matcher.addAction(REPEAT);  

	}

	@Override
	protected void onHandleIntent(Intent intent)
	{
		alarmId = intent.getLongExtra("ALARMID", -1);
		option = intent.getStringExtra("OPTION");
		time = intent.getLongExtra("TIME", -1);
		interval = intent.getLongExtra("INTERVAL", -1);
		
		String action = intent.getAction();
		
		if (matcher.matchAction(action)) 
		{
				if (POPULATE.equals(action))
				{
					//RemindMe.dbHelper.populate(Long.parseLong(alarmId), RemindMe.db);
					//execute(alarmId, titre, startTime);
					bidule();
				}
				
				if (CREATE.equals(action)) 
				{
					//execute(CREATE, alarmId, alarmMsgId, startTime, endTime);
					/////////////////chargement();  le 20 Mars 2017
					
					int pipo = 555;
				}
				
				
				if (CANCEL.equals(action)) 
				{
					//execute(CANCEL, alarmId, alarmMsgId, startTime, endTime);
					//RemindMe.dbHelper.shred(RemindMe.db);
					cancel();
				}			
				
				if (REPEAT.equals(action)) 
				{
					//execute(CANCEL, alarmId, alarmMsgId, startTime, endTime);
					//RemindMe.dbHelper.shred(RemindMe.db);
					repeat();
				}			

		}

		
			
			//AlarmManager alarmMgr;
	    	//PendingIntent alarmIntent;
	    	//alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
	    	//intent = new Intent(this, RepeatReceiver.class);
	    	//alarmIntent = PendingIntent.getBroadcast(this, (int) alarmId, intent, 0);
	    	//long now = System.currentTimeMillis();
		 	//long timestamp_start = now + (30 * 1000 ); // dans 30s 
	    	//int interval = 20 * 1000; // chaque minute
	    	//alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, timestamp_start, interval , alarmIntent);
	}
	
	
	/*********************************************************************/
	
	
	
	private void bidule() 
	{		
		
		
//		alarmId = intent.getLongExtra("ALARMID", -1);
//		titre = intent.getStringExtra("TITRE");
//		time = intent.getLongExtra("TIME", -1);

		
		Intent i;
		PendingIntent pi;				
		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		
		i = new Intent(this, AlarmReceiver.class);
		long keycode = alarmId; 
		i.putExtra("keycode", alarmId);
		i.putExtra("titre", titre);
		i.putExtra("time", time);
		i.putExtra("delai", delai);
		//i.putExtra("option_delai", option_delai);
		i.putExtra("option_vibrate", option_vibrate);
		i.putExtra("option_ringtone", option_ringtone);
		
		pi = PendingIntent.getBroadcast(this, (int) keycode, i, PendingIntent.FLAG_UPDATE_CURRENT);
		//long now = System.currentTimeMillis();
		//long time = now + 60 * 1000 * 2;
		am.set(AlarmManager.RTC_WAKEUP, time, pi);
	}
	
	
	public void cancel()
	{

		Intent i = null;
		PendingIntent pi;		
		
		//alarmId = 1;
		
		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		
		i = new Intent(this, RepeatReceiver.class);
		
		pi = PendingIntent.getBroadcast(this, (int) alarmId, i, PendingIntent.FLAG_UPDATE_CURRENT);
		am.cancel(pi);
		
	}	
	
	
	private void repeat() 
	{		
		
//		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/*		
		i = new Intent(this, AlarmReceiver.class);
		long keycode = alarmId; 
		i.putExtra("keycode", alarmId);
		i.putExtra("titre", titre);
		i.putExtra("time", time);
		i.putExtra("delai", delai);
		//i.putExtra("option_delai", option_delai);
		i.putExtra("option_vibrate", option_vibrate);
		i.putExtra("option_ringtone", option_ringtone);
		
		pi = PendingIntent.getBroadcast(this, (int) keycode, i, PendingIntent.FLAG_UPDATE_CURRENT);
		am.set(AlarmManager.RTC_WAKEUP, time, pi);
		*/
		
	//	PendingIntent pendingIntent;
	//    AlarmManager alarmManager;
		
/*		
		Intent intent = new Intent(this, AlarmReceiver.class);
		
		//long keycode = alarmId; 
		intent.putExtra("keycode", alarmId);
		intent.putExtra("titre", titre);
		intent.putExtra("time", time);
		intent.putExtra("delai", delai);
		//i.putExtra("option_delai", option_delai);
		intent.putExtra("option_vibrate", option_vibrate);
		intent.putExtra("option_ringtone", option_ringtone);
		
		
		
		
		pendingIntent = PendingIntent.getBroadcast(this, (int) alarmId, intent, 0);
		alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		/*alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()	+ (i * 1000), (i * 1000), pendingIntent);*/
//		alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), 8000, pendingIntent);
		
		//AlarmManager.INTERVAL_FIFTEEN_MINUTES
		
//		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, interval , pendingIntent);
	
		
		
/*		
		AlarmManager alarmMgr;
    	PendingIntent alarmIntent;

    	alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    	Intent intent = new Intent(this, RepeatReceiver.class);
    	alarmIntent = PendingIntent.getBroadcast(this, (int) alarmId, intent, 0);
    	long now = System.currentTimeMillis();
	 	long timestamp_start = now + (30 * 1000 ); // dans 30s 
    	int interval = 20 * 1000; // tous les 20 secondes
    	alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, timestamp_start, interval , alarmIntent);
    	
 */   	
    	
		
		
    	AlarmManager alarmMgr;
    	PendingIntent alarmIntent;

    	alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    	Intent intent = new Intent(this, RepeatReceiver.class);
    	
    	intent.putExtra("id", alarmId);
    	intent.putExtra("option", option);
    	
    	alarmIntent = PendingIntent.getBroadcast(this, (int) alarmId, intent, 0);
    	
    	alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time, interval , alarmIntent);
    	
    	
    /*	
    	
		Intent i;
		PendingIntent pi;				
		AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		
		i = new Intent(this, AlarmReceiver.class);
		long keycode = alarmId; 
		i.putExtra("keycode", alarmId);
		i.putExtra("titre", titre);
		i.putExtra("time", time);
		i.putExtra("delai", delai);
		//i.putExtra("option_delai", option_delai);
		i.putExtra("option_vibrate", option_vibrate);
		i.putExtra("option_ringtone", option_ringtone);
		
		pi = PendingIntent.getBroadcast(this, (int) keycode, i, PendingIntent.FLAG_UPDATE_CURRENT);
		//long now = System.currentTimeMillis();
		//long time = now + 60 * 1000 * 2;
		am.set(AlarmManager.RTC_WAKEUP, time, pi);

	*/	
    	
		
	}

	
	

	

	
}
