package com.androidbegin.buttonclicktutorial;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CheckBox;

public class AddEvent02 extends Activity //implements OnClickListener
{

	private Button btnStartTime = null;
	private Calendar dateStart = null;
	
	private String date_event;
	
    
    LinearLayout layout1;
    private static int viewsCount = 0;
    private List<View> allViews = new ArrayList<View>();
    LinearLayout.LayoutParams params;
    
    
	//fields
		private static String sStrSelect = "Select time";
		private static String sStrSet = "set";
		private static String sStrNone = "none";
		
		//fields
		public static final int SELECT_TIME_REQUEST = 112;
		public static final int iSliderViewWidth = 276;
		private static final int iSmallButtonWidth = 100;	
		
		//fields
		private LinearLayout layContent = null;
		private LinearLayout layContent2 = null;
		
		
		private Button btnNone = null;
		private Button btnOK = null;
		
		//fields
		private boolean bNoneButton = true;
		private boolean b24HourMode = false;
		private int start_hour = -1;
		private int start_minutes = -1;
		private int end_hour = -1;
		private int end_minutes = -1;
		
		
		
		private TextView infos; 
		CheckBox checkBox;
		
		TimeWidgetSlider start_timeSliderHour = null;
		TimeWidgetSlider start_timeSliderMinutes = null;
		TimeWidgetSlider end_timeSliderHour = null;
		TimeWidgetSlider end_timeSliderMinutes = null;
		
		TextView tv_set_time_start;
		TextView tv_set_time_end;
		
		DB_alarm db;
		private SQLiteDatabase dataBase;
		
		ArrayList<appointment> appointment_liste = new ArrayList<appointment>();
		String option_alarm;
		
	    static final int TIME_DIALOG_START = 1200;
	    static final int TIME_DIALOG_END = 1300;
	    	 
		int hour;
		int minute;
		
		int global_start_hour = 0; 
		int global_start_min = 0;

		int global_end_hour = 0; 
		int global_end_min = 0;

		
		
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);

		db = new DB_alarm(getApplicationContext());

		Bundle extras = getIntent().getExtras();
		date_event = extras.getString("EXTRA");
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy"); 
   	   	Date date = null;
   	   	try
	    {
   	   		date = sdf.parse(date_event);
		} 
	    catch (ParseException e)
	    {
			e.printStackTrace();
		}
	      
   	   	long toto = date.getTime();
		
        Toast.makeText(getBaseContext(),  date_event , Toast.LENGTH_LONG).show();

        // setContentView(widget());
        ActionBar actionBar = getActionBar();
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("Add rendez-vous");
        
        //http://alvinalexander.com/source-code/android/how-get-android-actionbar-backup-button-work-android-back-button
        //http://www.theappguruz.com/blog/android-working-android-actionbar
        
        layout(); 
        
        //	composants();
        //	v3();
		
/*		
		btnStartTime = (Button)findViewById(R.id.button1);  	
		
		btnStartTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				
	    	//TimeWidget.Open(MainActivity.this, false, false, dateStart.get(Calendar.HOUR_OF_DAY), dateStart.get(Calendar.MINUTE));
	    	//TimeWidget.Open(MainActivity.this, false, false, 14 , 10);
				
				//widget();
				
			}
		});
*/		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) { 
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            // app icon in action bar clicked; go home
	           // return true;
	           // default:
	           // return super.onOptionsItemSelected(item); 
	            
	        	//CalendarCustomView.debug();
	        	
	        	
	            this.finish();
	            return true;
	            default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public LinearLayout createLayout(int iOrientation)
	  {
			LinearLayout lay = new LinearLayout(this);		
			lay.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			lay.setOrientation(iOrientation);
			return lay;
	  }
	
	 public Button createButton(String sText, int iWidth, int iHeight)
	  {
	  	Button btn = new Button(this);
	  	btn.setText(sText);
	  	btn.setLayoutParams(new LayoutParams(iWidth, iHeight));
	  	return btn;
	  }

	  public TextView createLabel(String sText, int iWidth, int iHeight)
	  {
	  	TextView label = new TextView(this);
	  	label.setText(sText);
	  	label.setLayoutParams(new LayoutParams(iWidth, iHeight));
	  	return label;
	  }
	  
	 /* private void getDataFromSliders()
	  {
	  	start_hour = timeSliderHour.getValue();
	  	start_minutes = timeSliderMinutes.getValue();
	  }
	*/
	 public void generateBottomButtons(LinearLayout layBottomControls)
	  {
	  	TextView labMargin = createLabel("Toto", 8, android.view.ViewGroup.LayoutParams.FILL_PARENT);

	  	btnNone = createButton(sStrNone, iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);  	
	  	btnNone.setBackgroundResource(android.R.drawable.btn_default_small);
	  	
	  	btnOK = createButton(sStrSet, iSmallButtonWidth, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);  	
	  	btnOK.setBackgroundResource(android.R.drawable.btn_default_small);
	  	
	  	//set events
	  	btnNone.setOnClickListener(new Button.OnClickListener()
	  	{
				public void onClick(View arg0) {
					clearTime();
					OnClose();
			}});

	  	btnOK.setOnClickListener(new Button.OnClickListener()
	  	{
				public void onClick(View arg0)
				{
					//getDataFromSliders();
					OnClose();
			}});
	  	
	  	layBottomControls.setGravity(Gravity.CENTER_HORIZONTAL);  	
	  	if (bNoneButton)
	  	{
	  		layBottomControls.addView(btnNone);
	  		layBottomControls.addView(labMargin);
	  	}
	  	layBottomControls.addView(btnOK);
	  }
	
	 
	 public void composants()
	{
/*		
		 
		 LinearLayout parent = new LinearLayout(this);

		// parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		 
		 
		 LayoutParams linearlayoutlayoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
		 parent.setOrientation(LinearLayout.VERTICAL);

		 
		 
		 setContentView(parent, linearlayoutlayoutparams);
	        // add text view
	        TextView tv = new TextView(this);
	        tv.setText("Dynamic Text!");
	        parent.addView(tv);
	 
	        // add edit text
	        linearlayoutlayoutparams  = new LinearLayout.LayoutParams(
	                LinearLayout.LayoutParams.WRAP_CONTENT,
	                LinearLayout.LayoutParams.WRAP_CONTENT);
	             
	        
	        EditText et = new EditText(this);
	        et.setText("Dynamic EditText!");
	        et.setMinLines(1);
	        et.setMaxLines(3);
	        parent.addView(et); // , linearlayoutlayoutparams);
*/	        
	        
	        
	        
	  
	        //LinearLayout myLInearLayout = null;
	        TextView valueTV;
	        Button valueB;
	        
	        LinearLayout myLInearLayout = new LinearLayout(this);	         
	        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
	        myLInearLayout.setOrientation(LinearLayout.VERTICAL);
	        
	        
	        LayoutParams lparams = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	        
	        setContentView(myLInearLayout, lparams);
		    
	         
	        //add textView
	        valueTV = new TextView(this);
	        valueTV.setText("The developer ");
	        valueTV.setId(5);
	        valueTV.setX(50);
	        valueTV.setY(415);
	        
	       //** valueTV.setPadding(200, 20, 20, 20);
	        
	        //valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
	        
	       // valueTV.setLayoutParams(params);
	         
	        // add Button
	        valueB = new Button(this);
	        valueB.setText("thedeveloperworldisyours");
	        valueB.setId(55);
	         
	        //add the textView and the Button to LinearLayout
	        myLInearLayout.addView(valueTV);
	       // myLInearLayout.addView(valueB);
	        
	}
	 
	 /**********************************************/	 
	public void v2()
	{
		LinearLayout linLayout = new LinearLayout(this);
        linLayout.setOrientation(LinearLayout.VERTICAL);
        LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
        setContentView(linLayout, linLayoutParam);
         
        LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         
        TextView tv = new TextView(this);
        tv.setText("TextView");
        tv.setLayoutParams(lpView);
        linLayout.addView(tv);
         
        Button btn = new Button(this);
        btn.setText("Button");
        linLayout.addView(btn, lpView);
 
         
        LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftMarginParams.leftMargin = 50;
         
        Button btn1 = new Button(this);
        btn1.setText("Button1");
        linLayout.addView(btn1, leftMarginParams);
 
         
        LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightGravityParams.gravity = Gravity.RIGHT;
         
        Button btn2 = new Button(this);
        btn2.setText("Button2");
        linLayout.addView(btn2, rightGravityParams);
	}

	/**********************************************/	 
	public void v3()
	{
		 LinearLayout lm = new LinearLayout(this);
		 lm.setOrientation(LinearLayout.VERTICAL);
		 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		 setContentView(lm, params);
		 
	        //Create four 
	        for(int j=0;j<=4;j++) 
	        {   
	            // Create LinearLayout
	           LinearLayout ll = new LinearLayout(this);
	            ll.setOrientation(LinearLayout.HORIZONTAL);
	             
	            // Create TextView
	            TextView product = new TextView(this);
	            product.setText(" Product"+j+"    ");
	            ll.addView(product);
	             
	            // Create TextView
	            TextView price = new TextView(this);
	            price.setText("  $"+j+"     ");
	            ll.addView(price);
	             
	            // Create Button
	            final Button btn = new Button(this);
	                // Give button an ID
	                btn.setId(j+1);
	                btn.setText("Add To Cart");
	                // set the layoutParams on the button
	                btn.setLayoutParams(params);
	                 
	                final int index = j;
	                // Set click listener for button
	                btn.setOnClickListener(new OnClickListener() {
	                    public void onClick(View v) {
	                         
	                        Log.i("TAG", "index :" + index);
	                         
	                        Toast.makeText(getApplicationContext(), 
	                                "Clicked Button Index :" + index, 
	                                Toast.LENGTH_LONG).show();
	                         
	                    }
	                });
	                 
	               //Add button to LinearLayout
	                ll.addView(btn);
	               //Add button to LinearLayout defined in XML
	                lm.addView(ll);  
	        }
	}
	
	/**********************************************/
	
	public 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);
        ll.setTag(0); 
 		/**/
 		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);
        ll.setTag(1); 
 		/**/
        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);
        ll.setTag(2); 
 		/**/
        final EditText editText = new EditText(this);
        //editText.setText(titre);
        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);
        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);
        
        
        /***********************/        
        /**/
 		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);
        
        start_timeSliderHour = new TimeWidgetSlider(this, true, TimeWidgetSlider.STYPE_HOURS, iSliderViewWidth); // 24 heures 
		start_timeSliderMinutes = new TimeWidgetSlider(this, b24HourMode, TimeWidgetSlider.STYPE_MINUTES, iSliderViewWidth);
		//start_timeSliderHour.setValue(starthour,true);
	  	//start_timeSliderMinutes.setValue(startminute, true);
	  	
		/**/
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(5); 
        /**/
        ll.addView(start_timeSliderHour);
        ll.addView(layMargin);
        ll.addView(start_timeSliderMinutes);
        ll.setPadding(100, 0, 0, 10);
		/**/
        mainLayout.addView(ll);
        /***********************/        

        
        /***********************/        
        /**/
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(6); 
        /**/
        tv = new TextView(this);
        tv.setText("End Time");
        tv.setPadding(10, 10, 0, 10);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
        
    	end_timeSliderHour = new TimeWidgetSlider(this, true, TimeWidgetSlider.STYPE_HOURS, iSliderViewWidth); // 24 heures 
		end_timeSliderMinutes = new TimeWidgetSlider(this, b24HourMode, TimeWidgetSlider.STYPE_MINUTES, iSliderViewWidth);
		
		//end_timeSliderHour.setValue(endhour, true);
	  	//end_timeSliderMinutes.setValue(endminute, true);
		
        /**/
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(7); 
        /**/
        ll.addView(end_timeSliderHour);
        ll.addView(layMargin2);
        ll.addView(end_timeSliderMinutes);
        ll.setPadding(100, 0, 0, 10);
		/**/
		mainLayout.addView(ll);
        /***********************/        

		
		
 		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.VERTICAL);
        ll.setTag(1234); 
		TimePicker tp_start = new TimePicker(this); 
		tp_start.setIs24HourView(true); 
		tp_start.setForegroundGravity(Gravity.CENTER);
        tp_start.setCurrentHour(13); 
        tp_start.setCurrentMinute(27);
        /**/
 	    ll.addView(tp_start);
        mainLayout.addView(ll);
        

 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(4); 
 		/**/
        tv = new TextView(this);
        tv.setText("End Time");
        tv.setPadding(10, 10, 0, 30);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);

		
		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(1234); 
		TimePicker tp_stop = new TimePicker(this); 
		tp_stop.setIs24HourView(true); 
		tp_stop.setForegroundGravity(Gravity.CENTER);
		tp_stop.setCurrentHour(6); 
        tp_stop.setCurrentMinute(45);
		//tp.setPadding(10, 10, 0, 10);
        /**/
 	    ll.addView(tp_stop);
        mainLayout.addView(ll);
        
        
      //  https://www.codota.com/android/methods/android.widget.TimePicker/setCurrentMinute/

 		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(10, 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 = new TextView(this);
        tv.setText("End Time");
        tv.setPadding(10, 10, 0, 30);
 	    ll.addView(tv);
        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(10, 10, 50, 30);
 	    ll.addView(tv_set_time_end);
 	    Button btn_end_time = new Button(this);
 	    btn_end_time.setId(244);
 	    btn_end_time.setText("Add");   
 	    ll.addView(btn_end_time);
        mainLayout.addView(ll);
        
        
		
		/**/
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setTag(8); 
        /**/
        checkBox = new CheckBox(this);
        checkBox.setId(800);
        checkBox.setText("Alarm");
        checkBox.setPadding(10, 10, 50, 10);
        /**/
 	    ll.addView(checkBox);

 	    final Spinner Spinner_alarm = new Spinner(this);  
        Spinner_alarm.setId(900);
        Spinner_alarm.setPadding(10, 10, 0, 10);
        layoutParams=new LayoutParams(125, 75);
        Spinner_alarm.setLayoutParams(layoutParams);
        List<String> myArraySpinner = new ArrayList<String>();
        myArraySpinner.add("15mn");
        myArraySpinner.add("1H");
        myArraySpinner.add("Boom");     
        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.INVISIBLE); // hide
 	    ll.addView(Spinner_alarm);
        mainLayout.addView(ll);
        
        /**/
        
        /**/
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(9); 
        /**/
        tv = new TextView(this);
        tv.setText("Phone");
        tv.setPadding(10, 10, 0, 10);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
        
        
        /**/
 		ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(10); 
 		/**/
        EditText editText_phone = new EditText(this);
        editText_phone.setId(100);
        //editText.setText(titre);
        layoutParams=new LayoutParams(300, 75);
        editText_phone.setLayoutParams(layoutParams);
        editText_phone.setSingleLine(true);
        editText_phone.setFilters(new InputFilter[] { new InputFilter.LengthFilter(10) });
        editText_phone.setInputType(InputType.TYPE_CLASS_PHONE);
        /**/
 	    ll.addView(editText_phone);
 	  
 	    //Button btn_phone = new Button(this);
 	    //btn_phone.setPadding(0,0,0,0);
 	    //btn_phone.setText("Call");
        //ll.addView(btn_phone);
        /**/
        mainLayout.addView(ll);
		
		 /**/
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setTag(8); 
        /**/
        tv = new TextView(this);
        tv.setText("Notes");
        tv.setPadding(10, 10, 0, 10);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
		
        /**/
        ll = new LinearLayout(this);
       	ll.setOrientation(LinearLayout.VERTICAL);
       	ll.setTag(9); 
        /**/
       	final View childView = getLayoutInflater().inflate(R.layout.notes_view, null);
        /**/
        ll.addView(childView);
        mainLayout.addView(ll);
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ll.setGravity(Gravity.CENTER);
       
        Button b1 = new Button(this);
        b1.setId(1);
        b1.setText("Valider");
        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);
        /*****/

        
        CheckBox_AlarmListener(Spinner_alarm);
        //addButtonListener();
        
        ButtonListener();

        

        
        Button btn_valider = (Button) findViewById(1);
        btn_valider.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					start_hour = start_timeSliderHour.getValue();
				  	start_minutes = start_timeSliderMinutes.getValue();
				  	end_hour = end_timeSliderHour.getValue();
				  	end_minutes = end_timeSliderMinutes.getValue();
				  	
				  	// creation des dates
				  	String[] items = date_event.split(" ");
				  	// jour mois annee
				  	int jour = Integer.parseInt(items[0]);
				  	int mois = Integer.parseInt(items[1]);
				  	int annee = Integer.parseInt(items[2]);
				  	mois = mois - 1;
				  	
					Calendar cal_date = Calendar.getInstance();
					cal_date.set(Calendar.YEAR, annee);
					cal_date.set(Calendar.DAY_OF_MONTH, jour);
					cal_date.set(Calendar.MONTH, mois);
					cal_date.set(Calendar.HOUR_OF_DAY, 0);
					cal_date.set(Calendar.MINUTE, 0);
					cal_date.set(Calendar.SECOND, 0);
					cal_date.set(Calendar.MILLISECOND, 0);
			        Date Date_date =  cal_date.getTime();
			        long date = cal_date.getTimeInMillis();
				  			
				  	Calendar cal_start = Calendar.getInstance();
			        cal_start.set(annee,mois,jour,start_hour, start_minutes);
			        cal_start.set(Calendar.SECOND, 0);
			        cal_start.set(Calendar.MILLISECOND, 0);
			        Date Date_start =  cal_start.getTime();
			        long startTime = cal_start.getTimeInMillis();
			        
			    	Calendar cal_end = Calendar.getInstance();
			        cal_end.set(annee,mois,jour, end_hour, end_minutes);
			        cal_end.set(Calendar.SECOND, 0);
			        cal_end.set(Calendar.MILLISECOND, 0);
			        Date Date_end =  cal_end.getTime();
			        long endTime = cal_end.getTimeInMillis();
			        
			        String titre = editText.getText().toString();

			        EditText phone = (EditText)findViewById(100);  
			        String string_phone = phone.getText().toString();
			        
			        EditText notes = (EditText)findViewById(R.id.notes);  
			        String string_notes = notes.getText().toString();
			        
			        boolean check = validation(titre, startTime, endTime);
/*			   
			        if(check == true)
			        {
				        	checkBox = (CheckBox) findViewById(800);
					        Boolean checkBoxState = checkBox.isChecked();
					        if(checkBoxState == true)
					        {
						        option_alarm = Spinner_alarm.getSelectedItem().toString();
					        }
					        else
					        {
					        	option_alarm = "";
					        }
				        	
				        	
				        	long id = db.insert_appoitment(titre, date, startTime, endTime, string_phone , string_notes , "actif");
				        	Toast.makeText(getApplicationContext(), "Rendez-vous pris en compte", Toast.LENGTH_LONG).show();
				    		
					        mois = mois + 1;
					        String dateInString = jour + "-" + mois +  "-" + annee + " " + start_hour + ":" + start_minutes;
							long  time_millis = Tools.convertir_date_timestamp(dateInString); 
				        	
				    		Intent service = new Intent(AddEvent02.this, AlarmService.class);
				    		service.putExtra("ALARMID", id );              
				    		service.putExtra("TITRE", titre);
				    		service.putExtra("TIME", time_millis);
				    		service.setAction(AlarmService.POPULATE);
				    		startService(service);
							OnClose();  
			        }
*/
			        
//		        	long id = db.insert_appoitment(titre, date, startTime, endTime, string_phone , string_notes , "actif");
//		        	Toast.makeText(getApplicationContext(), "DEbug ----- Rendez-vous pris en compte", Toast.LENGTH_LONG).show();
		        	
					//long now = System.currentTimeMillis();

		        	
			        //long id = 100;
			        //long time = 1487802900000L;
			        
			        
 					//start_hour = 
				  	//start_minutes = start_timeSliderMinutes.getValue();

//			        date_event;
//			        date;
			        
			        
			        
			        
//			        String dateInString = "22-02-2017 21:48";
			        
//			        mois = mois + 1;
//			        dateInString = jour + "-" + mois +  "-" + annee + " " + start_hour + ":" + start_minutes;
//					long  time_millis = Tools.convertir_date_timestamp(dateInString); 
			        
					//Calendar cal = Calendar.getInstance();
		            //cal.set(annee,mois,jour,21,15); // year month day
		             
		            //cal.set(2017,1,22,21,15); // year month day
		            //Date event =  cal.getTime(); 
      			    //long heure_alarme = cal.getTimeInMillis();
			        
			        //time = now + 60 * 1000 * 1;
					
//		    		Intent service = new Intent(AddEvent02.this, AlarmService.class);
//		    		service.putExtra("ALARMID", id );              //String.valueOf(id));
//		    		service.putExtra("TITRE", titre);
//		    		service.putExtra("TIME", time_millis);
//		    		service.setAction(AlarmService.POPULATE);
//		    		startService(service);
		    		

		        	
/*		        	 
			        

					long time = now + 60 * 1000 * 1;
					long id = 100;
					titre = "alpha";
		    		Intent service = new Intent(AddEvent02.this, AlarmService.class);
		    		service.putExtra("ALARMID", id );              //String.valueOf(id));
		    		service.putExtra("TITRE", titre);
		    		service.putExtra("TIME", time);
		    		service.setAction(AlarmService.POPULATE);
		    		startService(service);

		    		time = now + 60 * 1000 * 2;
					id = 200;
					titre = "Beta";
		    		service = new Intent(AddEvent02.this, AlarmService.class);
		    		service.putExtra("ALARMID", id );              //String.valueOf(id));
		    		service.putExtra("TITRE", titre);
		    		service.putExtra("TIME", time);
		    		service.setAction(AlarmService.POPULATE);
		    		startService(service);

		    		
		    		
		    		time = now + 60 * 1000 * 3;
					id = 300;
					titre = "Delta";
		    		service = new Intent(AddEvent02.this, AlarmService.class);
		    		service.putExtra("ALARMID", id );              //String.valueOf(id));
		    		service.putExtra("TITRE", titre);
		    		service.putExtra("TIME", time);
		    		service.setAction(AlarmService.POPULATE);
		    		startService(service);
			        
*/
			        
			        
			        
 				}
 	    });
        
        
        Button btn_quitter = (Button) findViewById(2);
        btn_quitter.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					finish();
 				}
 	    });
        
	}
	
/*	
	public void addButtonListener() 
	{
		        Button button = (Button) findViewById(144);

		        button.setOnClickListener(new OnClickListener() 
		        {
		            @Override
		            public void onClick(View v) 
		            {
		                showDialog(TIME_DIALOG_ID);
					//	Toast.makeText(AddEvent02.this, "TimePicker", Toast.LENGTH_LONG).show();

		            }
		        });
	}
*/	
	public void ButtonListener() 
	{
		
		findViewById(144).setOnClickListener(mGlobal_OnClickListener);
		findViewById(244).setOnClickListener(mGlobal_OnClickListener);
	      

//		button1.setOnClickListener((OnClickListener) this);
//		button2.setOnClickListener((OnClickListener) this);
		//button1.setOnClickListener((OnClickListener) this);
		//button2.setOnClickListener((OnClickListener) this);
/*
		setClickListeners(button1);
		setClickListeners(button2);
		
		 Button one = (Button) findViewById(144);
		    one.setOnClickListener(this); // calling onClick() method
		    Button two = (Button) findViewById(R.id.twoButton);
		    two.setOnClickListener(this);

		
		 button1.setOnClickListener(this);

		 button1.View.OnClickListener(this);
*/		 
	}
	
	/*private void setClickListeners(View view)
	{
	view.setOnClickListener(clickListener);
	}*/
	
	
	//Global On click listener for all views
    OnClickListener mGlobal_OnClickListener = new OnClickListener()
    {
	        public void onClick(final View v)
	        {
	            switch(v.getId())
	            {
	                case 144:
						//Toast.makeText(AddEvent02.this, "un", Toast.LENGTH_LONG).show();
		                //showDialog(TIME_DIALOG_START);
	                	dialog_box_timepicker("Choice start Time", "start");
	                    break;
	                case 244:
	                    //Toast.makeText(AddEvent02.this, "Button2 clicked.", Toast.LENGTH_SHORT).show();
		                //showDialog(TIME_DIALOG_END);
	                	dialog_box_timepicker("Choice end Time", "end");
	                    break;
	            }
	        }
    };
	
	
    //setOnCancelListener
    //http://stackoverflow.com/questions/14092093/is-there-a-way-to-use-cancel-in-android-jellybean-timepickerdialog
    //http://www.coderzheaven.com/2012/09/01/show-timepickerdialogs-activity-listener-function-android/
    //https://android--examples.blogspot.fr/2015/09/how-to-set-cancel-button-in.html
    
    //// Put a cancel button in progress dialog

    //http://www.bogotobogo.com/Android/android8DatePickerTimePickerClocks.php
    
    // http://android-er.blogspot.fr/2012/05/androidapptimepickerdialog.html
    
    // http://weide-dev.blogspot.fr/2010/04/timepickerdialog.html
    
	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch (id) 
		{
			case TIME_DIALOG_START:
				//return new TimePickerDialog(this, timePickerListener_start, 18, 30, true);
				
				TimePickerDialog tpd = new TimePickerDialog(this, timePickerListener_start, 18, 30, true); 
				
				//tpd.setOnTimeChangedListener(this); 
				
		        /*tpd.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

		            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		                updateDisplay(hourOfDay, minute);
		            }
		        });
				*/

				//tpd.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel"); //, new DialogInterface.OnClickListener();
				// http://alvinalexander.com/java/jwarehouse/android/core/java/android/app/TimePickerDialog.java.shtml
				
				 tpd.setMessage("TimePickerDialog Message");
				
				tpd.setTitle("tvTitle");
				
				//tpd.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", tpd);
				
				
				tpd.setButton(TimePickerDialog.BUTTON_POSITIVE, "Yes", new TimePickerDialog.OnClickListener() 
				{

				            @Override
				            public void onClick(DialogInterface dialog, int which)
				            {
				                if (which == DialogInterface.BUTTON_POSITIVE)
				                {
				                	
				            		//public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute)
				            		//{
				            		//	int hour = selectedHour;
				            		//	int minute = selectedMinute;
				            		//}

				                  //  Toast.makeText(AddEvent02.this, "Yeap", Toast.LENGTH_SHORT).show();


				                    //int hour = ((TimePicker) tpd).getCurrentHour();
				                    //int minute = tpd.getCurrentMinute();
				                	
				                	
				        			tv_set_time_start.setText(new StringBuilder().append(pad(hour))	.append(":").append(pad(minute)));

				                }
				            }
				});

		/*		void onTimeChanged (TimePicker view, 
		                int hourOfDay, 
		                int minute)
		*/		
				
				tpd.setButton(DatePickerDialog.BUTTON_NEGATIVE, "aaaCancel", new DatePickerDialog.OnClickListener() 
				{

				            @Override
				            public void onClick(DialogInterface dialog, int which)
				            {
				                if (which == DialogInterface.BUTTON_NEGATIVE)
				                {
				                    //Toast.makeText(AddEvent02.this, "FINNNNNN", Toast.LENGTH_SHORT).show();
				        			tv_set_time_start.setText("--:--");

				                }
				            }
				        });
				
				
			/*	
				tpd(new DialogInterface.OnCancelListener()
				{
		            @Override
		            public void onCancel(DialogInterface arg0)
		            {
		                //your cancel task.
		            	String aaa = "55";
		           
		            }
				}    
		          );
*/

				
				return tpd;
				
				
			case TIME_DIALOG_END:
				return new TimePickerDialog(this, timePickerListener_end, 20, 30, true);
	

		}
		return null;
	}

	
	private TimePickerDialog.OnTimeSetListener timePickerListener_start =  new TimePickerDialog.OnTimeSetListener() 
	{
		public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute)
		{
			hour = selectedHour;
			minute = selectedMinute;
            Toast.makeText(AddEvent02.this, "*********", Toast.LENGTH_SHORT).show();
			tv_set_time_start.setText(new StringBuilder().append(pad(hour))	.append(":").append(pad(minute)));
		}
		
		public void onTimeChanged (TimePicker view,   int hourOfDay, int minute)
		{
            Toast.makeText(AddEvent02.this, "Changed", Toast.LENGTH_SHORT).show();

		}

		
	};
	
	private TimePickerDialog.OnTimeSetListener timePickerListener_end =  new TimePickerDialog.OnTimeSetListener() 
	{
		public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute)
		{
			int hour = selectedHour;
			int minute = selectedMinute;
			//tv_set_time_end.setText(new StringBuilder().append(pad(hour))	.append(":").append(pad(minute)));
		}
		
		//setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)
		
		public void onTimeChanged (TimePicker view,   int hourOfDay, int minute)
		{
            Toast.makeText(AddEvent02.this, "Changed", Toast.LENGTH_SHORT).show();

		}
		
	};

	private static String pad(int c)
	{
		if (c >= 10)
		   return String.valueOf(c);
		else
		   return "0" + String.valueOf(c);
	}
	
	/****************************************************************/
	
	private void dialog_box_timepicker(String titre, final String action)
	{
	
		LayoutInflater inflater = getLayoutInflater();
		View alertLayout = inflater.inflate(R.layout.custom_datepicker_layout, null);
    
		final TimePicker tp = (TimePicker) alertLayout.findViewById(R.id.timePicker1);
		
		tp.setIs24HourView(true); 
		
		if(action.equals("start"))
		{
			tp.setCurrentHour(global_start_hour); 
			tp.setCurrentMinute(global_start_min);
		}
		else
		{
			tp.setCurrentHour(global_end_hour); 
			tp.setCurrentMinute(global_end_min);
		}
		
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(titre);
		// this is set the view from XML inside AlertDialog
		alert.setView(alertLayout);
		// disallow cancel of AlertDialog on click of back button and outside touch
		alert.setCancelable(false);
    
		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
			}
		});

		alert.setPositiveButton("Set", new DialogInterface.OnClickListener() 
		{

			@Override
			public void onClick(DialogInterface dialog, int which) 
			{
				if(action.equals("start"))
				{	
					global_start_hour = tp.getCurrentHour();
					global_start_min = tp.getCurrentMinute();
	
					tv_set_time_start.setText(new StringBuilder().append(pad(global_start_hour))	.append(":").append(pad(global_start_min)));
				}
				else
				{
					global_end_hour = tp.getCurrentHour();
					global_end_min = tp.getCurrentMinute();
					tv_set_time_end.setText(new StringBuilder().append(pad(global_end_hour))	.append(":").append(pad(global_end_min)));
				}
			}
		});

		AlertDialog dialog = alert.create();
		dialog.show();
	}
	
	/****************************************************************/
	
	
	
	public void CheckBox_AlarmListener(final Spinner option_alarm)
	{
			checkBox = (CheckBox) findViewById(800);
	
			checkBox.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v)
				{
		                //is chkIos checked?
					if (((CheckBox) v).isChecked())
					{	 
						option_alarm.setVisibility(View.VISIBLE); 

//						Toast.makeText(AddEvent02.this, "Checkbox", Toast.LENGTH_LONG).show();
					}
					else
					{
						option_alarm.setVisibility(View.INVISIBLE); // hide

					}
				}
			});

	}
	
	/*********************************************/
	
	public boolean validation(String titre,long startTime, long endTime)
	{
		
		 ArrayList<String> list_error = new ArrayList<String>();
		
		 if(titre.equals("") )
	     {
			 list_error.add("Pas de titre !!!");
	         //Toast.makeText(getApplicationContext(), "Vides ???", Toast.LENGTH_LONG).show();
	     }

	     if (startTime >=  endTime)
	     {
	    	 list_error.add("Incohérence : fin avant le début !!!!");
        	 //Toast.makeText(getApplicationContext(), "Probléme Date !!!!", Toast.LENGTH_LONG).show();
	     }
	        
	        
	     if ( endTime - startTime < 1800000 ) // 30 min : 60 * 1000 * 30  = 1800000
	     {
	    	 list_error.add("Rendez-vous inférieur à 30min !!!");
	         //Toast.makeText(getApplicationContext(), "Rendez-vous inférieur à 30min !!! ", Toast.LENGTH_LONG).show();
	     }
	     
	     
	     boolean overlap = non_recouvrement(startTime, endTime);
	     if(overlap == true)
	     {
	    	 list_error.add("Attention overlap !!!");
	     }
	     
	     
	     if(list_error.size() == 0 )
	     {	 
	     	 return true;
	     }
	     else
	     {
		        String toto = "";	
	    	 
	    	 	for(int x = 0; x <= list_error.size() - 1 ; x++)
	    	 	{
	    	   	 		toto = toto + list_error.get(x);
	    	   	 		toto = toto + System.getProperty("line.separator");
	    	 	}
	    	 	
		    	Toast.makeText(getApplicationContext(), toto, Toast.LENGTH_LONG).show();

		    	return false; 
	     }
	}
	
	/********************************************/
	public boolean non_recouvrement(long borne_startTime, long borne_endTime)
	{
  			// plage qui se chevauche - overlap
		
			SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy"); 
 	   	  	Date date = null;
 	   	  	try
 	   	  	{
 	   	  		date = sdf.parse(date_event);
 	   	  	} 
 	   	  	catch (ParseException e)
 	   	  	{
				e.printStackTrace();
 	   	  	}
	      
 	   	  	long timestamp = date.getTime();
		 	String stimestamp = String.valueOf(timestamp);
		         
		    String requete = "SELECT * FROM table_appointment WHERE date = " + stimestamp ;
		     	
		    Cursor Cursor = db.DB_query(requete);
		    int val = Cursor.getCount();
		   
		    boolean overlap = false;
		    		
		    if(val != 0)
		    {
		    			if (Cursor.moveToFirst()) 
		 				{
			 					do 
			 					{
				 						String string_start_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_START_TIME ));
				 						String string_end_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_END_TIME ));
				 						
				 						//String s = "ggg " + start_timestamp;
				 						//long ll = Long.parseLong(start_timestamp);   
				 						
				 						long start_timestamp = Long.parseLong(string_start_timestamp);   
				 						long end_timestamp = Long.parseLong(string_end_timestamp);   
				 						
				 						if(  borne_startTime >= start_timestamp && borne_startTime <= end_timestamp  )
				 						{
				 							overlap = true;
				 						}
				 						
				 						
				 						if(  borne_endTime >= start_timestamp && borne_endTime <= end_timestamp  )
				 						{
				 							overlap = true;
				 						}
				 						
				 						
				 						if( borne_startTime <=  start_timestamp && borne_endTime >= end_timestamp)
				 						{
				 							overlap = true;
				 						}
			 						
			 					} while (Cursor.moveToNext());
			 					
		 				}
		    			
		    			
		 		}
		    	
		    
		    	return overlap;
	}
	
	
	/********************************************/
	
	public View widget()
	{
		
	 		LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	
	 		layMain.setPadding(8, 8, 8, 8);

	 		LinearLayout layTopControls = createLayout(LinearLayout.HORIZONTAL);
	 		LinearLayout layContentTop = createLayout(LinearLayout.HORIZONTAL);
	 		LinearLayout layContentBottom = createLayout(LinearLayout.HORIZONTAL);
	 		LinearLayout layBottomControls = createLayout(LinearLayout.HORIZONTAL);
	 		LinearLayout layMargin = createLayout(LinearLayout.HORIZONTAL);
	 		LinearLayout layMargin2 = createLayout(LinearLayout.HORIZONTAL);
	 		
	 		layContent = createLayout(LinearLayout.VERTICAL);
	  	
	 		layContent2 = createLayout(LinearLayout.VERTICAL);
	 		
		  	
	 		// 	layTopControls.addView(createLabelCaption());  	
	 		layTopControls.setGravity(Gravity.CENTER_HORIZONTAL);
	  	
	 		//generateBottomButtons(layBottomControls);

	 		layContentTop.getLayoutParams().height = 16;
	 		layContentBottom.getLayoutParams().height = 18;
	 		layMargin.getLayoutParams().height = 18;
	 		layMargin2.getLayoutParams().height = 18;
			
	        TextView tv = new TextView(this);
	        tv.setText("Evenement++++");
	        //tv.setFilters(new InputFilter[] { new InputFilter.LengthFilter(7) }); // length
	        
	        int maxLength = 3;
	        InputFilter[] FilterArray = new InputFilter[1];
	        FilterArray[0] = new InputFilter.LengthFilter(maxLength);
	        tv.setFilters(FilterArray);
	        
	        tv.setPadding(10, 0, 0, 0);
	        layMain.addView(tv);
	        
	        final EditText editText = new EditText(this);
	        editText.setId(viewsCount++);
	        editText.setHint("Titre");
	        editText.setSingleLine(true);
	        layMain.addView(editText);
	        
	        tv = new TextView(this);
	        String tmp = "Date : " + date_event;
	        tv.setText(tmp);
	        tv.setPadding(10, 30, 0, 30);
	        layMain.addView(tv);
			
	        tv = new TextView(this);
	        tv.setText("Start Time");
	        tv.setPadding(10, 10, 0, 30);
	        layMain.addView(tv);
	        
	        
			start_timeSliderHour = new TimeWidgetSlider(this, true, TimeWidgetSlider.STYPE_HOURS, iSliderViewWidth); // 24 heures 
			start_timeSliderMinutes = new TimeWidgetSlider(this, b24HourMode, TimeWidgetSlider.STYPE_MINUTES, iSliderViewWidth);

			end_timeSliderHour = new TimeWidgetSlider(this, true, TimeWidgetSlider.STYPE_HOURS, iSliderViewWidth); // 24 heures 
			end_timeSliderMinutes = new TimeWidgetSlider(this, b24HourMode, TimeWidgetSlider.STYPE_MINUTES, iSliderViewWidth);

			
			layContent.addView(start_timeSliderHour);
			layContent.addView(layMargin);
			layContent.addView(start_timeSliderMinutes);
	        layContent.setPadding(100, 0, 0, 60);
	        layMain.addView(layContent);
	        
	        tv = new TextView(this);
	        tv.setText("End Time");
	        tv.setPadding(10, 10, 0, 30);
	        layMain.addView(tv);
	        
	        
	        layContent2.addView(end_timeSliderHour);
		 	layContent2.addView(layMargin2);
			layContent2.addView(end_timeSliderMinutes);
	        layContent2.setPadding(100, 0, 0, 60);
	        layMain.addView(layContent2);
		        
	        
	        LinearLayout ll = new LinearLayout(this);
	        ll.setOrientation(LinearLayout.VERTICAL);
	        
	        /**/
	        ll = new LinearLayout(this);
	        ll.setOrientation(LinearLayout.VERTICAL);
	        ll.setTag(8); 
	        /**/
	        tv = new TextView(this);
	        tv.setText("Notzzzzzes");
	        tv.setPadding(10, 10, 0, 10);
	        /**/
	        layMain.addView(tv);
	 	    //ll.addView(tv);
	        //mainLayout.addView(ll);
	        
			
	        /**/
//	        ll = new LinearLayout(this);
//	       	ll.setOrientation(LinearLayout.VERTICAL);
//	       	ll.setTag(9); 
	        /**/
	       	final View childView = getLayoutInflater().inflate(R.layout.notes_view, null);
	        /**/
//	        ll.addView(childView);
	        layMain.addView(childView);
	        
         
	        Button btn = new Button(this);
	        btn.setText("Ajouter");
	        layMain.addView(btn);
	        
	       
	      	btn.setOnClickListener(new Button.OnClickListener()
		  	{
					public void onClick(View arg0)
					{
						start_hour = start_timeSliderHour.getValue();
					  	start_minutes = start_timeSliderMinutes.getValue();
					  	end_hour = end_timeSliderHour.getValue();
					  	end_minutes = end_timeSliderMinutes.getValue();
					  	
					  	// creation des dates
					  	
					  	String[] items = date_event.split(" ");
					  	// jour mois annee
					  	int jour = Integer.parseInt(items[0]);
					  	int mois = Integer.parseInt(items[1]);
					  	int annee = Integer.parseInt(items[2]);
					  	mois = mois - 1;
					  	
						Calendar cal_date = Calendar.getInstance();
						cal_date.set(Calendar.YEAR, annee);
						cal_date.set(Calendar.DAY_OF_MONTH, jour);
						cal_date.set(Calendar.MONTH, mois);
						cal_date.set(Calendar.HOUR_OF_DAY, 0);
						cal_date.set(Calendar.MINUTE, 0);
						cal_date.set(Calendar.SECOND, 0);
						cal_date.set(Calendar.MILLISECOND, 0);
				        Date Date_date =  cal_date.getTime();
				        long date = cal_date.getTimeInMillis();
					  			
					  	Calendar cal_start = Calendar.getInstance();
				        cal_start.set(annee,mois,jour,start_hour, start_minutes);
				        cal_start.set(Calendar.SECOND, 0);
				        cal_start.set(Calendar.MILLISECOND, 0);
				        Date Date_start =  cal_start.getTime();
				        long startTime = cal_start.getTimeInMillis();
				        
				    	Calendar cal_end = Calendar.getInstance();
				        cal_end.set(annee,mois,jour, end_hour, end_minutes);
				        cal_end.set(Calendar.SECOND, 0);
				        cal_end.set(Calendar.MILLISECOND, 0);
				        Date Date_end =  cal_end.getTime();
				        long endTime = cal_end.getTimeInMillis();
				        
				        String titre = editText.getText().toString();
				        
				        // stockage
/////////////////				       // db.insert_appoitment(titre, date, startTime, endTime,"actif");
					  	
						
					  	// fermer l'activity
						OnClose();  // 9h30 à 12h55
			}});
		  	
	  	
	  //	layMain.addView(layContentBottom);
	 // 	layMain.addView(layBottomControls);  	

	  	return layMain;
		
	}
	
	public void OnClose()
	{
		Bundle data = new Bundle();
		data.putInt("Hour", start_hour);
		data.putInt("Minute", start_minutes);
    
		Intent intentData = new Intent("");
		intentData.putExtras(data);
		setResult(RESULT_OK, intentData);
		
		this.finish();
	}

	public void clearTime()
	{
		start_hour = -1;
		start_minutes = -1;
	}
	

}
