package astrology.findyourfate.com;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;


import java.util.Calendar;
import java.util.Locale;

import org.json.JSONException;

import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;

public class dreamanalysis extends Fragment{
	
	public dreamanalysis(){}
	  EditText dat;
	  EditText firstname,lastname,email,conemail,address,datedream,typeofdreamt;
		 RadioGroup pay;
		 Button btn;
		 WebView webView;
			private static final String CONFIG_ENVIRONMENT = PaymentActivity.ENVIRONMENT_NO_NETWORK;
			   private static final String CONFIG_CLIENT_ID = "GB7QEWLT4JRQ6";
			    private static final String CONFIG_RECEIVER_EMAIL = "stanley@findyourfate.com"; 

		 
	  public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
	 	
	        View rootView = inflater.inflate(R.layout.dreamanalysis_layout, container, false);
	        webView = (WebView)rootView.findViewById(R.id.myview); 
	        
	        dat=(EditText)rootView.findViewById(R.id.dateofbirth);
	        
	        firstname=(EditText)rootView.findViewById(R.id.firstname);
		     lastname=(EditText)rootView.findViewById(R.id.lastname);
		     email=(EditText)rootView.findViewById(R.id.email);
		     conemail=(EditText)rootView.findViewById(R.id.cemail);
		     address=(EditText)rootView.findViewById(R.id.address);
		     datedream=(EditText)rootView.findViewById(R.id.dateofbirth);
		     typeofdreamt=(EditText)rootView.findViewById(R.id.type);
		     pay=(RadioGroup)rootView.findViewById(R.id.payable);
	        
		      final AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
		       
		     lastname.setVisibility(View.VISIBLE);
		      
		        btn = (Button)rootView .findViewById(R.id.payment);
		        btn.setOnClickListener(new View.OnClickListener() {
		            @Override
		            public void onClick(View v) {
		            	
		            	  String user = email.getText().toString();
		            	  String fname=firstname.getText().toString();
		            	  String lname=lastname.getText().toString();
		            	  String cemail=conemail.getText().toString();
		            	  String dat=datedream.getText().toString();
		            	  String dreamtype=typeofdreamt.getText().toString();
		            	  String addre=address.getText().toString();
		            	  
		            	  
			            	String format= "First Name :" +fname +"<br />"
			            	+"Last Name :" +lname + "<br />"
			            	+"E-mail Address : " +user + "<br/>"
			            	+"Confirm E-mail Address : " +cemail + "<br/>"
			            	+"Address : " +addre + "<br/>"
			            	+"Date of Dream : " +dat + "<br/>"
			            	+"Type what you Dreamt? : " +dreamtype + "<br/>"
			                +"Details : PayPal - USD 10 "  + "<br/>";
			            
			            	if(firstname.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) {
			            						 firstname.requestFocus(); 
			            					 }
			            					 });
			            		 AlertDialog alert = alt_bld.create();
			            		 alert.setTitle("Alert Message");
			            		 alert.setIcon(R.drawable.alert);
			            		 alert.show();
			            		 firstname.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(email.getText().toString().length() == 0 )
			            	{
			            		 alt_bld.setTitle("Alert Message");
			            		 alt_bld.setMessage("Please enter your 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().length() == 0 )
			            	{
			            		 alt_bld.setTitle("Alert Message");
			            		 alt_bld.setMessage("Please enter your confirm email id")
			            		 .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(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(datedream.getText().toString().length() == 0 )
			            	{
			            		 alt_bld.setTitle("Alert Message");
			            		 alt_bld.setMessage("Please enter your date of dream")
			            		 .setCancelable(false)
			            					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
			            					 public void onClick(DialogInterface dialog, int id) {
			            						datedream.requestFocus(); 
			            					 }
			            					 });
			            		 AlertDialog alert = alt_bld.create();
			            		 alert.setTitle("Alert Message");
			            		 alert.setIcon(R.drawable.alert);
			            		 alert.show();
			            		 datedream.setFocusable(true);
			            	}	
			            	else if(typeofdreamt.getText().toString().length() == 0 )
			            	{
			            		 alt_bld.setTitle("Alert Message");
			            		 alt_bld.setMessage("Please enter your type of dreamt?")
			            		 .setCancelable(false)
			            					 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
			            					 public void onClick(DialogInterface dialog, int id) {
			            						typeofdreamt.requestFocus(); 
			            					 }
			            					 });
			            		 AlertDialog alert = alt_bld.create();
			            		 alert.setTitle("Alert Message");
			            		 alert.setIcon(R.drawable.alert);
			            		 alert.show();
			            		 typeofdreamt.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().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( !(firstname.getText().toString().length() == 0) || !(lastname.getText().toString().length() == 0)||!(email.getText().toString().length() == 0) || !(conemail.getText().toString().length() == 0) || !(address.getText().toString().length() == 0)||!(datedream.getText().toString().length() == 0)||!(typeofdreamt.getText().toString().length() == 0) )
			            	{
			            		
			            		
			            		 try {   
		            GMailSender sender = new GMailSender(user,format,"Dream Analysis - Confirmation Details  - findyourfate.com apps" );
		                 } catch (Exception e) {   
			                         Log.e("SendMail", e.getMessage(), e);   
			                     } 
			            		 Intent intent = new Intent(getActivity(), PayPalService.class);
		            	         
			            	        intent.putExtra(PaymentActivity.EXTRA_PAYPAL_ENVIRONMENT, CONFIG_ENVIRONMENT);
			            	        intent.putExtra(PaymentActivity.EXTRA_CLIENT_ID, CONFIG_CLIENT_ID);
			            	        intent.putExtra(PaymentActivity.EXTRA_RECEIVER_EMAIL, CONFIG_RECEIVER_EMAIL);
			            	         
			            	        startService(intent);
			            	        
			            	  	  PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal("10.00"), "USD", "Ask a Question- Payment Confirmation Details - findyourfate.com Apps");
			  	            	
			            	        Intent intent1 = new Intent(getActivity(), PaymentActivity.class);
			            	         
			            	        intent1.putExtra(PaymentActivity.EXTRA_PAYPAL_ENVIRONMENT, CONFIG_ENVIRONMENT);
			            	        intent1.putExtra(PaymentActivity.EXTRA_CLIENT_ID, CONFIG_CLIENT_ID);
			            	        intent1.putExtra(PaymentActivity.EXTRA_RECEIVER_EMAIL, CONFIG_RECEIVER_EMAIL);
			            	         
			            	        // It's important to repeat the clientId here so that the SDK has it if Android restarts your 
			            	        // app midway through the payment UI flow.
			            	        intent1.putExtra(PaymentActivity.EXTRA_CLIENT_ID, "credential-from-developer.paypal.com");
			            	        intent1.putExtra(PaymentActivity.EXTRA_PAYER_ID, "your-customer-id-in-your-system");
			            	        intent1.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
			            	         
			            	        startActivityForResult(intent1, 0);
		   	           }
						}

					private void startService(Intent intent) {
						// TODO Auto-generated method stub
						
					}
	          });
	        
	        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();
	                }
	            });
	        return rootView;
	  }
	  public void onActivityResult (int requestCode, int resultCode, Intent data) {
          if (resultCode == Activity.RESULT_OK) {
              PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
              if (confirm != null) {
                  try {
                      Log.i("paymentExample", confirm.toJSONObject().toString(4));

                      // TODO: send 'confirm' to your server for verification.
                      // see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
                      // for more details.

                  } catch (JSONException e) {
                      Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                  }
              }
          }
          else if (resultCode == Activity.RESULT_CANCELED) {
              Log.i("paymentExample", "The user canceled.");
              getActivity().finish();
              Intent int_spa = new Intent(getActivity(),ourservices.class);
 	            int_spa.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 	            startActivity(int_spa);
          }
          else if (resultCode == PaymentActivity.RESULT_PAYMENT_INVALID) {
              Log.i("paymentExample", "An invalid payment was submitted. Please see the docs.");
          }
      }
	  @Override
	    public void onDestroy() {
	        stopService(new Intent(getActivity(), PayPalService.class));
	        super.onDestroy();
	    }

	private void stopService(Intent intent) {
		
	}
	
		 
	  
	  
		        }

