package astrology.findyourfate.com;

import java.io.File;

import com.amazon.device.ads.AdLayout;
import com.amazon.device.ads.AdRegistration;
import com.amazon.device.ads.AdTargetingOptions;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ShareActionProvider;
import android.widget.TextView;

public class setuppage extends Activity {
	 TextView dob,zodiac,edit;
	 String fname="myfile.xml";
	 private Context context;
	 LinearLayout layouts;
	// private AdView mAdview;
	 private AdLayout adView;
	 private ShareActionProvider mShareActionProvider;
	 private EasyTracker easyTracker = null;
	 
	public void onCreate(Bundle savedInstanceState) { 
		 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); 
        	super.onCreate(savedInstanceState);
        	setContentView(R.layout.findzodiac);
        	
  		   // mAdview = (AdView) findViewById(R.id.adView);
	       // mAdview.setAdListener(new ToastAdListener(this));
        	AdRegistration.setAppKey("fbce94eb2ff347e5a855d4496106f125");
		 	this.adView = (AdLayout) findViewById(R.id.ad_view);
			AdTargetingOptions adOptions = new AdTargetingOptions();
			AdRegistration.enableLogging(false);
			 AdRegistration.enableTesting(false);
	        easyTracker = EasyTracker.getInstance(setuppage.this);
        	layouts=(LinearLayout) findViewById(R.id.layout);
        	
        	 final LinearLayout layout=new  LinearLayout(this);
    		 layout.setOrientation(LinearLayout.VERTICAL);
    		 layout.setGravity(Gravity.CENTER);
    		 layout.setPadding(40, 50, 40, 30);
    			// layout.setPadding(left, top, right, bottom);
    		
    		 //layout.setBackgroundColor(Color.WHITE);
    	 	String dat=getIntent().getStringExtra("date");
        	String sign = getIntent().getStringExtra("key"); 
        	
          dob = new TextView(this);
   		  dob.setGravity(Gravity.LEFT);
   		  dob.setPadding(30, 75, 30, 15);
   		  edit = new TextView(this);
   		 if(dat!=null)
   		 {
   		 dob.setText(Html.fromHtml("<font color=\"#000000\" size=\"10\" face=\"ms sans serif, verdana\">"+"<h1>"+"Your  Date  of  Birth :- "+dat+"</h1>"+"</font>"));
   	     layout.addView(dob);
   		 }
   	    zodiac = new TextView(this);
   	  	zodiac.setGravity(Gravity.LEFT);
   	    zodiac.setPadding(30, 0, 30, 15);
	  	zodiac.setText(Html.fromHtml("<font color=\"#000000\" size=\"10\" face=\"ms sans serif, verdana\">"+"<h1>"+"Your  Zodiac  Sign  is :- "+sign+"</h1></font>"));
   		layout.addView(zodiac);
   		 
   		 edit = new TextView(this);
   		 edit.setGravity(Gravity.RIGHT);
   		 edit.setPadding(0, 0, 30, 0);
   		 edit.setText(Html.fromHtml("<font color=\"#000000\" size=\"10\" face=\"ms sans serif, verdana\">"+"<h3><u>"+"Tap to Edit"+"</u></h3></font><br>"));
   	     layout.addView(edit);
   	     edit.setOnClickListener(new OnClickListener() {
   	    	 @Override
      	public void onClick(View v) {
      			Intent intent = new Intent(setuppage.this,astology.class);
      			startActivity(intent);   
      		  }
      		  });   
   		// mAdview.loadAd(new AdRequest.Builder().build());
   	     this.adView.loadAd(adOptions);
   	     layouts.removeAllViews();
		 layouts.addView(layout);
     	}
	@Override
    public void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
    }
   @Override
    public void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }
    @Override
	public void onBackPressed() {
	       	  setuppage.this.finish();
	}
	 @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			MenuInflater menuInflater = getMenuInflater();
	        menuInflater.inflate(R.menu.main, menu);
	        return super.onCreateOptionsMenu(menu);
		}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		 if (item.getItemId() == android.R.id.home)
		 {
			Intent int_d = new Intent(this,MainActivity.class);
            int_d.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(int_d);
		 }
	         else if (item.getItemId() ==R.id.drawer)
	         {
				 Intent int_dr = new Intent(this,MainActivity.class);
			     int_dr.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			     startActivity(int_dr);
	         			}
				   else if (item.getItemId() ==R.id.service)
				     {
				       Intent int_ser = new Intent(this,ourservices.class);
				       int_ser.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				       startActivity(int_ser);
				         }
	            else if (item.getItemId() ==R.id.astrology)
	            {
				File myFile = new File(getFilesDir() + "/" + fname);
				    if (myFile.exists()) 
				        {
					        try {
					    	 FileInputStream file = new FileInputStream(new File(getFilesDir() + "/" + fname));
						     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
						     DocumentBuilder builder =  builderFactory.newDocumentBuilder();
						     Document xmlDocument = builder.parse(file);
						     XPath xPath =  XPathFactory.newInstance().newXPath();
						     System.out.println("*************************");
						          String   expression = "/root/zodiac";
						            System.out.println(expression);
						            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
						            for (int i = 0; i < nodeList.getLength(); i++) {
						               String value=nodeList.item(i).getLastChild().getNodeValue(); 
						               if(value.length() == 0)
						               {
						               Intent int_ast = new Intent(this,astology.class);
							            int_ast.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							            startActivity(int_ast);
						               }
						               else
						               {
										Intent in= new Intent(this,horoscope.class);
										  in.putExtra("key", value);
									      in.putExtra("modeday","Today");							
										  startActivity(in);
						          	}
						  		}   
					        	}catch (FileNotFoundException e) {
						            e.printStackTrace();
								} catch (SAXException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (ParserConfigurationException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (XPathExpressionException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}			
 
				            }
				                    
					        else
				            {
				            	 Intent int_ast = new Intent(this,astology.class);
						            int_ast.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						            startActivity(int_ast);
					       }
		}
		else if (item.getItemId() == R.id.setup)
		{
			File myFile1 = new File(getFilesDir() + "/" + fname);
			    if (myFile1.exists()) 
				            {
					        	
					        	try {  	
					        	FileInputStream file = new FileInputStream(new File(getFilesDir() + "/" + fname));
						               
						            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
						             
						            DocumentBuilder builder =  builderFactory.newDocumentBuilder();
						             
						            Document xmlDocument = builder.parse(file);
						 
						            XPath xPath =  XPathFactory.newInstance().newXPath();
						   
						            System.out.println("*************************");
						            String   expression = "/root/zodiac";
						            System.out.println(expression);
						            String   expressio = "/root/dob";
							        System.out.println(expressio);
							         
							              NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
							            
							            for (int i = 0; i < nodeList.getLength(); i++) {
							            	
							                     	
							               String value=nodeList.item(i).getFirstChild().getNodeValue(); 
							               
							             
							               NodeList nodeList1 = (NodeList) xPath.compile(expressio).evaluate(xmlDocument, XPathConstants.NODESET);
								            
								            for (int i1 = 0; i1 < nodeList1.getLength(); i1++) {
								            	
								                     	
								               String val=nodeList1.item(i1).getLastChild().getNodeValue(); 
								             
								     if(value.length() == 0)
						               {
						               Intent int_ast = new Intent(this,astology.class);
							            int_ast.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							            startActivity(int_ast);
						               }
						               else
						               {
										Intent in= new Intent(this,setuppage.class);
										 in.putExtra("date",val);
										  in.putExtra("key", value);
										  startActivity(in);
							           }
						            }   
							     }
				           }catch (FileNotFoundException e) {
					            e.printStackTrace();
							} catch (SAXException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (ParserConfigurationException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (XPathExpressionException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
				            }
						          
				            else
				            {
				            	 Intent int_ast = new Intent(this,astology.class);
						            int_ast.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						            startActivity(int_ast);
				            }       
		}
							
		 else if (item.getItemId() == R.id.exit)
		  {
			 AlertDialog.Builder builder = new AlertDialog.Builder(this);
			    builder.setMessage("Are you sure  want to exit?")
			           .setCancelable(false)
			           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			               public void onClick(DialogInterface dialog, int id) {
			            	   Intent intent = new Intent(Intent.ACTION_MAIN);
			   		    	intent.addCategory(Intent.CATEGORY_HOME);
			   		    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			   		    	startActivity(intent);
			               }
			           })
			           .setNegativeButton("No", new DialogInterface.OnClickListener() {
			               public void onClick(DialogInterface dialog, int id) {
			                    dialog.cancel();
			               }
			           });
			    AlertDialog alert = builder.create();
			    alert.show();
		  }
		/* else if (item.getItemId() == R.id.share)
		 {
				Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		    	emailIntent.setType("text/plain");
		    	emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Astrology by findyourfate.com");
		        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"Hi,I would like to suggest, this  Astrology by findyourfate.com App to you. \n Click this Link to Download app \"http://goo.gl/iexPxk\"\n\n Regards");
		    	startActivity(emailIntent);  
			
		 }	
		 else if (item.getItemId() == R.id.in)
		  {
			  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(setuppage.this);
			    alertDialogBuilder.setTitle("Horoscope Compatibility");
			
			           alertDialogBuilder.setMessage("Compatibility of zodiac signs for love and romantic relationship. Search what zodiac signs are compatible. Find quality results here!No two sun signs are alike and totally compatible. A degree of incompatibility always exists because that is how we have been made.Marriages are made in heaven BUT!!! Compatible Love Matches are made here through this app.The paid in-app compatibility report is \"the best buy\".");
			     alertDialogBuilder.setPositiveButton("Open",new DialogInterface.OnClickListener() {
			        	   
			        	                   public void onClick(DialogInterface dialog,int id) {
			        	                	   final String appPackageName = "compatibility.findyourfate.com"; // getPackageName() from Context or Activity object
			        	                	   try {
			        	                	       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
			        	                	   } catch (android.content.ActivityNotFoundException anfe) {
			        	                	       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
			        	                	   }
			        	                   }
			        	                   });
			          alertDialogBuilder.setNeutralButton("Already/Never",new DialogInterface.OnClickListener() {
			                  public void onClick(DialogInterface dialog,int id) {	  
			                    //  MainActivity.this.finish();
			                	  dialog.cancel();
			                  }
			  
			              });
			  
			            // set negative button: No message
					alertDialogBuilder.setNegativeButton("Later",new DialogInterface.OnClickListener() {
		
			                  public void onClick(DialogInterface dialog,int id) {
			                      // cancel the alert box and put a Toast to the user
			                      dialog.cancel();                    
			                  }
			
			              });
			           AlertDialog alertDialog = alertDialogBuilder.create();
			 
			           // show alert
			           alertDialog.show();			  
			      } */
			else
			{
				
			}
 			return true;
		}
	}

