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;

public class ringtone extends Activity
{
	
	 Ringtone ringtone;
	 private Button btn_stop;
	 TextView tv_titre;
	 Vibrator vibrator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ringtone_layout);
		
		Bundle extras = getIntent().getExtras();
		String titre = extras.getString("titre");
		long delai = extras.getLong("delai", -1);
		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_delai = "";
        long min = delai / 60000;
        if(min == 15)
        {
        	str_delai = " dans 15min";
        }
        if(min == 30 )
        {
        	str_delai = " dans 30min";
        }
        
        if(min == 60 )
        {
        	str_delai = " dans1H";
        }
*/
        //String str = "Rendez-vous " + titre + " le " + jour + " à " + heure;
        
        String str = "";
        str = "Rendez-vous " + titre;
	 	str = str + System.getProperty("line.separator");
	 	str = str + " le " + jour + " à " + heure;
        
        tv_titre.setText(str);

		if(option_vibrate.equals("option_vibrate"))
		{
			 vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
			 vibrator.vibrate(300); // 300ms 
		}
		
		if(option_ringtone.equals("option_ringtone"))
		{
	        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
	        if (alarmUri == null)
	        {
	            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
	        }
	        
	        ringtone = RingtoneManager.getRingtone(ringtone.this, alarmUri);
	        ringtone.play();
		}
        
/*        
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null)
        {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        
        ringtone = RingtoneManager.getRingtone(ringtone.this, alarmUri);
        ringtone.play();
*/        
        
        
        
        
        btn_stop.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
            	
            	if(option_vibrate.equals("option_vibrate"))
        		{	
                	ringtone.stop();
                	finish();
        		}
            	
        		if(option_ringtone.equals("option_ringtone"))
        		{
        			vibrator.cancel();
        		}	
            	
            }
        });
		
		
		
		
	}	


}
