package com.example.widget02;


import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import java.util.ArrayList;
import java.util.List;


import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * Created by pratap.kesaboyina on 10-06-2015.
 */
public class MainActivity extends Activity {


    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;
  		
  		TimeWidgetSlider timeSliderHour = null;
  		TimeWidgetSlider timeSliderMinutes = null;
    
    /*    */
    
    
    
    
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dynamic_form);

        // To set Margin for the child Views
       params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        params.setMargins(5, 5, 5, 5);

        // LinearLayout acts as parent Layout , we will add child Views to this Layout dynamically
        layout1 = (LinearLayout) findViewById(R.id.layout1);
        // Sample Data for Spinner
        ArrayList<String> spinnerList = new ArrayList<String>();
        spinnerList.add("Select");
        spinnerList.add("Hyderabad");
        spinnerList.add("Banglore");
        spinnerList.add("Chennai");
        spinnerList.add("Delhi");
        spinnerList.add("Mumbai");

// create edittext dynamically , added to LinearLayout
        createEditText("First Name");
        createEditText("Last Name");
        createEditText("Age");
        
        setContentView(widget());
		
        createEditText("Address");
        createSpinner(spinnerList);
        createEditText("State");


// create spinners dynamically , added to LinearLayout


// create checkbox dynamically , added to LinearLayout
        createCheckBox("Key Contact");
        createCheckBox("Target contact");
        saveButton();

    }

// create a button to show/save data , entered in the Form
    private void saveButton() {
        Button saveButton = new Button(this);
        saveButton.setHeight(WRAP_CONTENT);
        saveButton.setText("Save");
        saveButton.setOnClickListener(submitListener);
        layout1.addView(saveButton,params);
    }

    // Access the value of the EditText

    private View.OnClickListener submitListener = new View.OnClickListener() {
        public void onClick(View view) {
            StringBuilder stringBuilder = new StringBuilder();
            for (View singView : allViews) {

                String className = Utils.getClassName(singView.getClass());

                if (className.equalsIgnoreCase("EditText")) {
                    EditText editText = (EditText) singView;
                    stringBuilder.append(" "+editText.getText().toString());
                } else if (className.equalsIgnoreCase("Spinner")) {
                    Spinner spiner = (Spinner) singView;
                    stringBuilder.append(" "+spiner.getSelectedItem());
                }
                else if (className.equalsIgnoreCase("CheckBox")) {
                    CheckBox checkBox = (CheckBox) singView;
                    stringBuilder.append(" "+checkBox.isChecked());
                }

            }
            Log.i("All Data ", stringBuilder.toString());

            Utils.showAlertDialog(view.getContext(), "Data", stringBuilder.toString());
        }
    };


    public void createEditText(String hint) {
        EditText editText = new EditText(this);
        editText.setId(viewsCount++);
        editText.setHint(hint);
        allViews.add(editText);
        layout1.addView(editText,params);

    }

    public void createSpinner(List<String> spinnerList) {



        Spinner spinner = new Spinner(this);
        spinner.setId(viewsCount++);
        spinner.setBackgroundResource(R.drawable.dropdown_normal_holo_light);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerList);
        spinner.setAdapter(spinnerArrayAdapter);
        allViews.add(spinner);
        layout1.addView(spinner,params);
    }


    public void createCheckBox(String label) {

        final CheckBox checkBox = new CheckBox(this);
        checkBox.setId(viewsCount++);
        checkBox.setText(label);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
        allViews.add(checkBox);
        layout1.addView(checkBox,params);
    }

    
    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 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, b24HourMode, TimeWidgetSlider.STYPE_HOURS, iSliderViewWidth);
			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("Dynamic Text!");
	        layMain.addView(tv);
	 
			
			
			
	  	layMain.addView(layContent);
	  	
	  //	layMain.addView(layContentBottom);
	 // 	layMain.addView(layBottomControls);  	

	  	return layMain;
		
	}

    


}
