package virgo.findyourfate.com;

import java.net.URL;
import java.util.Calendar;

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.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.StrictMode;

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;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class monthlyh 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.monthly, container, false);
			   String userAgent = new WebView(getActivity()).getSettings().getUserAgentString();
	    	   if (userAgent.contains("Mobile")) {
	    		   rootView = inflater.inflate(R.layout.monthly, container, false);
	    	   } 
	    	   else {
	    		   rootView = inflater.inflate(R.layout.monthlytab, 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);
	      
	        	  
	        	   nextyearcontent=(TextView) rootView.findViewById(R.id.nextyear);
	        	   prevyrcontent=(TextView)rootView.findViewById(R.id.prevyear);
	        	   
	        	   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);
	        	  
	       		daily.setBackgroundResource(R.drawable.daily1);
				week.setBackgroundResource(R.drawable.weekly1);
				month.setBackgroundResource(R.drawable.monthlypress);
				year.setBackgroundResource(R.drawable.yearly1);
				
				
				  nextyearcontent.setVisibility(View.GONE);
		    	   prevyrcontent.setVisibility(View.GONE);
		    	   
		    	   
		    	   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();

		                }
		            });
		            
		    	   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);     
		    	   
	             	    
	             	   if (moth==1)
	   			     {
	   			     	  mname1="jan";
	   			     	  month_name1="January";
	   			     }
	   			     else if(moth==2)
	   			     {
	   			     	mname1="feb";
	   			     	month_name1="February";
	   			     }
	   			     else if(moth==3)
	   			     {
	   			     	mname1="mar";
	   			     	month_name1="March";
	   			     }
	   			     else if(moth==4)
	   			     {
	   			     	mname1="apr";
	   			     	month_name1="April";
	   			     }
	   			     else if(moth==5)
	   			     {
	   			     	mname1="may";
	   			     	month_name1="May";
	   			     }
	   			     else if(moth==6)
	   			     {
	   			     	mname1="jun";
	   			     	month_name1="June";
	   			     }
	   			     else if(moth==7)
	   			     {
	   			     	mname1="jul";
	   			     	month_name1="July";
	   			     }
	   			     else if(moth==8)
	   			     {
	   			     	mname1="aug";
	   			     	month_name1="August";
	   			     }
	   			     else if(moth==9)
	   			     {
	   			     	mname1="sep";
	   			     	month_name1="September";
	   			     }
	   			     else if(moth==10)
	   			     {
	   			     	mname1="oct";
	   			     	month_name1="October";
	   			     }
	   			     else if(moth==11)
	   			     {
	   			     	mname1="nov";
	   			     	month_name1="November";
	   			     }
	   			     else if(moth==12)
	   			     {
	   			     	mname1="dec";
	   			     	month_name1="December";
	   			     } 
	   			      
		     	   
		    					URL url2 = null;
		    	  				try {
		    	  				   url2 = new URL("https://www.findyourfate.com/android-horoscope/monthly/"+yr+"/"+mname1+".xml");
		    	  			       DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance();
		    	  	  	      	   DocumentBuilder db2 = null;
		    	  			 	   db2 = dbf2.newDocumentBuilder();
		    	  			 	   Document doc2 = null;
		    	  			 	   doc2 = db2.parse(new InputSource(url2.openStream()));
		    	  			   	   doc2.getDocumentElement().normalize();
		    	  	  	      	   NodeList nodeList2 = doc2.getElementsByTagName("item");
		    	  	  	       
		    	  	  	      	  WebSettings webSettings2 = content.getSettings(); 
		    	  	  		      webSettings2.setSupportZoom(true);
		    	  	  		      webSettings2.setBuiltInZoomControls(true);
		    	  	  			   
		    	  	  		     Node node2 = nodeList2.item(5);
		    	  	         Element monthelement = (Element) node2;
		    	  	        NodeList contentList2 = monthelement.getElementsByTagName("content");
		    	  	      	Element contentElement2 = (Element) contentList2.item(0);
		    	  	      	contentList2 = contentElement2.getChildNodes();
		    	  	  			   
		    	  	  		 String text;
		    	  	  		sign = "Monthly Horoscope";
		    	  				// String userAgent = new WebView(getActivity()).getSettings().getUserAgentString();
		    	  				  if (userAgent.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:#090034; text-align: justify;}</style></head><body bgcolor=\"\"><h4 align=\"center\">"+" Monthly Horoscope <br>"+"<b><font size=\""+2+"\">"+month_name1+","+yr+"</font></b></h4>"+ "<p align=\"justify\">" +"<font color=\"#000000\" size=\""+3+"\" face=\"ms sans serif, verdana\">"+ ((Node) contentList2.item(0)).getNodeValue()+ "</font></p> "+ "</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:#090034; text-align: justify;}</style></head><body bgcolor=\"\"><h2 align=\"center\">"+" Monthly Horoscope <br>"+"<b><font size=\""+4+"\">"+month_name1+","+yr+"</font></b></h2>"+ "<p align=\"justify\">" +"<font color=\"#000000\" size=\""+5+"\" face=\"ms sans serif, verdana\">"+ ((Node) contentList2.item(0)).getNodeValue()+ "</font></p> "+ "</body></html>";
		    	  				  		 }
		    	  				content.loadDataWithBaseURL("file:///android_res/drawable/",text, "text/html", "utf-8",null);  
		    	  			 	  
		    	  				}
		    	  				  catch (Exception e) {
		    	  	             	 System.out.println("XML Pasing Excpetion = " + e);
		    	  	       	   }
		    	  				
		
		    	  				
		    	  				
		    	  				 daily.setOnClickListener(new View.OnClickListener() {
		    	  					
		    	  					@Override
		    	  					public void onClick(View v) {
		    	  						// TODO Auto-generated method stub
		    	  						 daily.setClickable(false);
		    	  						  Fragment newFragment = new horoscope();  
		    	  						 	FragmentTransaction transaction = getFragmentManager().beginTransaction();
		    	  					 	transaction.replace(R.id.monthview, 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.monthview, newFragment);  
		    	  							transaction.commit();  

		    	  					}
		    	  				});
		    	  			 
		    	  			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.monthview, newFragment);  
		    	  						transaction.commit();  
		    	  				}
		    	  			});
		    	  				
		    	  				
			  
		
			  return rootView ;
}
}