 package scorpio.findyourfate.com;

import java.net.URL; 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; 
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.StrictMode;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; 
 
public class horoscope extends Fragment {

	 String stext = null;
     WebView content; 
     String sign=null ;
 	 String fromdate=null;
 	 String todate =null;
 	 String weekmonth,weekyear;
  	 View rootView;
  	 TextView nextyearcontent,prevyrcontent;
  	 String yrcontent=null;
  	 Button daily,week,month,year;
     String mname1 = null;
	 String month_name1=null;

  	 
     @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
    	   
    	   StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); 
    	   rootView = inflater.inflate(R.layout.horoscope, container, false);
 	 
    	   String userAgent = new WebView(getActivity()).getSettings().getUserAgentString();
    	   if (userAgent.contains("Mobile")) {
    		   rootView = inflater.inflate(R.layout.horoscope, container, false);
    		   
    	    } 
    	   else {
    		   rootView = inflater.inflate(R.layout.horoscopetab, container, false);
   	  		    
    	   }
    	   
    	 
    	   content = (WebView)rootView.findViewById(R.id.content);
         	content.setBackgroundColor(0);
        	 WebChromeClient webChromeClient=new WebChromeClient();
       	  content.setWebChromeClient(webChromeClient);
       	  content.getSettings().setBuiltInZoomControls(true);
       	  content.getSettings().setDisplayZoomControls(false);
          	
           daily=(Button)rootView.findViewById(R.id.dailyv);
     	   week=(Button)rootView.findViewById(R.id.weeklyv);
     	   month=(Button)rootView.findViewById(R.id.monthlyv);
     	   year=(Button)rootView.findViewById(R.id.yearlyv);
     	
       nextyearcontent=(TextView) rootView.findViewById(R.id.nextyear);
 	   prevyrcontent=(TextView)rootView.findViewById(R.id.prevyear);
 	   
 		nextyearcontent.setVisibility(View.VISIBLE);
 		prevyrcontent.setVisibility(View.GONE);
 		
	    ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService (Context.CONNECTIVITY_SERVICE);
    	String error1;
    	    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) {
    		   } else {
    		  error1= "<html><head> <style type=\"text/css\">@font-face {font-family: MyFont; src: url(\"file:///android_asset/fonts/SEGOEPR.TTF\")}body { font-family: MyFont;  text-align: justify;font-size:larger; color:#000000; font-weight:bold; line-height:32px; }</style></head><body><p align=\"center\" style=\"padding-top:100px; padding-bottom:100px;\"><font  color=\"#000000\"  size=\"+2\" ><b> Unable to Download Feed <br />Check Your Data/WIFI Connection </b></font></p></body></html>";
     		  content.loadDataWithBaseURL("file:///android_res/drawable/", error1, "text/html", "utf-8",null); 
     	   }
    	    
    	    final Calendar cal = Calendar.getInstance();
       		final int moth=cal.get(Calendar.MONTH) + 1 ;
       	    final int date=cal.get(Calendar.DAY_OF_MONTH) ;
       	    final int yr=cal.get(Calendar.YEAR);
       	    
       	    
    		GregorianCalendar gc = new GregorianCalendar();
    		gc.add(Calendar.DATE, 1);	
    		  final int nxtdate=gc.get(Calendar.DAY_OF_MONTH) ;
    		  final int nxtmonth=gc.get(Calendar.MONTH) + 1 ;
    		  final int nxtyr=gc.get(Calendar.YEAR);
    		
     
       	   if (moth==1 || nxtmonth==1 )
    		     {
    		     	  mname1="jan";
    		     	  month_name1="January";
    		     }
    		     else if(moth==2|| nxtmonth==2)
    		     {
    		     	mname1="feb";
    		     	month_name1="February";
    		     }
    		     else if(moth==3|| nxtmonth==3)
    		     {
    		     	mname1="mar";
    		     	month_name1="March";
    		     }
    		     else if(moth==4 || nxtmonth==4)
    		     {
    		     	mname1="apr";
    		     	month_name1="April";
    		     }
    		     else if(moth==5 || nxtmonth==5) 
    		     {
    		     	mname1="may";
    		     	month_name1="May";
    		     }
    		     else if(moth==6 || nxtmonth==6)
    		     {
    		     	mname1="jun";
    		     	month_name1="June";
    		     }
    		     else if(moth==7 || nxtmonth==7)
    		     {
    		     	mname1="jul";
    		     	month_name1="July";
    		     }
    		     else if(moth==8 || nxtmonth==8)
    		     {
    		     	mname1="aug";
    		     	month_name1="August";
    		     }
    		     else if(moth==9 || nxtmonth==9)
    		     {
    		     	mname1="sep";
    		     	month_name1="September";
    		     }
    		     else if(moth==10 || nxtmonth==10)
    		     {
    		     	mname1="oct";
    		     	month_name1="October";
    		     }
    		     else if(moth==11 || nxtmonth==11)
    		     {
    		     	mname1="nov";
    		     	month_name1="November";
    		     }
    		     else if(moth==12|| nxtmonth==12)
    		     {
    		     	mname1="dec";
    		     	month_name1="December";
    		     } 
    		      
                 
       		daily.setBackgroundResource(R.drawable.dailypress);
    		week.setBackgroundResource(R.drawable.weekly1);
    		month.setBackgroundResource(R.drawable.monthly1);
    		year.setBackgroundResource(R.drawable.yearly1);
    		
    	    
          	  final ProgressDialog progressDialog = new ProgressDialog(getActivity());
         	  progressDialog.setTitle("Please wait");
               progressDialog.setMessage("Loading Content...");
                  progressDialog.setCancelable(true);
               progressDialog.show();

               content.setWebViewClient(new WebViewClient() {
                   @Override
                   public boolean shouldOverrideUrlLoading(WebView view, String url) {
                       view.loadUrl(url);
                       return true;
                   }

                   @Override
                   public void onPageFinished(WebView view, String url) {
                       if (progressDialog.isShowing()) {
                           progressDialog.dismiss();
                       }
                   }

                   @Override
                   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                       Toast.makeText(getActivity(), "Error:" + description, Toast.LENGTH_SHORT).show();

                   }
               });
            
      	 URL url = null;
	     
	  		try {
				url = new URL("https://www.findyourfate.com/android-horoscope/daily/"+yr+"/"+mname1+"/"+yr+moth+date+".xml");
	 	     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			 DocumentBuilder db = null;
			 db = dbf.newDocumentBuilder();
			 Document doc = null;
			 doc = db.parse(new InputSource(url.openStream()));
			 // TODO Auto-generated catch block
			 // TODO Auto-generated catch block
		 	  doc.getDocumentElement().normalize();
			  NodeList nodeList = doc.getElementsByTagName("item");
			  WebSettings webSettings = content.getSettings(); 
		      webSettings.setSupportZoom(true);
		      webSettings.setBuiltInZoomControls(true);
		      Node node = nodeList.item(7); 
			  Element fstElmnt = (Element) node;
			  NodeList contentList = fstElmnt.getElementsByTagName("content");
			  Element contentElement = (Element) contentList.item(0);
			  contentList = contentElement.getChildNodes();
			 	 	
			 		String 	text = null;
		  				 
		  				String userAgent1 = new WebView(getActivity()).getSettings().getUserAgentString();
		  				if (userAgent1.contains("Mobile")) {
		  					 text = "<html><head><style type=\"text/css\"> @font-face {font-family: MyFont;src: url('file:///android_asset/fonts/MTCORSVA.TTF')}body {font-family: MyFont;color:#290000; text-align: justify;}</style></head><body bgcolor=\"\"><h4 align=\"center\">"+" Scorpio-Daily Horoscope <br>"+"<b><font size=\""+2+"\">"+month_name1+" "+date+" ,"+yr+"</font></b></h4>"+ "<table border='0'><tr><td><p align=\"justify\">" +"<font color=\"#000000\" size=\""+3+"\"   face=\"ms sans serif, verdana\">"+ ((Node) contentList.item(0)).getNodeValue()+ "</font></p> </td></tr></table>"+ "</body></html>";
		  			 	} 
		  				else {
		  					 text = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url('file:///android_asset/fonts/MTCORSVA.TTF')}body {font-family: MyFont;color:#290000; text-align: justify;}</style></head><body bgcolor=\"\"><h2 align=\"center\">"+" Scorpio-Daily Horoscope <br>"+"<font size=\""+3+"\" >"+month_name1+" "+date+" ,"+yr+"</font></b></h2>"+ "<p align=\"justify\">" +"<font color=\"#000000\" size=\""+5+"\"   face=\"ms sans serif, verdana\">"+ ((Node) contentList.item(0)).getNodeValue()+ "</font></p> "+"</body></html>";
		  	 			}
		  				content.setBackgroundColor(0);
		  				content.loadDataWithBaseURL("file:///android_res/drawable/",text, "text/html", "utf-8",null);  
		 		}
			
			  catch (Exception e) {
		     	 System.out.println("XML Pasing Excpetion = " + e);
			   }
			
   
	  		nextyearcontent.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					nextyearcontent.setVisibility(View.GONE);
					prevyrcontent.setVisibility(View.VISIBLE);
				
					
			 	    
			   	    ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService (Context.CONNECTIVITY_SERVICE);
			       	String error1;
			       	    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected()) {
			       		   } else {
			       		  error1= "<html><head> <style type=\"text/css\">@font-face {font-family: MyFont; src: url(\"file:///android_asset/fonts/SEGOEPR.TTF\")}body { font-family: MyFont;  text-align: justify;font-size:larger; color:#000000; font-weight:bold; line-height:32px; }</style></head><body><p align=\"center\" style=\"padding-top:100px; padding-bottom:100px;\"><font  color=\"#000000\"  size=\"+2\" ><b> Unable to Download Feed <br />Check Your Data/WIFI Connection </b></font></p></body></html>";
			        		  content.loadDataWithBaseURL("file:///android_res/drawable/", error1, "text/html", "utf-8",null); 
			        	   }
			  
			       	    
					
					  URL url = null;
					     
				  		try {
							url = new URL("https://www.findyourfate.com/android-horoscope/daily/"+yr+"/"+mname1+"/"+yr+nxtmonth+nxtdate+".xml");
				 	     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
						 DocumentBuilder db = null;
						 db = dbf.newDocumentBuilder();
						 Document doc = null;
						 doc = db.parse(new InputSource(url.openStream()));
						 // TODO Auto-generated catch block
						 // TODO Auto-generated catch block
					 	  doc.getDocumentElement().normalize();
						  NodeList nodeList = doc.getElementsByTagName("item");
						  WebSettings webSettings = content.getSettings(); 
					      webSettings.setSupportZoom(true);
					      webSettings.setBuiltInZoomControls(true);
					      Node node = nodeList.item(7); 
						  Element fstElmnt = (Element) node;
						  NodeList contentList = fstElmnt.getElementsByTagName("content");
						  Element contentElement = (Element) contentList.item(0);
						  contentList = contentElement.getChildNodes();
						 	 	
						 		String 	text = null;
					  				 sign = "Daily Horoscope Gemini ";
					  				String userAgent1 = new WebView(getActivity()).getSettings().getUserAgentString();
					  				if (userAgent1.contains("Mobile")) {
					  					 text = "<html><head><style type=\"text/css\"> @font-face {font-family: MyFont;src: url('file:///android_asset/fonts/MTCORSVA.TTF')}body {font-family: MyFont;color:#290000; text-align: justify;}</style></head><body bgcolor=\"\"><h4 align=\"center\">"+" Scorpio -Daily Horoscope <br>"+"<b><font size=\""+2+"\">"+month_name1+" "+nxtdate+" ,"+nxtyr+"</font></b></h4>"+ "<table border='0'><tr><td><p align=\"justify\">" +"<font color=\"#000000\" size=\""+3+"\"   face=\"ms sans serif, verdana\">"+ ((Node) contentList.item(0)).getNodeValue()+ "</font></p> </td></tr></table>"+ "</body></html>";
					  			 	} 
					  				else {
					  					 text = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url('file:///android_asset/fonts/MTCORSVA.TTF')}body {font-family: MyFont;color:#290000; text-align: justify;}</style></head><body bgcolor=\"\"><h2 align=\"center\">"+" Scorpio -Daily Horoscope <br>"+"<font size=\""+3+"\" >"+month_name1+" "+nxtdate+" ,"+nxtyr+"</font></b></h2>"+ "<p align=\"justify\">" +"<font color=\"#000000\" size=\""+5+"\"   face=\"ms sans serif, verdana\">"+ ((Node) contentList.item(0)).getNodeValue()+ "</font></p> "+"</body></html>";
					  	 			}
					  				content.setBackgroundColor(0);
					  				content.loadDataWithBaseURL("file:///android_res/drawable/",text, "text/html", "utf-8",null);  
					 		}
						
						  catch (Exception e) {
					     	 System.out.println("XML Pasing Excpetion = " + e);
						   }
							
				  	  final ProgressDialog progressDialog = new ProgressDialog(getActivity());
			     	  progressDialog.setTitle("Please wait");
			           progressDialog.setMessage("Loading Content...");
			              progressDialog.setCancelable(true);
			           progressDialog.show();

			           content.setWebViewClient(new WebViewClient() {
			               @Override
			               public boolean shouldOverrideUrlLoading(WebView view, String url) {
			                   view.loadUrl(url);
			                   return true;
			               }

			               @Override
			               public void onPageFinished(WebView view, String url) {
			                   if (progressDialog.isShowing()) {
			                       progressDialog.dismiss();
			                   }
			               }

			               @Override
			               public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			                   Toast.makeText(getActivity(), "Error:" + description, Toast.LENGTH_SHORT).show();

			               }
			           });
			    
				}
			});  
	  		
	  		prevyrcontent.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					 URL url = null;
				     
						nextyearcontent.setVisibility(View.VISIBLE);
						prevyrcontent.setVisibility(View.GONE);
				 	 
				 try {
							url = new URL("https://www.findyourfate.com/android-horoscope/daily/"+yr+"/"+mname1+"/"+yr+moth+date+".xml");
				 	     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
						 DocumentBuilder db = null;
						 db = dbf.newDocumentBuilder();
						 Document doc = null;
						 doc = db.parse(new InputSource(url.openStream()));
						 // TODO Auto-generated catch block
						 // TODO Auto-generated catch block
					 	  doc.getDocumentElement().normalize();
						  NodeList nodeList = doc.getElementsByTagName("item");
						  WebSettings webSettings = content.getSettings(); 
					      webSettings.setSupportZoom(true);
					      webSettings.setBuiltInZoomControls(true);
					      Node node = nodeList.item(7); 
						  Element fstElmnt = (Element) node;
						  NodeList contentList = fstElmnt.getElementsByTagName("content");
						  Element contentElement = (Element) contentList.item(0);
						  contentList = contentElement.getChildNodes();
						 	 	
						 		String 	text = null;
					  				String userAgent1 = new WebView(getActivity()).getSettings().getUserAgentString();
					  				if (userAgent1.contains("Mobile")) {
					  					 text = "<html><head><style type=\"text/css\"> @font-face {font-family: MyFont;src: url('file:///android_asset/fonts/MTCORSVA.TTF')}body {font-family: MyFont;color:#290000; text-align: justify;}</style></head><body bgcolor=\"\"><h4 align=\"center\">"+" Scorpio - Daily Horoscope <br>"+"<b><font size=\""+2+"\">"+month_name1+" "+date+" ,"+yr+"</font></b></h4>"+ "<table border='0'><tr><td><p align=\"justify\">" +"<font color=\"#000000\" size=\""+3+"\"   face=\"ms sans serif, verdana\">"+ ((Node) contentList.item(0)).getNodeValue()+ "</font></p> </td></tr></table>"+ "</body></html>";
					  			 	} 
					  				else {
					  					 text = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url('file:///android_asset/fonts/MTCORSVA.TTF')}body {font-family: MyFont;color:#290000; text-align: justify;}</style></head><body bgcolor=\"\"><h2 align=\"center\">"+" Scorpio - Daily Horoscope <br>"+"<font size=\""+3+"\" >"+month_name1+" "+date+" ,"+yr+"</font></b></h2>"+ "<p align=\"justify\">" +"<font color=\"#000000\" size=\""+5+"\"   face=\"ms sans serif, verdana\">"+ ((Node) contentList.item(0)).getNodeValue()+ "</font></p> "+"</body></html>";
					  	 			}
					  				content.setBackgroundColor(0);
					  				content.loadDataWithBaseURL("file:///android_res/drawable/",text, "text/html", "utf-8",null);  
					 		}
						
						  catch (Exception e) {
					     	 System.out.println("XML Pasing Excpetion = " + e);
						   }
						
				}
			});  
	  		
	  		     week.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					  week.setClickable(false);
			      	  					
					  Fragment newFragment = new weeklyh();  
					 	FragmentTransaction transaction = getFragmentManager().beginTransaction();
					 	transaction.replace(R.id.container, newFragment);  
						transaction.commit();  	
				}
			});  
			  
		  month.setOnClickListener(new View.OnClickListener() {
				@Override
			public void onClick(View v) {
					// TODO Auto-generated method stub
					  month.setClickable(false);
					  Fragment newFragment = new monthlyh();  
					 	FragmentTransaction transaction = getFragmentManager().beginTransaction();
					 	transaction.replace(R.id.container, newFragment);  
						transaction.commit();  
				}
			});
			  
			year.setOnClickListener(new View.OnClickListener() {
		 		@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
		 			
		 		 		  year.setClickable(false);
						  Fragment newFragment = new yearlyh();  
						 	FragmentTransaction transaction = getFragmentManager().beginTransaction();
						 	transaction.replace(R.id.container, newFragment);  
							transaction.commit();  
	
	
				}
			});
			 
	 		
    return rootView;
 }
	  
}