package com.androidbegin.buttonclicktutorial;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.app.ActionBar.LayoutParams;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;

public class infos_appointment extends Activity 
{
		TimeWidgetSlider start_timeSliderHour = null;
		TimeWidgetSlider start_timeSliderMinutes = null;
		TimeWidgetSlider end_timeSliderHour = null;
		TimeWidgetSlider end_timeSliderMinutes = null;
		
		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;
	    
    
		private static String sStrSelect = "Select time";
		private static String sStrSet = "set";
		private static String sStrNone = "none";
		
		public static final int SELECT_TIME_REQUEST = 112;
		public static final int iSliderViewWidth = 276;
		private static final int iSmallButtonWidth = 100;	
		
		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 RelativeLayout mLayout;
		 
		String display_date = "";
		 
		int starthour;
		int startminute;
		int endhour; 
		int endminute;
		
		String date_selection;
		int index;
		
		DB_alarm db;
		appointment class_id = new appointment();
		
		String sid , titre,  start_timestamp , end_timestamp , phone , notes , status ,	 creation,  modification; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		  
		db = new DB_alarm(getApplicationContext());
		
		Bundle extras = getIntent().getExtras();
		index = extras.getInt("index");
		date_selection = extras.getString("date_selection");
		
		//cal_display
		String[]tmp = date_selection.split(" ");
	    int jour = Integer.parseInt(tmp[0]);
	    int mois = Integer.parseInt(tmp[1]);
	    int annee = Integer.parseInt(tmp[2]);
	    mois = mois - 1;
	    Calendar cal = Calendar.getInstance();
	    cal.set(annee, mois, jour, 0, 0, 0); 
	    cal.set(Calendar.MILLISECOND, 0);
		
	    SimpleDateFormat formatter = new SimpleDateFormat("d MMMM, yyyy", Locale.ENGLISH);
        date_event = formatter.format(cal.getTime());

        /* lecture de la database*/
        
    	String id = String.valueOf(index);
		String requete = "SELECT * FROM table_appointment WHERE _id = " + id ;
 	
		Cursor Cursor = db.DB_query(requete);
		int val = Cursor.getCount();
		
		if(val != 0)
		{
				if (Cursor.moveToFirst()) 
				{
 					do 
 					{
							sid = Cursor.getString(Cursor.getColumnIndex(DB_alarm.KEY_ID ));
	 						titre = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_TITRE ));
	 						start_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_START_TIME ));
	 						end_timestamp = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_END_TIME ));
	 						phone = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_PHONE ));
	 						notes = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_NOTES ));
	 						status = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_STATUS ));
	 						creation = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_CREATEDTIME ));
	 						modification = Cursor.getString(Cursor.getColumnIndex(DB_alarm.COL_MODIFIEDTIME ));
 					} while (Cursor.moveToNext());
 					
				}
		}
	
        
        /**/
        
		//class_id = MainActivity.appointment_DB.get(index-1);
		//titre = class_id.getTitre();
		//Date start_time = class_id.getStart_time(); 
		//Date end_time = class_id.getEnd_time(); 
		//phone =  class_id.getPhone(); 
		//notes = class_id.getNotes(); 
		
		
		//class_id = agenda02.appointment_liste.get(index-1);
		//titre = class_id.getTitre();
		//Date start_time = class_id.getStart_time(); 
		//Date end_time = class_id.getEnd_time(); 
		//phone =  class_id.getPhone(); 
		//notes = class_id.getNotes(); 
		
		Date start_time = new Date(Long.parseLong(start_timestamp));
		Date end_time = new Date(Long.parseLong(end_timestamp));

		
        //
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(start_time);
		starthour = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
		startminute = calendar.get(Calendar.MINUTE);
		calendar.setTime(end_time);
		endhour = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
		endminute = calendar.get(Calendar.MINUTE);
		
		
		layout();
	   //setContentView(widget());
			
	}
	
	
	
	public void zorro()
	{
		
		//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);    
        
        for (int i=0; i<30; i++)
        {
	            LinearLayout ll = new LinearLayout(this);
	            ll.setOrientation(LinearLayout.HORIZONTAL);
	            ll.setTag(i);                           
	            TextView tv=new TextView(this);
	            tv.setText("Rowxxx " + i);           
	            ll.addView(tv);
	            Button b = new Button(this);
	            b.setTag(i);
	            b.setText("Button " + i);           
	            ll.addView(b);                   
	            mainLayout.addView(ll);
        }
        
        scrollView.addView(mainLayout);
        setContentView(scrollView);
		
	}
	
	
	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);
 		
 		//layContent = createLayout(LinearLayout.VERTICAL);
 		//layContent2 = createLayout(LinearLayout.VERTICAL);

 		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);
		
    	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(6); 
        /**/
        tv = new TextView(this);
        tv.setText("End Time");
        tv.setPadding(10, 10, 0, 10);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
		
        /**/
        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(9); 
        /**/
        tv = new TextView(this);
        tv.setText("Phone");
        tv.setTag(50);
        tv.setPadding(10, 10, 0, 10);
        /**/
 	    ll.addView(tv);
        mainLayout.addView(ll);
		

        /********** lancement de l'activity ***************************************************/
       
 /*       
        if(phone.equals("") )
        {
		 		ll = new LinearLayout(this);
		        ll.setOrientation(LinearLayout.HORIZONTAL);
		        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.setId(60);
		 	    btn_phone.setText("Call");
		 	    
		 	    ll.addView(btn_phone);
		 	    
		 	    mainLayout.addView(ll);
        } 
       
 /*	    
        else
        {	
	        	ll = new LinearLayout(this);
		        ll.setOrientation(LinearLayout.HORIZONTAL);
		        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.setId(60);
		 	    btn_phone.setText("Call");
		 	    
		 	    ll.addView(btn_phone);
	        
		 	    mainLayout.addView(ll);
	       	
 		        final String tel = "5545";
		        btn_phone.setOnClickListener( new OnClickListener()
		        {
		        	public void onClick(View v)
		        	{
						Intent intent = new Intent(Intent.ACTION_CALL);
						intent.setData(Uri.parse("tel:"+tel ));
						startActivity(intent);
					}
			    });
        }
 */       
        
        
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        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.setId(60);
 	    btn_phone.setText("Call");
 	    
 	    ll.addView(btn_phone);
    
 	    mainLayout.addView(ll);
   	
	    final String tel = "5545";
        btn_phone.setOnClickListener( new OnClickListener()
        {
        	public void onClick(View v)
        	{
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+tel ));
				startActivity(intent);
			}
	    });
        
        /**********************************************************
		 /**/
        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.VERTICAL);
        ll.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        Button btn = new Button(this);
        btn.setText("Ajouter");
        btn.setGravity(Gravity.CENTER);
        
        btn.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        ll.addView(btn);
        mainLayout.addView(ll);
       */ 
        
        /*
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        EditText notes = new EditText(this);
        LinearLayout.LayoutParams aa =new LinearLayout.LayoutParams(  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
        notes.setWidth(250);
        notes.setLines(4);
        notes.setGravity(Gravity.TOP);
    	notes.setVerticalScrollBarEnabled(true);
    	notes.setScroller(new Scroller(this));
    	notes.isVerticalScrollBarEnabled();
        notes.setSingleLine(false);
        notes.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    	notes.setMaxLines(4);
        notes.setBackgroundColor(Color.parseColor("#e744cc"));  // 0xfff00000
        notes.setLayoutParams(aa);
        layout.addView(notes);
        mainLayout.addView(layout);
        */
        
 
        /* OK
        LinearLayout llBasicInfo = new LinearLayout(this);
        llBasicInfo.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        llBasicInfo.setOrientation(LinearLayout.VERTICAL);
        Button bt = new Button(this);
        bt.setText("fdghsdsdfg");
        LinearLayout.LayoutParams aa_layoutParams =new LinearLayout.LayoutParams(  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
        aa_layoutParams.gravity=Gravity.CENTER;
        bt.setLayoutParams(aa_layoutParams);
        llBasicInfo.addView(bt);
        mainLayout.addView(llBasicInfo);
		*/
        
       /* OK 
       ll = new LinearLayout(this);
       LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 2f);  
       Button bt = new Button(this);
       bt.setLayoutParams(layoutParams1);
       ll.setOrientation(LinearLayout.HORIZONTAL);
       ll.setGravity(Gravity.CENTER);
       TextView product = new TextView(this);
       product.setText("sdqsddq");
       product.setPadding(0,0,25,0);
       ll.addView(product);
       TextView price = new TextView(this);
       price.setText("ujujuuuu");
       ll.addView(price);
       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("Delete");
       ll.addView(b2);
        
       Button b3 = new Button(this);
       b3.setId(3);
       b3.setText("Exit");
       ll.addView(b3);

       mainLayout.addView(ll);
       
           //// phone number
           //ed1.setInputType(InputType.TYPE_CLASS_NUMBER);
           //View childView2 = getLayoutInflater().inflate(R.layout.custom03, null);
           //layMain.addView(childView2);	
        
        /* ok*/
        /*
        for (int i=0; i<30; i++)
        {
            ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ll.setTag(i);                           
            tv=new TextView(this);
            tv.setText("Row " + i);           
            ll.addView(tv);
            Button b = new Button(this);
            b.setTag(i);
            b.setText("Button " + i);           
            ll.addView(b);                   
            mainLayout.addView(ll);
        }
        */
        
        scrollView.addView(mainLayout);
        layMain.addView(scrollView);
        setContentView(layMain);
        
        /*************************************/        
        
 	    // invisible si null
        if(phone.equals("") )
        {
     	    Button yyy = (Button)findViewById(60); 
     	    //yyy.setEnabled(false);
     		yyy.setVisibility(View.INVISIBLE); // hide

        }
        
        // remplir les champs
        final EditText idphone = (EditText)findViewById(100);  
        idphone.setText(phone);
        
        EditText idnotes = (EditText)findViewById(R.id.notes);  
        idnotes.setText(notes);
        
        //final EditText editText = new EditText(this);
        //editText.setText("titre");
        final Button btncall = (Button)findViewById(60);  
        
        
        /** OK si on change le focus en passant a une autre vue **/
        
/*
        idphone.setOnFocusChangeListener(new OnFocusChangeListener() 
        {          
	               @Override
	               public void onFocusChange(View v, boolean hasFocus)
	               {
		                   if (!hasFocus)
		                   {
		                	     String s = idphone.getText().toString();
			              		 if(s.equals("") )
			              	     {
			              			xxx.setVisibility(View.INVISIBLE); // hide
			              			//BUTTON.setVisibility(View.VISIBLE); // show
			              	     }
			              		 else
			              		 {
			              			xxx.setVisibility(View.VISIBLE); 
			              		 }
		                   }
	               }
           });
   */     
        
        /** OK  : changement dynamique **/
        
        idphone.addTextChangedListener(new TextWatcher()
        {
	            public void onTextChanged(CharSequence s, int start, int before, int count)
	            {
	            	String chaine = s.toString();
	                
	            	
	            	//if(chaine.equals("") )
	                if( chaine.length() <= 9 )
	                {
	                	btncall.setVisibility(View.INVISIBLE); // hide
	                }
	                else
	                {
	                	btncall.setVisibility(View.VISIBLE); // hide
	                }
	            }
	
	            public void beforeTextChanged(CharSequence s, int start, int count, int after)
	            {
	            	//Toast.makeText(getApplicationContext(), "before", Toast.LENGTH_LONG).show();
	            }
	
	            public void afterTextChanged(Editable s)
	            {
	            	//Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
	            }
        });
        
        
        /********************************************************************/
        
        
        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_selection.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();
			        
			        long now = System.currentTimeMillis();
			    	ContentValues contentValues = new ContentValues();
			    	contentValues.put(DB_alarm.COL_TITRE, titre);
			    	contentValues.put(DB_alarm.COL_START_TIME, startTime);
			    	contentValues.put(DB_alarm.COL_END_TIME, endTime);
			    	contentValues.put(DB_alarm.COL_PHONE, string_phone);
			    	contentValues.put(DB_alarm.COL_NOTES, string_notes);
			    	contentValues.put(DB_alarm.COL_MODIFIEDTIME, now);
			    	
			    	// mise a jour de la database
			    	String position = Integer.toString(index);
			    	db.update_appoitment(position, contentValues);

			    	// mise a jour arraylist -> appointment_liste
			    	class_id.setTitre(titre);
			    	class_id.setStart_time(Date_start); 
					class_id.setEnd_time(Date_end); 
			    	class_id.setPhone(string_phone);
			    	class_id.setNotes(string_notes);
			    	
			    	// message post traitement
			    	Toast.makeText(getApplicationContext(), "Modifications enregistrées", Toast.LENGTH_LONG).show();
			        
 				}
 	    });
        
        Button btn_supprimer = (Button) findViewById(2);
        btn_supprimer.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					//Toast.makeText(getApplicationContext(), "Supprimer", Toast.LENGTH_LONG).show();
 					delete_appoitntment();
 				}
 	    });
		
        
        Button btn_quitter = (Button) findViewById(3);
        btn_quitter.setOnClickListener(new OnClickListener() 
 	    {
 				@Override
 				public void onClick(View arg0) 
 				{
 					finish();
 				}
 	    });
        
	}
	
	
	public void delete_appoitntment()
    {
    	
							AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

							alertDialogBuilder.setTitle("Confirmation");
			
							// set dialog message
							alertDialogBuilder
							.setMessage("Click yes to delete !")
							.setCancelable(false)
							.setPositiveButton("Yes",new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,int id)
								{
									Toast.makeText(getApplicationContext(), "Rendez-vous supprimer !.", Toast.LENGTH_LONG).show();
									db.delete_appointment(sid); 
									finish();
								}
							})
							
							.setNegativeButton("No",new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,int id)
								{
									dialog.cancel();
									//Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_LONG).show();
								}
							});

							// create alert dialog
							AlertDialog alertDialog = alertDialogBuilder.create();

							// show it
							alertDialog.show();
    }
	
	
	
	public LinearLayout createLayout(int Orientation)
	  {
			LinearLayout lay = new LinearLayout(this);		
			lay.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			lay.setOrientation(Orientation);
			return lay;
	  }
	
	public View widget()
	{
		
	 		LinearLayout layMain = createLayout(LinearLayout.VERTICAL);  	
	 		layMain.setPadding(8, 8, 8, 8);
	 		
	 		
	 	   View childView2 = getLayoutInflater().inflate(R.layout.custom02, null);
	        layMain.addView(childView2);	
	 		

	 		LinearLayout layMargin = createLayout(LinearLayout.HORIZONTAL);
	 		LinearLayout layMargin2 = createLayout(LinearLayout.HORIZONTAL);
	 		
	 		layContent = createLayout(LinearLayout.VERTICAL);
	 		layContent2 = createLayout(LinearLayout.VERTICAL);

	 		layMargin.getLayoutParams().height = 18;
	 		layMargin2.getLayoutParams().height = 18;
			
	 		
	 	/*	RelativeLayout.LayoutParams imageViewParam = new RelativeLayout.LayoutParams(
	                RelativeLayout.LayoutParams.WRAP_CONTENT,
	                RelativeLayout.LayoutParams.WRAP_CONTENT);
	 	*/	
	 		ImageView imageView = new ImageView(this);
	 		imageView.setImageResource(R.drawable.appointment);
	 		//imageView.setGravity( Gravity.CENTER);  
	 		//imageView.setLeft(left);  
	 		//imageView.setX(10);
	 		//imageViewParam.addRule(RelativeLayout.ALIGN_LEFT);
	 		//imageView.setPadding(20,20,20,20);


	 		//imageView.setX(20);
	 		//imageView.setY(75);
	 		//imageView.setPadding(10, 50, 0, 50);
	 		//layMain.addView(imageView);

	 		// http://stackoverflow.com/questions/12238164/how-to-programmatically-align-an-android-textview-to-the-right-of-another-textvi
	 		
	 		//http://stackoverflow.com/questions/33759256/how-can-i-display-programmatically-a-textview-next-to-a-button-in-a-relativelayo?noredirect=1&lq=1
	 		
	 		//android align view programmatically example
	 		
	 		// https://android--code.blogspot.fr/2015/05/android-relativelayout-leftof.html
	 		
	 		//http://www.mysamplecode.com/2011/10/android-programmatically-generate.html
	 		
	 		// https://android--code.blogspot.fr/2015/05/android-relativelayout-leftof.html
	 		
	 		// android textview align parent right programmatically
	 		
	 		
	 		LayoutParams layoutParams=new LayoutParams(75, 75);
	 	    layoutParams.setMargins(10,10,0,0);
	 	    imageView.setLayoutParams(layoutParams);
	 		layMain.addView(imageView);

	 		// TextView ffff = new TextView(this);
		    // ffff.setText("Zorro");
	 		//ffff.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
	        //layMain.addView(ffff);

	 		
	 		 LinearLayout ll = new LinearLayout(this);
	         ll.setOrientation(LinearLayout.HORIZONTAL);
	             
	            // Create TextView
	            TextView product = new TextView(this);
	            product.setText("sdqsddq");
	            product.setPadding(0,0,25,0);
	            ll.addView(product);
	             
	            // Create TextView
	            TextView price = new TextView(this);
	            price.setText("ujujuuuu");
	            ll.addView(price);
	            layMain.addView(ll);
	 		
/*	            
	            LinearLayout la = new LinearLayout(this);
		        la.setOrientation(LinearLayout.VERTICAL);
	            EditText mess = new EditText(this);
	            mess.setWidth(250);
	            mess.setHeight(50);
	            mess.setLines(4);
	            mess.setGravity(Gravity.TOP);
	            mess.setSingleLine(false);
	            mess.setMaxLines(4);
	            mess.setBackgroundColor(0xccc00000);
	            la.addView(mess);
	            layMain.addView(la);
*/	            
	            
	            
	    		EditText et = new EditText(this);
	    		et.setVerticalScrollBarEnabled(true);
	    		et.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
	    		et.setSingleLine(false);
	            et.setMaxLines(4);
	            et.setBackgroundColor(0xccc00000); // #d3d7b6
	            et.setBackgroundColor(Color.parseColor("#e7eecc"));
	    		//layMain.addView(et);
	            
	            //http://www.dreamincode.net/forums/topic/326868-scrollbar-in-a-edittext-dynamicallyprogrammatically-in-android/
	            
	            
	            EditText notes = new EditText(this);
		        notes.setWidth(250);
		        notes.setLines(4);
		        notes.setGravity(Gravity.TOP);
	        	notes.setVerticalScrollBarEnabled(true);
	        	notes.isVerticalScrollBarEnabled();
		        notes.setSingleLine(false);
		        notes.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
	        	notes.setMaxLines(4);
		        notes.setBackgroundColor(Color.parseColor("#e744cc"));  // 0xfff00000
		        //layMain.addView(notes);    
	            
		        /*	        
		        <EditText
	    		android:id="@+id/edit1"
	     		android:background="#d3d7b6"
	    		android:layout_width="250dp"
	    		android:layout_height="100dp"
	    		android:scrollbars="vertical"
	    		android:inputType="textMultiLine" 
	    		android:maxLines="3"
	    		android:gravity="top"
	    		android:layout_marginBottom="10dp"
	            android:layout_marginLeft="20dp"
	            android:layout_marginRight="20dp"
	            android:layout_marginTop="20dp"
	    	/>
		        
	*/	        
		        
		        View childView = getLayoutInflater().inflate(R.layout.custom01, null);
		        layMain.addView(childView);	
		        
		        
		        /***************************/
		        
	 		
	        TextView tv = new TextView(this);
	        tv.setText("Edfqffdvenement");
	        tv.setPadding(10, 10, 0, 0);
	        layMain.addView(tv);
	        
	        final EditText editText = new EditText(this);
	        //editText.setHint("XXXXT85itre");
	        editText.setText(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);
		        
         
	        Button btn = new Button(this);
	        btn.setText("Ajouter");
	        layMain.addView(btn);
	        
	/*        
	        ScrollView m_Scroll;
	        m_Scroll = new ScrollView(this);
	        m_Scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
	                                           LayoutParams.FILL_PARENT));
	        m_Scroll.addView( layMain, new
	        LayoutParams(LayoutParams.FILL_PARENT,
	              LayoutParams.FILL_PARENT) );
	 */       
	        
	       
	      	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();
				        
					  	
						
			}});
		  	
	  	

	  	return layMain;
		
	}
	
	
}