package com.example.widget01;


import java.util.ArrayList;
import java.util.Calendar;
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;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity
{

	private Button btnStartTime = null;
	private Calendar dateStart = null;
	
	
    
    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 Button btnNone = null;
		private Button btnOK = null;
		
		//fields
		private boolean bNoneButton = true;
		private boolean b24HourMode = false;
		private int iHour = -1;
		private int iMinutes = -1;
		
		
		private TextView infos; 
		
		TimeWidgetSlider timeSliderHour = null;
		TimeWidgetSlider timeSliderMinutes = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);

	  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()
	  {
	  	iHour = timeSliderHour.getValue();
	  	iMinutes = 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);
	 		layContent = 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;

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

		//	timeSliderHour.setTimeChangeEvent(mOnTimeChangeEvent);
		//	timeSliderMinutes.setTimeChangeEvent(mOnTimeChangeEvent);		

			layContent.addView(timeSliderHour);
			layContent.addView(layMargin);
			layContent.addView(timeSliderMinutes);
			
	 // 	layMain.addView(layTopControls);
	 // 	layMain.addView(layContentTop);
			
			
			// add text view
	        TextView tv = new TextView(this);
	        tv.setText("Evenement");
	        layMain.addView(tv);
	 
	        
	        EditText editText = new EditText(this);
	        editText.setId(viewsCount++);
	        editText.setHint("hint");
	        layMain.addView(editText);
	        
	        editText = new EditText(this);
	        editText.setId(viewsCount++);
	        editText.setHint("hint");
	        layMain.addView(editText);
	        
	        
			
	        tv = new TextView(this);
	        tv.setText("Start Time");
	        tv.setPadding(0, 30, 0, 30);
	        layMain.addView(tv);
	        
	        
	        infos = new TextView(this);
	        infos.setText("toto");
	        layMain.addView(infos);

	        
	        layContent.setPadding(100, 0, 0, 0);
	        layMain.addView(layContent);
	        
	        
	  	//layMain.setX(300);
	  	//layMain.setY(300);
	  	
	  	
	  
         
	        Button btn = new Button(this);
	        btn.setText("Button");
	        layMain.addView(btn);
	        
	        // lancer la dialogbox
	      	btn.setOnClickListener(new Button.OnClickListener()
		  	{
					public void onClick(View arg0)
					{
						getDataFromSliders();
						OnClose();
			}});
		  	
	  	
	  //	layMain.addView(layContentBottom);
	 // 	layMain.addView(layBottomControls);  	

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

	public void clearTime()
	{
		iHour = -1;
		iMinutes = -1;
	}

	
	
	public void updateLabelCaption()
	{				
		//infos.setTime(timeSliderHour.getValue(), timeSliderMinutes.getValue());
		int hour = timeSliderHour.getValue();
		int min = timeSliderMinutes.getValue();
		String.valueOf(timeSliderHour.getValue());
		String.valueOf(timeSliderMinutes.getValue());
		
		String s = String.valueOf(timeSliderHour.getValue()) + " " + String.valueOf(timeSliderMinutes.getValue());
 		infos.setText(s);
		
	}
	
	private TimeWidgetSlider.OnTimeChange mOnTimeChangeEvent = new TimeWidgetSlider.OnTimeChange()
	{
		public void OnChange(TimeWidgetSlider slider)
		{
			updateLabelCaption();
			Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
		}		
	};
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) 
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
