package com.androidbegin.buttonclicktutorial;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


// http://www.edumobile.org/android/ringtone-randomizer-example/

// http://android-er.blogspot.fr/2015/06/android-example-using.html
public class Repeat_ringtone extends Activity
{
	
	 Ringtone ringtone;
	 Vibrator vibrator;
	 
	 private Button btn_stop;
	 TextView tv_titre;
		 
	 int dot = 200; // Length of a Morse Code "dot" in milliseconds
	 int dash = 500; // Length of a Morse Code "dash" in milliseconds
	 int short_gap = 200; // Length of Gap Between dots/dashes
	 int medium_gap = 500; // Length of Gap Between Letters
	 int long_gap = 1000; // Length of Gap Between Words
	 
	 long[] pattern = { 0, // Start immediately
	            dot, short_gap, dot, short_gap, dot, // s
	            medium_gap, dash, short_gap, dash, short_gap, dash, // o
	            medium_gap, dot, short_gap, dot, short_gap, dot, // s
	            long_gap };
	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
				super.onCreate(savedInstanceState);
				setContentView(R.layout.ringtone_layout);
				
				Bundle extras = getIntent().getExtras();
				String titre = extras.getString("titre");
				long time = extras.getLong("time", -1);
				final String option_vibrate = extras.getString("option_vibrate");
				final String option_ringtone = extras.getString("option_ringtone");
				
		        //SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
		        //Date netDate = (new Date(time));
		        //String str_date=  sdf.format(netDate);
		
		        //split
		        //String[] items = str_date.split(" ");
			  	// jour mois annee
			  	//String jour = items[0];
			  	//String heure = items[1];
				
				tv_titre = (TextView) findViewById(R.id.tv_titre);
		        btn_stop = (Button) findViewById(R.id.btn_stop);
		
		
		        
		        //String str = "";
		        //str = "Rendez-vous " + titre;
			 	//str = str + System.getProperty("line.separator");
			 	//str = str + " le " + jour + " à " + heure;
        
        
		        tv_titre.setText(titre);
		
				if(option_vibrate.equals("vibrate"))
				{
					 vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
					 vibrator.vibrate(300); // 300ms 
		             // Vibrates on SOS pattern
					 vibrator.vibrate(pattern, -1);  // 1 pour repeat
		
				}
				
				if(option_ringtone.equals("ringtone"))
				{
			        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
			        ringtone = RingtoneManager.getRingtone(Repeat_ringtone.this, alarmUri);
			        ringtone.play();
			  /*      
			        try 
					{
						Thread.sleep(3000); // sec
					} 
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
			        
			        ringtone.stop();
			   */     
			        
				}
		        
  
        
		        btn_stop.setOnClickListener(new OnClickListener()
		        {
		            @Override
		            public void onClick(View v) 
		            {
		            	
		        		if(option_ringtone.equals("ringtone"))
		        		{
		                	ringtone.stop();
		                	finish();
		        		}	
		            	if(option_vibrate.equals("vibrate"))
		        		{	
		                	vibrator.cancel();
		                	finish();
		        		}
		            }
		        });
		
	}	


}
