package com.androidbegin.buttonclicktutorial;

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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class RepeatReceiver  extends BroadcastReceiver
{
	
	//http://stackoverflow.com/questions/7517932/difficulty-in-opening-database-in-broadcastreceiver
	
	
	DB_alarm db;
	private SQLiteDatabase dataBase;
	
	
	long id;
	String option;
		
	Ringtone ringtone;
	Vibrator vibrator;
	
	long repeat_id; 
	String repeat_titre; 
	long repeat_time;
	String repeat_days;
	String repeat_interval;
	String repeat_active;
	String repeat_vibrate;
	String repeat_ringtone;
	long repeat_created_time;
	long repeat_modified_time;
	
	@Override
	public void onReceive(Context context, Intent intent)
	{
		
			db = new DB_alarm(context);
		
			id = intent.getLongExtra("id", -1); 
			option = intent.getStringExtra("option"); 
			
			String sid = String.valueOf(id);
			String requete ="SELECT * FROM " + DB_alarm.TABLE_REPEAT  +" WHERE _id ='"+sid+"';";
			Cursor Cursor = db.DB_query(requete);
			
			int val = Cursor.getCount();
		    Log.i("***** TRACE ********** => ", String.valueOf(val));

			
			if(val != 0)
			{
					if (Cursor.moveToFirst()) 
					{
	 					do 
	 					{
	 						repeat_id = Cursor.getInt(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_ID));
							repeat_titre = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_NAME));
							repeat_time = Cursor.getLong(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_TIME));
							repeat_days = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_DAYS));
							repeat_interval = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_INTERVAL));
							repeat_active = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_ACTIVE));
							repeat_vibrate = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_VIBRATE));
							repeat_ringtone = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_RINGTONE));
							repeat_created_time = Cursor.getLong(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_CREATED_TIME));
							repeat_modified_time = Cursor.getLong(Cursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_MODIFIED_TIME));
			
								
	 					} while (Cursor.moveToNext());
	 					
					}
			}
			
			
			if(option.equals("option1") )
			{
					/*
					if(repeat_active.equals("active"))
					{		
						Toast.makeText(context, new Date().toString(),Toast.LENGTH_SHORT).show();
						if(repeat_ringtone.equals("ringtone"))
						{
						        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
						        ringtone = RingtoneManager.getRingtone(context, alarmUri);
						        ringtone.play();

						        try 
								{
									Thread.sleep(5000);
								} 
								catch (InterruptedException e)
								{
									e.printStackTrace();
								}
						        
						        ringtone.stop();
						}
					}			
			
					else
					{
						Toast.makeText(context, "Inactive",Toast.LENGTH_SHORT).show();
		
					}
					*/
					

					if(repeat_active.equals("active"))
					{
					
						Intent myIntent=new Intent(context, Repeat_ringtone.class);   
						myIntent.putExtra("titre", repeat_titre);
						myIntent.putExtra("time", repeat_time);
						myIntent.putExtra("option_vibrate", repeat_vibrate);
						myIntent.putExtra("option_ringtone", repeat_ringtone);
						
						myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
						context.startActivity(myIntent); 
						
					}
					else
					{
						Toast.makeText(context, "Inactive",Toast.LENGTH_SHORT).show();
		
					}
					
					
					
			}
			
			/*******************************************************/

			if(option.equals("option2") ) 
			{
				String plouf = "";
				
			}
			
			/*******************************************************/

			
			if(option.equals("option3") ) 
			{
					// on est quel jour de la semaine ???
			 		Calendar calendar = Calendar.getInstance();
			 		int current_day = calendar.get(Calendar.DAY_OF_WEEK);
			 		
	
			 		// convert current_day en string
			 		String str_current_day = Integer.toString(current_day);
			 		
			 		if(repeat_active.equals("active"))
					{
			 				// filtrage pour déclencher l'alarme ou pas
			 				// 1 2 3 4 5 6 7
			 				boolean found = repeat_days.contains(str_current_day);
			 				
			 				if(found == true)
			 				{
					 				if(repeat_ringtone.equals("ringtone"))
									{
									        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
									        ringtone = RingtoneManager.getRingtone(context, alarmUri);
									        ringtone.play();
			
									        try 
											{
												Thread.sleep(30000); // 30 * 1000 = 30 s
											} 
											catch (InterruptedException e)
											{
												e.printStackTrace();
											}
									        
									        ringtone.stop();
									}
			 				}		
			 			
					}
				
			}
			
			/*******************************************************/
			
	}
}

