package com.androidbegin.buttonclicktutorial;

import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Fragment;
import android.text.format.DateFormat;
import android.widget.TextView;
import android.app.DialogFragment;
import android.app.Dialog;
import java.util.Calendar;
import android.widget.TimePicker;
import android.widget.Toast;


public class Fragment01 extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{

	String flag = "";
	
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        // horaire par defaut
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        TimePickerDialog tpd = new TimePickerDialog(getActivity(),this, hour, minute, DateFormat.is24HourFormat(getActivity()));
        tpd.setTitle("Set time");
        
        
        
        tpd.setCancelable(false);
        tpd.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",   new DialogInterface.OnClickListener() 
        {
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                    	flag = "Cancel";
                    	dialog.dismiss();
                    }
        });
        
        
        tpd.setButton(DialogInterface.BUTTON_POSITIVE, "OK",   new DialogInterface.OnClickListener() 
        {
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                    	
                    	flag = "OK";
                    	dialog.dismiss();
                    }
        });
        
        return tpd;
        
       // return new TimePickerDialog(getActivity(),this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

    
    //onTimeSet() callback method
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        TextView tv = (TextView) getActivity().findViewById(AddRepeat.TAG_TIME);
        
        // mettre au gormat xx : xx
        if(  flag.equals("OK") )
        {	
        
        	String hours = String.valueOf(hourOfDay);
        	String minutes = String.valueOf(minute);
        	String str = String.format("%02d:%02d", Integer.valueOf(hours), Integer.valueOf(minutes) );
            str = "Time : " + str;
            tv.setText(str);
        	
        }
        else
        {   
        	CharSequence val = tv.getText();
        	
        	if(!val.equals("-- : --") )
        	{
        		// ne change rien
        	}
        	else
        	{
        		tv.setText("-- : --");
                String str = "Time : " + "-- : --";
                tv.setText(str);

        	}	
        }
    }
   
}