package com.androidbegin.buttonclicktutorial;

import android.app.Activity;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Space;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;



// http://www.java2s.com/Code/Java/Data-Type/Getdateofyesterday.htm


public class config_alarm extends Activity implements View.OnClickListener 
{

	private Button activeButton1 = null;
    private Button activeButton2 = null;
    private Button activeButton3 = null;
    private Button activeButton4 = null;
    private Button activeButton5 = null;
    private Button activeButton6 = null;
    private Button activeButton7 = null;
    
    String TAG_LUNDI = "";
    String TAG_MARDI = "";
    String TAG_MERCREDI = "";
    String TAG_JEUDI = "";
    String TAG_VENDREDI = "";
    String TAG_SAMEDI = "";
    String TAG_DIMANCHE = "";

    Button button1 , button2, button3 , button4, button5 , button6 , button7;
    
	DB_alarm db;
	private SQLiteDatabase dataBase;
	
	String days_list_name = "";
	String days_list_num = "";


	int array_days_config_valeur[] = new int[7];
	String array_days_config_name[] = new String[7];
	
	//aPowersOfTwo[1]=Math.pow(2,1);
	
	final int TIME_DIALOG_ID = 0;
	private int mHour;
	private int mMinute;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.test06);
        
		db = new DB_alarm(getApplicationContext());
    
		layout();
/*		
        Button btn_apply = (Button) findViewById(R.id.btn_apply);  
        btn_apply.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 						//sauver_configuration();
 						
 					 	String titre = "blabla";
 					 	long timestamp_start = 0;
 					 	String interval = "hour";
 					 	
 					 	long now = System.currentTimeMillis();
 					 	timestamp_start = now + (60 * 1000 * 5); // 5 sec
 						
 						long id = db.insert_repeat("titre1" ,  timestamp_start,  days_list_name , interval , "status", "option_vibrate", "option_ringtone" );
 					 	
						//Intent service = new Intent(config_alarm.this, AlarmService.class);
						//service.putExtra("ALARMID", id );   
						//service.putExtra("TIME", timestamp_start);
						//long interval_val = 20 * 1000; // 20 s 
						//service.putExtra("INTERVAL", interval_val);
						//service.setAction(AlarmService.REPEAT);
						//startService(service);  
						
						finish();
				        Toast.makeText(config_alarm.this,  "TRACE" , Toast.LENGTH_LONG).show();

 				}
 	    });
*/        
    }    
    
    private void traitement()
    {
    	TimePicker simpleTimePicker = (TimePicker) findViewById(R.id.timePicker);
	   	simpleTimePicker.setIs24HourView(true); // not display AM/PM mode
        
        button1 = (Button) findViewById(R.id.week_btn1);
        button2 = (Button) findViewById(R.id.week_btn2);
        button3 = (Button) findViewById(R.id.week_btn3);
        button4 = (Button) findViewById(R.id.week_btn4);
        button5 = (Button) findViewById(R.id.week_btn5);
        button6 = (Button) findViewById(R.id.week_btn6);
        button7 = (Button) findViewById(R.id.week_btn7);
        
        Calendar calendar = Calendar.getInstance();
        int current_day = calendar.get(Calendar.DAY_OF_WEEK);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        
        
    	Calendar cal_build_date = Calendar.getInstance();
    	cal_build_date.set(Calendar.YEAR, year);
    	cal_build_date.set(Calendar.DAY_OF_MONTH, day);
    	cal_build_date.set(Calendar.MONTH, month);
    	cal_build_date.set(Calendar.HOUR_OF_DAY, 0);
    	cal_build_date.set(Calendar.MINUTE, 0);
    	cal_build_date.set(Calendar.SECOND, 0);
    	cal_build_date.set(Calendar.MILLISECOND, 0);
        Date Date_date =  cal_build_date.getTime();
        long timestamp_current_date = cal_build_date.getTimeInMillis();
	
        
        //cal_build_date.add(Calendar.DAY_OF_MONTH, 3);
        //long toto = cal_build_date.getTimeInMillis();
       // Calendar cal_build_date = Calendar.getInstance();
    	
        
        // samedi 7       Sat
        // dimanche 1     Sun
        // lundi 2        Mon
        // mardi 3        Thues
        // mercredi 4     Wednes
        // jeudi 5		  Thurs
        // vendredi 6	  Fri	
        
        
        long timestamp_day = 60 * 60 * 24 * 1000; 
        long  timestamp_repeat = 0;
        long toto = 0;
        Date netDate = null;
        String aaa = "";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE dd MM yyyy");

        
        int hours = 13;
        int minutes = 47;
        long timestamp = hours * ( 60 * 60 * 1000) +   minutes * (60 * 1000 )  ; 

        //  current_day  
        int repeat_day = 5;  // Jeudi 5   
       
        
        if(current_day < repeat_day)
        {
        	int diff_day = repeat_day - current_day;
        	timestamp_repeat = timestamp_current_date + (diff_day * timestamp_day) + timestamp ;
            netDate = (new Date(timestamp_repeat));
            aaa =  sdf.format(netDate);
        	
        	//cal_build_date.add(Calendar.DATE, -1);
            //Date_date =  cal_build_date.getTime();
            //cal_build_date.add(Calendar.DAY_OF_MONTH, 3);
            //Date_date =  cal_build_date.getTime();
            //toto = cal_build_date.getTimeInMillis();
        }
        
        timestamp_current_date = 1491343200000L;
        current_day = 4; // Mer
        repeat_day = 2;  // Lun    
        
        //Mercredi 5 Avril  = 1491343200000 
        
        if(current_day > repeat_day)
        {
            int step = 7 - current_day + repeat_day;
        	timestamp_repeat = timestamp_current_date + (step * timestamp_day) + timestamp ;
        	netDate = (new Date(timestamp_repeat));
            aaa =  sdf.format(netDate);
        }
        
        
        
        current_day = 1; // Dim
        repeat_day = 7;  // Sam    
        
        //Mercredi 5 Avril  = 1491343200000 
        
        
        	int diff_day = repeat_day - current_day;

            timestamp_current_date = 1491084000000L;
        	timestamp_repeat = timestamp_current_date + (diff_day * timestamp_day) + timestamp ;
        	netDate = (new Date(timestamp_repeat));
            aaa =  sdf.format(netDate);
        
            
        current_day = 1; // Dim
        repeat_day =  1;  // DIM    
        
    	long now = System.currentTimeMillis();
    	int zzz = 0;

        
        if(current_day == repeat_day)
        {
        	timestamp_repeat = timestamp_current_date + timestamp ;
            netDate = (new Date(timestamp_repeat));
            aaa =  sdf.format(netDate);

			if(timestamp_repeat < now)
			{
				zzz = 5;
				
				// horaire dépasser 
				
				// a
			}
            
        }
        
        timestamp_repeat = timestamp_repeat + 0;
        
        int[] unsortIntArray = new int[] {7,5,4,6,1,2,3};
        Arrays.sort(unsortIntArray);
        
        
        Long time = new GregorianCalendar().getTimeInMillis();  //+24*60*60*1000;
        // samedi 7
        // dimanche 1
        // lundi 2
        // mardi 3
        // mercredi 4
        // jeudi 5
        // vendredi 6
        
        //charger_configuration();

    	
    	
    }
    
    private void layout()
    {
    	LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	
 		layMain.setPadding(8, 8, 8, 8);
 		
 		ScrollView scrollView= new ScrollView(this);
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);               
        
    	LinearLayout layMargin = createLayout(LinearLayout.HORIZONTAL);
 		LinearLayout layMargin2 = createLayout(LinearLayout.HORIZONTAL);
 		
 		layMargin.getLayoutParams().height = 18;
 		layMargin2.getLayoutParams().height = 18;
        
 		/**/
 		LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
 		/**/
 		ImageView imageView = new ImageView(this);
 		imageView.setImageResource(R.drawable.appointment);
 		LayoutParams layoutParams=new LayoutParams(75, 75);
 	    layoutParams.setMargins(10,10,0,0);
 	    imageView.setLayoutParams(layoutParams);
 		/**/
 	    ll.addView(imageView);
        mainLayout.addView(ll);
        
        /**/
 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
 		/**/
        TextView tv = new TextView(this);
        tv.setText("Evenement");
        tv.setPadding(10, 10, 0, 0);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
        
        /**/
 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
 		/**/
        final EditText editText = new EditText(this);
        layoutParams=new LayoutParams(450, 75);
        editText.setLayoutParams(layoutParams);
        editText.setSingleLine(true);
        editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20) });
        /**/
 	    ll.addView(editText);
        mainLayout.addView(ll);
        
        
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        Button btn_timer_dialog = new Button(this);
        btn_timer_dialog.setId(7755);
        btn_timer_dialog.setText("SET TIME");
        ll.addView(btn_timer_dialog);
        mainLayout.addView(ll);

        
        //https://android--examples.blogspot.fr/2015/04/timepickerdialog-in-android.html
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        /**/
 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(3); 
 		/**/
        tv = new TextView(this);
        String tmp = "Date : " + "date_event";
        tv.setText(tmp);
        tv.setPadding(10, 30, 0, 30);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
        
//		https://www.codota.com/android/methods/android.widget.TimePicker/setCurrentMinute/

/******************** Set mode *************************/        
        
        final RadioGroup rg_mode = new RadioGroup(getApplicationContext());
        rg_mode.setOrientation(RadioGroup.HORIZONTAL);
        rg_mode.setId(4321);
        
        RadioButton rb_alarm = new RadioButton(getApplicationContext());
        rb_alarm.setId(3678);
        rb_alarm.setChecked(true);
        rb_alarm.setText("Alarm");
        rb_alarm.setTextColor(Color.BLACK);
        rg_mode.addView(rb_alarm);
        
        RadioButton rb_plage = new RadioButton(getApplicationContext());
        rb_plage.setId(4678);
        rb_plage.setText("Rendez-vous");
        rb_plage.setTextColor(Color.BLACK);
        rg_mode.addView(rb_plage);
        mainLayout.addView(rg_mode);
        
/******************** Set horaire *************************/        
/*        
 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(4); 
        tv = new TextView(this);
        tv.setText("Start Time");
        tv.setPadding(10, 10, 0, 30);
 	    ll.addView(tv);
        mainLayout.addView(ll);
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        tv_set_time_start = new TextView(this);
        tv_set_time_start.setText("--:--");
        tv_set_time_start.setPadding(20, 10, 50, 30);
 	    ll.addView(tv_set_time_start);
 	    Button btn_start_time = new Button(this);
 	    btn_start_time.setId(144);
 	    btn_start_time.setText("Add");   
 	    ll.addView(btn_start_time);
        mainLayout.addView(ll);

        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(4); 
        tv_set_label_time_end = new TextView(this);
        tv_set_label_time_end.setText("End Time");
        tv_set_label_time_end.setPadding(10, 10, 0, 30);
 	    ll.addView(tv_set_label_time_end);
        mainLayout.addView(ll);
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        tv_set_time_end = new TextView(this);
        tv_set_time_end.setText("--:--");
        tv_set_time_end.setPadding(20, 10, 50, 30);
 	    ll.addView(tv_set_time_end);
 	    btn_end_time = new Button(this);
 	    btn_end_time.setId(244);
 	    btn_end_time.setText("Add");  
 	    btn_end_time.setEnabled(false);
	    ll.addView(btn_end_time);
        mainLayout.addView(ll);
*/
/******************** option alarme *************************/        
		
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setTag(8); 
 	    
 	   	final TextView tv_delay = new TextView(this);
 	   	tv_delay.setText("Delay");
 	   	tv_delay.setVisibility(View.VISIBLE); 
 	  	tv_delay.setPadding(10, 10, 0, 30);
	   	ll.addView(tv_delay);
 	    
 	    final Spinner Spinner_alarm = new Spinner(this);  
        Spinner_alarm.setId(900);
        Spinner_alarm.setPadding(30, 10, 0, 10);
        layoutParams=new LayoutParams(125, 75);
        layoutParams.width += 50;  //
        Spinner_alarm.setLayoutParams(layoutParams);
        List<String> myArraySpinner = new ArrayList<String>();
        myArraySpinner.add("0min");
        myArraySpinner.add("15min");
        myArraySpinner.add("30min");     
        myArraySpinner.add("1H");
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, myArraySpinner);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        Spinner_alarm.setAdapter(spinnerArrayAdapter);
        Spinner_alarm.setVisibility(View.VISIBLE); 
 	    ll.addView(Spinner_alarm);
        mainLayout.addView(ll);
/*        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setTag(18); 
 	    checkBox_option_vibrate = new CheckBox(this);
 	    checkBox_option_vibrate.setId(850);
 	    checkBox_option_vibrate.setText("Vibrate");
 	    checkBox_option_vibrate.setPadding(20, 10, 50, 10);
 	    ll.addView(checkBox_option_vibrate);

 	    checkBox_option_ringtone = new CheckBox(this);
 	    checkBox_option_ringtone.setId(870);
 	    checkBox_option_ringtone.setText("Ringtone");
 	    checkBox_option_ringtone.setPadding(20, 10, 50, 10);
 	    ll.addView(checkBox_option_ringtone);
 	    mainLayout.addView(ll);
*/ 	    
 	    
        final RadioGroup rg = new RadioGroup(getApplicationContext());
        rg.setOrientation(RadioGroup.HORIZONTAL);
        rg.setId(1234);
        RadioButton rb_ringtone = new RadioButton(getApplicationContext());
        rb_ringtone.setId(1678);
        rb_ringtone.setChecked(true);
        rb_ringtone.setText("Ringtone");
        rb_ringtone.setTextColor(Color.BLACK);
        rg.addView(rb_ringtone);
        RadioButton rb_vibrate = new RadioButton(getApplicationContext());
        rb_vibrate.setId(2678);
        rb_vibrate.setText("Vibrate");
        rb_vibrate.setTextColor(Color.BLACK);
        rg.addView(rb_vibrate);
        mainLayout.addView(rg);
        
/******************** Bouton *************************/        
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setGravity(Gravity.CENTER);
       
        Button b1 = new Button(this);
        b1.setId(1);
        b1.setText("Ajouter");
        ll.addView(b1);
        
        Button b2 = new Button(this);
        b2.setId(2);
        b2.setText("Exit");
        ll.addView(b2);

        mainLayout.addView(ll);
        
        /** appliquer le code dans la vue ***/
        scrollView.addView(mainLayout);
        layMain.addView(scrollView);
        setContentView(layMain);
        
        // dialogFragment

        //http://www.journaldev.com/9976/android-date-time-picker-dialog
        // http://www.techiecommunity.net/Android/android-TimePickerDialog-example
        // http://www.ahotbrew.com/android-timepicker-example/
        //https://examples.javacodegeeks.com/android/core/ui/timepicker/android-time-picker-example/
        
        // http://android-coding.blogspot.fr/2012/07/dialogfragment-with-interface-to-pass.html
        
        btn_timer_dialog = (Button) findViewById(7755);
        btn_timer_dialog.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					//finish();
 			    	Toast.makeText(getApplicationContext(), "Timer set" , Toast.LENGTH_SHORT).show();
 			    	
 			    	String DIALOG_TIME = "MainActivity.TimeDialog";
 			    	
 			    	debug_fragment pipo = new debug_fragment();
 	                pipo.show(getFragmentManager(), DIALOG_TIME);
 	            }
 			    	
 				  //  DialogFragment newFragment = new TimePickerFragment();
 				  //  newFragment.show(getFragmentManager(), "timePicker");


 				
 	    });

        
        
        
        Button btn_apply = (Button) findViewById(1);
        btn_apply.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					//finish();
 			    	Toast.makeText(getApplicationContext(), "Apply" , Toast.LENGTH_SHORT).show();

 				}
 	    });
        
        // http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/
        //http://www.appsrox.com/android/instachat/
        // https://www.learnhowtoprogram.com/android/user-interface-basics-637d41b1-35dc-400a-bcc3-65794760474d/working-with-simple-fragments-dialogfragment
        
        Button btn_quitter = (Button) findViewById(2);
        btn_quitter.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					//finish();
 			    	//Toast.makeText(getApplicationContext(), "Quitter" , Toast.LENGTH_SHORT).show();

 					 DialogFragment alertDialExample = new dialog_fragment();
 	                alertDialExample.show(getFragmentManager(), "AlertDialogFragment");
 	                
 	            //   AddItemDialog dialog = new AddItemDialog();
 	            //  dialog.show(getSupportFragmentManager(), "AddItemDialog");
 					
 				}
 	    });

        
    	
    }
    
    
   
    
    
    
 
    
 /*   
    private class mAlarmTimeSetListener implements OnTimeSetListener {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            edTxt.setText(hourOfDay + ":" + minute);
            timePickDialog.hide();

        }

    }
   */ 
    
    private void charger_configuration()
    {
        
    		int[] days = { 1,1,1,1,1,1,1}; // first is sunday, last is saturday
    		
    		if( days[0] == 1)
			{
    			button1.setSelected(true);
    			activeButton1 = button1;
			}	
    		if( days[1] == 1)
			{
    			button2.setSelected(true);
    			activeButton2 = button2;
			}
    		if( days[2] == 1)
			{
    			button3.setSelected(true);
    			activeButton3 = button3;
			}
    		if( days[3] == 1)
			{
    			button4.setSelected(true);
    			activeButton4 = button4;
			}	
    		if( days[4] == 1)
			{
    			button5.setSelected(true);
    			activeButton5 = button5;
			}
    		if( days[5] == 1)
			{
    			button6.setSelected(true);
    			activeButton6 = button6;
			}
    		if( days[6] == 1)
			{
    			button7.setSelected(true);
    			activeButton7 = button7;
			}	
    }
    
    private void sauver_configuration()
    {
    	// lire l'état des boutons
    	
    	if (activeButton1 != null)
 		{
    		days_list_name = days_list_name + "LUN "; // 2
    		days_list_num = days_list_num + "2 "; 	
    		array_days_config_valeur[0]= 2 ;
    		array_days_config_name [0]= "LUN";
 		}
    	
    	if (activeButton2 != null)
 		{
    		days_list_name = days_list_name + "MAR "; // 3
    		days_list_num = days_list_num + "3 "; 	
    		array_days_config_valeur[1]= 3 ;
    		array_days_config_name [1]= "MAR";
 		}
    	
    	if (activeButton3 != null)
 		{
    		days_list_name = days_list_name + "MER "; // 4
    		days_list_num = days_list_num + "4 "; 	
    		array_days_config_valeur[2]= 4 ;
    		array_days_config_name [2]= "MER";
 		}
    	
    	if (activeButton4 != null)
 		{
    		days_list_name = days_list_name + "JEU ";  // 5
    		days_list_num = days_list_num + "5 "; 	
    		array_days_config_valeur[3]= 5 ;
    		array_days_config_name [3]= "JEU";
 		}
    
    	if (activeButton5 != null)
 		{
    		days_list_name = days_list_name + "VEN ";  // 6
    		days_list_num = days_list_num + "6 "; 	
    		array_days_config_valeur[4]= 6 ;
    		array_days_config_name [4]= "VEN";
 		}
    	
    	if (activeButton6 != null)
 		{
    		days_list_name = days_list_name + "SAM ";   // 7
    		days_list_num = days_list_num + "7 "; 	
    		array_days_config_valeur[5]= 7 ;
    		array_days_config_name [5]= "SAM";
 		}
    	
    	if (activeButton7 != null)
 		{
    		days_list_name = days_list_name + "DIM";  // 1
    		days_list_num = days_list_num + "1"; 	
    		array_days_config_valeur[6]= 1 ;
    		array_days_config_name [6]= "DIM";
 		}
  
    	// supprimer les espaces left et right
    	days_list_name = days_list_name.trim();
    	
    	long id = db.insert_repeat("titre1" ,  123456,  days_list_name , "inverval", "status", "option_vibrate", "option_ringtone" );
    	id = db.insert_repeat("titre2" ,  4645,  days_list_name , "inverval", "status", "option_vibrate", "option_ringtone" );
    	id = db.insert_repeat("titre3" ,  10000,  days_list_name , "inverval", "status", "option_vibrate", "option_ringtone" );
    	id = db.insert_repeat("titre4" ,  5664,  days_list_name , "inverval", "status", "option_vibrate", "option_ringtone" );
	    
    	Toast.makeText(getApplicationContext(), "Sauvegarde de l'alarme repeat" , Toast.LENGTH_SHORT).show();

    }
    
    private LinearLayout createLayout(int iOrientation)
	  {
			LinearLayout lay = new LinearLayout(this);		
			lay.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			lay.setOrientation(iOrientation);
			return lay;
	  }
	
    
    private void gui3()
    {
    	
    	
    }
    
    private void gui2()
    {
    	/*
    	   <ToggleButton
           android:id="@+id/toggleButton2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textOn="on"
           android:textOff="off"
           android:checked="true" />   
        */
    	
    	LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	

 		LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);

    	final RadioButton[] rb = new RadioButton[5];
    	final ToggleButton[] tb = new ToggleButton[5];
    	
    	
        RadioGroup rg = new RadioGroup(this); //create the RadioGroup
        rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
        for(int i=0; i<5; i++){
            
        	//rb[i]  = new RadioButton(this);
            //rb[i].setText("a " + i);
            //rb[i].setId(i);
            //rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
            
            tb[i] = new ToggleButton(this);
            tb[i].setTextOn("AA");
            tb[i].setChecked(false);
            tb[i].setId(i);
            rg.addView(tb[i]);

        }
        ll.addView(rg);//you add the whole RadioGroup to the layout
        
 	    layMain.addView(ll);
        setContentView(layMain);

       
        // add Toggle button
//       ToggleButton tb = new ToggleButton(this);
//        tb.setTextOn("Dynamic Toggle Button - ON");
//        tb.setTextOff("Dynamic Toggle Button - OFF");
//        tb.setChecked(true);
//        tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//        ll.addView(tb);
    	
    	
    }
    
    public void  clickEvent(View v)
    {

    	 switch (v.getId())
    	 {

    	 	case R.id.week_btn1:
	      		if (activeButton1 != null)
	     		{
	                 button1.setSelected(false);
	                 activeButton1 = null;
	             }
	             else
	             {	
	             	activeButton1 = button1;
	             	button1.setSelected(true);
	             }
	      		break;
    	 
    	 	case R.id.week_btn2:
         		if (activeButton2 != null)
        		{
                    button2.setSelected(false);
                    activeButton2 = null;
                }
                else
                {	
                	activeButton2 = button2;
                	button2.setSelected(true);
                }
         		break;

         	case R.id.week_btn3:
        		if (activeButton3 != null)
        		{
                    button3.setSelected(false);
                    activeButton3 = null;
                }
                else
                {	
                	activeButton3 = button3;
                	button3.setSelected(true);
                }
         		break;
         
         	case R.id.week_btn4:
        		if (activeButton4 != null)
        		{
                    button4.setSelected(false);
                    activeButton4 = null;
                }
                else
                {	
                	activeButton4 = button4;
                	button4.setSelected(true);
                }
        		break;	
         	
         	case R.id.week_btn5:
        		if (activeButton5 != null)
        		{
                    button5.setSelected(false);
                    activeButton5 = null;
                }
                else
                {	
                	activeButton5 = button5;
                	button5.setSelected(true);
                }
        		break;
        		
         	case R.id.week_btn6:
        		if (activeButton6 != null)
        		{
                    button6.setSelected(false);
                    activeButton6 = null;
                }
                else
                {	
                	activeButton6 = button6;
                	button6.setSelected(true);
                }
        		break;
        		
         	case R.id.week_btn7:
        		if (activeButton7 != null)
        		{
                    button7.setSelected(false);
                    activeButton7 = null;
                }
                else
                {	
                	activeButton7 = button7;
                	button7.setSelected(true);
                }
        		break;	

     } 
/*    	
    	switch(element)
    	{
        	case 1:
        		if (activeButton1 != null) 
        		{
        			button1.setSelected(false);
                    activeButton1 = null;
                }
                else
                {	
                	activeButton1 = button1;
                	button1.setSelected(true);
                }
        		break;

        	case 2:
        		if (activeButton2 != null)
        		{
                    button2.setSelected(false);
                    activeButton2 = null;
                }
                else
                {	
                	activeButton2 = button2;
                	button2.setSelected(true);
                }
        		break;
        	case 3:
        		if (activeButton3 != null)
        		{
                    button3.setSelected(false);
                    activeButton3 = null;
                }
                else
                {	
                	activeButton3 = button3;
                	button3.setSelected(true);
                }
        		break;	
        	case 4:
        		if (activeButton4 != null)
        		{
                    button4.setSelected(false);
                    activeButton4 = null;
                }
                else
                {	
                	activeButton4 = button4;
                	button4.setSelected(true);
                }
        		break;	
        	case 5:
        		if (activeButton5 != null)
        		{
                    button5.setSelected(false);
                    activeButton5 = null;
                }
                else
                {	
                	activeButton5 = button5;
                	button5.setSelected(true);
                }
        		break;
        	case 6:
        		if (activeButton6 != null)
        		{
                    button6.setSelected(false);
                    activeButton6 = null;
                }
                else
                {	
                	activeButton6 = button6;
                	button6.setSelected(true);
                }
        		break;
        	case 7:
        		if (activeButton7 != null)
        		{
                    button7.setSelected(false);
                    activeButton7 = null;
                }
                else
                {	
                	activeButton7 = button7;
                	button7.setSelected(true);
                }
        		break;	
    	}
*/    	
    }	

    	
    
    
    private void gui()
    {
    	
        int buttonsSpacing = (int) getResources().getDimension(R.dimen.activity_horizontal_margin);
        int buttonSize = (int) getResources().getDimension(R.dimen.button_size);
    	
    	LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	
 		layMain.setPadding(58, 8, 8, 8);
 		
 		LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        TextView tv = new TextView(this);
        tv.setText("Evenement");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
 	    layMain.addView(ll);

 	    ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        tv = new TextView(this);
        tv.setText("sfffsq");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
 	    layMain.addView(ll);


 	    ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        tv = new TextView(this);
        tv.setText("zero");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
 	    layMain.addView(ll);

 	    /******* ligne 1 **********/ 
 	    
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
        button1 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button1.setId(1);
        button1.setText("LUN.");
        button1.setOnClickListener(this);
        ll.addView(button1);
        
        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button2 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button2.setId(2);
        button2.setText("MAR.");
        button2.setOnClickListener(this);
        ll.addView(button2);
	    
	    ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button3 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button3.setId(3);
        button3.setText("MER.");
        button3.setOnClickListener(this);
        ll.addView(button3);
	    
	    ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button4 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button4.setId(4);
        button4.setText("JEU.");
        button4.setOnClickListener(this);
        ll.addView(button4);
	    layMain.addView(ll);
	    
	    /************* SAUT ****************/
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
 		ImageView imageView = new ImageView(this);
 		LayoutParams layoutParams=new LayoutParams(20, 20);
  	    // layoutParams.setMargins(10,10,10,100);
 	    imageView.setLayoutParams(layoutParams);
 	    ll.addView(imageView);
 	    layMain.addView(ll);
        
	    /******* ligne 2 **********/ 
	    
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.HORIZONTAL);
        button5 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button5.setId(5);
        button5.setText("VEN.");
        button5.setOnClickListener(this);
        ll.addView(button5);
        
        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button6 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button6.setId(6);
        button6.setText("SAM.");
        button6.setOnClickListener(this);
        ll.addView(button6);
        
        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));
        
        button7 = (Button) getLayoutInflater().inflate(R.layout.circularbutton_layout, ll ,false);
        button7.setId(7);
        button7.setText("DIM.");
        button7.setOnClickListener(this);
        ll.addView(button7);
        
//        ll.addView(new Space(this), new ViewGroup.LayoutParams(buttonsSpacing, buttonSize));

	    
	    /************* SAUT ****************/
//        ll = new LinearLayout(this);
//       	ll.setOrientation(LinearLayout.VERTICAL);
// 		imageView = new ImageView(this);
// 		layoutParams=new LayoutParams(20, 100);
  	    // layoutParams.setMargins(10,10,10,100);
// 	    imageView.setLayoutParams(layoutParams);
// 	    ll.addView(imageView);

	    
	    // btn valide
/* 	    
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.VERTICAL);
	    Button btn_valide = new Button(this);
	    layoutParams=new LayoutParams(20, 100);
	    btn_valide.setLayoutParams(layoutParams);
	    btn_valide.setId(144);
	    btn_valide.setText("Add");   
 	    ll.addView(btn_valide);
*/
/*        
 	    ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        tv = new TextView(this);
        tv.setText("00000zero");
        tv.setPadding(10, 40, 40, 40);
 	    ll.addView(tv);
*/
        
 	    // insertion
 	    layMain.addView(ll);
        setContentView(layMain);

    }
	
    @Override
    public void onClick(View view) 
    {
    	int element = view.getId();
    	
    	switch(element)
    	{
        	case 1:
        		if (activeButton1 != null) 
        		{
                    button1.setSelected(false);
                    activeButton1 = null;
                }
                else
                {	
                	activeButton1 = button1;
                	button1.setSelected(true);
                }
        		break;

        	case 2:
        		if (activeButton2 != null)
        		{
                    button2.setSelected(false);
                    activeButton2 = null;
                }
                else
                {	
                	activeButton2 = button2;
                	button2.setSelected(true);
                }
        		break;
        	case 3:
        		if (activeButton3 != null)
        		{
                    button3.setSelected(false);
                    activeButton3 = null;
                }
                else
                {	
                	activeButton3 = button3;
                	button3.setSelected(true);
                }
        		break;	
        	case 4:
        		if (activeButton4 != null)
        		{
                    button4.setSelected(false);
                    activeButton4 = null;
                }
                else
                {	
                	activeButton4 = button4;
                	button4.setSelected(true);
                }
        		break;	
        	case 5:
        		if (activeButton5 != null)
        		{
                    button5.setSelected(false);
                    activeButton5 = null;
                }
                else
                {	
                	activeButton5 = button5;
                	button5.setSelected(true);
                }
        		break;
        	case 6:
        		if (activeButton6 != null)
        		{
                    button6.setSelected(false);
                    activeButton6 = null;
                }
                else
                {	
                	activeButton6 = button6;
                	button6.setSelected(true);
                }
        		break;
        	case 7:
        		if (activeButton7 != null)
        		{
                    button7.setSelected(false);
                    activeButton7 = null;
                }
                else
                {	
                	activeButton7 = button7;
                	button7.setSelected(true);
                }
        		break;	
    	}
    	
    }	

}
