package astrology.findyourfate.com;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;


public class nadifirstpage extends Fragment {
	
	public nadifirstpage(){}
EditText fn,lastname,address,city,dat,email,conemail;
	Button btn;
	RadioGroup pay;
	Spinner precountry,country;
	 WebView webView;
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.nadi, container, false);
        fn=(EditText)rootView.findViewById(R.id.firstname);
        lastname=(EditText)rootView.findViewById(R.id.lastname);
        address=(EditText)rootView.findViewById(R.id.address);
	     city=(EditText)rootView.findViewById(R.id.city);
	     dat= (EditText)rootView.findViewById(R.id.dateofbirth);
	     email=(EditText)rootView.findViewById(R.id.email);
	     country=(Spinner)rootView.findViewById(R.id.countr);
	     conemail=(EditText)rootView.findViewById(R.id.cemail);
           webView = (WebView)rootView.findViewById(R.id.myview); 
       // final String fn1=fn.getText().toString();
        btn=(Button)rootView.findViewById(R.id.button1);
         final AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity()); 
        btn.setOnClickListener(new OnClickListener()
        {
        public void onClick(View v)
            {
        	
        	
        	if(fn.getText().toString().length() == 0 )
        	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter your first name")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						 fn.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 fn.setFocusable(true);
        	}	
        	
        	else if(lastname.getText().toString().length() == 0 )
        	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter your last name")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						lastname.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 lastname.setFocusable(true);
        	}	
        	
        	else if(address.getText().toString().length() == 0 )
        	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter your address")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						address.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 address.setFocusable(true);
        	}	
        	else if(city.getText().toString().length() == 0 )
        	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter your city")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						city.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 city.setFocusable(true);
        	}	
        	
        	else if(dat.getText().toString().length() == 0 )
        	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter your date of birth")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						dat.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 dat.setFocusable(true);
        	}	
        	else if (!email.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.[a-z]+"))
            {
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter valid email address")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						email.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		email.setFocusable(true);
            }
        	else if (!conemail.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.[a-z]+"))
            {
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter confirmation email address")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						conemail.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		conemail.setFocusable(true);
            }
        	else if(!(conemail.getText().toString().length() == email.getText().toString().length()))
        						            	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please enter correct confirmation email address")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						conemail.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 conemail.setFocusable(true);
        	}	
        	else if(!(conemail.getText().toString().length() == email.getText().toString().length()))
        	{
        		alt_bld.setTitle("Alert Message");
        		alt_bld.setMessage("Please enter correct confirmation email address")
        		.setCancelable(false)
        		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        			public void onClick(DialogInterface dialog, int id) {
        				conemail.requestFocus(); 
        			}
        		});
        		AlertDialog alert = alt_bld.create();
        		alert.setTitle("Alert Message");
        		alert.setIcon(R.drawable.alert);
        		alert.show();
        		conemail.setFocusable(true);
        	}	
        	else if(country.getSelectedItem().toString().equals("--Select--"))
        	{
        		 alt_bld.setTitle("Alert Message");
        		 alt_bld.setMessage("Please select your Country")
        		 .setCancelable(false)
        					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        					 public void onClick(DialogInterface dialog, int id) {
        						 country.requestFocus(); 
        					 }
        					 });
        		 AlertDialog alert = alt_bld.create();
        		 alert.setTitle("Alert Message");
        		 alert.setIcon(R.drawable.alert);
        		 alert.show();
        		 country.setFocusable(true);
        	}
       		else if( !(fn.getText().toString().length() == 0) || !(lastname.getText().toString().length() == 0) ||!(city.getText().toString().length() == 0) || !country.getSelectedItem().toString().equals("--Select--")||!(dat.getText().toString().length() == 0)||!(email.getText().toString().length() == 0)|| !(conemail.getText().toString().length() == 0) ) 	
        	{
        		
     	   Intent intent = new Intent();
            intent.setClass(getActivity(), nadireading.class);
           intent.putExtra("firstname", fn.getText().toString());
           intent.putExtra("lastname",lastname.getText().toString());
           intent.putExtra("address",address.getText().toString());
           intent.putExtra("city",city.getText().toString());
           intent.putExtra("country",country.getSelectedItem().toString());
           intent.putExtra("dob",dat.getText().toString());
           intent.putExtra("email",email.getText().toString());
           intent.putExtra("confemail",conemail.getText().toString());
       
         startActivity(intent);
        	}
            }
        });
       
        final Calendar myCalendar = Calendar.getInstance();

        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }
            private void updateLabel() {

	            String myFormat = "dd/MM/yyyy"; //In which you need put here
	            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

	            dat.setText(sdf.format(myCalendar.getTime()));
            }
      };

         dat.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(getActivity(), date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                }
            });
        precountry = (Spinner)rootView.findViewById(R.id.countr);  
        
        String[] items = getResources().getStringArray(R.array.country_array);
      
        
        final ArrayList<String> list = new ArrayList<String>();
         for (int i = 0; i < items.length; ++i) {
           list.add(items[i]);
         }
      final StableArrayAdapter adapter = new StableArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item, list);
         
         precountry.setAdapter(adapter);
        return rootView;
    }
	 class StableArrayAdapter extends ArrayAdapter<String> {

		    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

		    public StableArrayAdapter(Context context, int textViewResourceId,
		        List<String> objects) {
		      super(context, textViewResourceId, objects);
		      for (int i = 0; i < objects.size(); ++i) {
		        mIdMap.put(objects.get(i), i);
		      }
		    }

		    @Override
		    public long getItemId(int position) {
		      String item = getItem(position);
		      
		      return mIdMap.get(item);
		    }

		    @Override
		    public boolean hasStableIds() {
		      return true;
		    }

		  }
		}



