package com.androidbegin.buttonclicktutorial;

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.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.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.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
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.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
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;

public class AddEvent02 extends Activity
{

	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; 
		
		TimeWidgetSlider start_timeSliderHour = null;
		TimeWidgetSlider start_timeSliderMinutes = null;
		TimeWidgetSlider end_timeSliderHour = null;
		TimeWidgetSlider end_timeSliderMinutes = null;
		
		DB_alarm db;
		private SQLiteDatabase dataBase;
		
	
	@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");
        Toast.makeText(getBaseContext(),  date_event , Toast.LENGTH_LONG).show();

        setContentView(widget());
		
	//	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();
				
			}
		});
*/		
	}
	
	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 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);
		        
         
	        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;
	}

	
	

	

}
