package mercuryretrograde.findyourfate.com; 


import android.annotation.SuppressLint; 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; 
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode; 
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout; 
import android.widget.ScrollView;
import android.widget.TextView;
public class contentmercury extends Activity
{  

    final Context context = this;
    int fsize,ssize;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String name = "fontsize";  
    public static SharedPreferences sharedpreferences;
	
	@SuppressLint("NewApi") 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		  StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); 
		  super.onCreate(savedInstanceState);
		 	         String text = null;
	         

		 	       DisplayMetrics metrics = new DisplayMetrics(); 
		 	        Activity activity = (Activity) context;
		 	        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		 	         
		 	        if (metrics.densityDpi <= DisplayMetrics.DENSITY_LOW) {
		 	        	//Toast.makeText(this, "low screen",Toast.LENGTH_LONG).show();
		 	        	ssize=2;
		 	        	 
		 	        } else if (metrics.densityDpi <= DisplayMetrics.DENSITY_HIGH) {
		 	        	// Toast.makeText(this, "high screen",Toast.LENGTH_LONG).show();
		 	        	ssize=3;
		 	        	 
		 	        }
		 	        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM) {
		 	        	// Toast.makeText(this, "medium screen",Toast.LENGTH_LONG).show();
		 	        	ssize=3;
		 	        	 
		 	        }
		 	        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XHIGH) {
		 	        	 //Toast.makeText(this, "xLarge screen",Toast.LENGTH_LONG).show();
		 	        	ssize=3;
		 	        	 
		 	        }
		 	        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXHIGH) {
		 	        	// Toast.makeText(this, "xxLarge screen",Toast.LENGTH_LONG).show();
		 	        	ssize=4;
		 	        	 
		 	        }
		 	        else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXXHIGH) {
		 	        	 
		 	        	ssize=5;
		 	        	 
		 	        }
		 	        else {
		 	        	 
		 	        	ssize=3;
		 	         }
		 		        String fontkey=getfontName(); 
		 		          if (fontkey != null) 
		 		        {
		 		       	  if(fontkey.equals("Small"))
		 		       	  {
		 		       		  fsize=1;
		 		       	  }
		 		       	  else if(fontkey.equals("Medium"))
		 		       	  {
		 		       		  fsize=2;
		 		       	  }
		 		       	  else if(fontkey.equals("Large"))
		 		       	  {
		 		       		  fsize=3;
		 		       	  }
		 		       	if(fontkey.equals("Larger"))
		 		       	  {
		 		       		  fsize=4;
		 		       	  }
		 		    	if(fontkey.equals("X-Large"))
		 		       	  {
		 		       		  fsize=5;
		 		       	  }
		 		        }
		 		        else
		 		        {
		 		   	   fsize=ssize;  
		 		        }
	        

						 
			 final RelativeLayout.LayoutParams rview = new RelativeLayout.LayoutParams(
				      RelativeLayout.LayoutParams.WRAP_CONTENT, 
				      RelativeLayout.LayoutParams.MATCH_PARENT);
				   

				//  rview.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				  //rview.addRule(RelativeLayout.CENTER_IN_PARENT);
				  
				  final RelativeLayout relativeLayout=new RelativeLayout(this);
				  int sdk = android.os.Build.VERSION.SDK_INT;
				  if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
					  relativeLayout.setBackgroundDrawable( getResources().getDrawable(R.drawable.bg));
				  } else {
					  relativeLayout.setBackground( getResources().getDrawable(R.drawable.bg));
				  }
		        ScrollView home_scroll = new ScrollView(this); 
			   // home_scroll.setBackgroundColor(Color.BLACK);  
		    	LinearLayout layout=new  LinearLayout(this);
		        layout.setOrientation(LinearLayout.VERTICAL);
		        layout.setGravity(Gravity.TOP);
			 
		             WebView content;
		             content=new WebView(this);
		             content.getSettings().setSupportZoom(true); 
		             content.getSettings().setBuiltInZoomControls(true);
		             text = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url('file:///android_asset/arial.ttf')}body {font-family: MyFont; text-align: justify;}</style></head><body>"+"<p align=\"center\"><font color=\"#e2ca72\" size=\""+fsize+"\" ><br><br><br>What is Mercury Retrograde?</font></p>"+
		                    ""+"<p align=\"justify\"><font color=\"#ffffff\" size=\""+fsize+"\" >"+
		                    ""+"All the planets of the solar system move in the same direction around the Sun, each with a different rate of speed. Mercury's orbit is 88 days long; therefore approximately 4 orbits of Mercury around the Sun equals 1 Earth year.<br><br>"+
		                    ""+"Occasionally, Mercury as like a few other planets appears to slow down, then stop, and then move slowly backwards for several weeks and this is termed as retrograde. Eventually, it appears to stop again and reverse direction moving slowly forward once again going Direct. Eventually Mercury appears to return to its normal orbital speed.<br><br>"+
		                    ""+"Why does this typical phenomenon take place? This occurs because Mercury travels faster than the Earth, and it periodically catches up with Earth, passing us by. When Mercury 'goes retrograde' it does not actually slow down, stop and move backwards. It only appears to do so. The retrograde henomenon has to do with the relative speed of the Earth and Mercury as well as their relationship to each other at a particular point in their orbits. There are three Mercury Retrograde periods each year, each lasting around 3 weeks.<br><br><br><br>"+ 
		                    ""+"</font></p> "+ "</body></html>";
		             content.setBackgroundColor(0);
		             content.loadDataWithBaseURL("file:///android_res/drawable/",text, "text/html", "utf-8",null);  
		             layout.addView(content);
		            
		             home_scroll.addView(layout);
		  		   relativeLayout.addView(home_scroll); 

		  		  
		  	
		  setContentView(relativeLayout);
		             
	              }
	

	

/*
@Override
     public void onBackPressed() {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setMessage("Are you sure you want to leave this App.")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    	Intent int_home1 = new Intent(contentmercury.this, MercuryRetrogradeActivity.class);
            int_home1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(int_home1);
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         dialog.cancel();
                    }
                });
         AlertDialog alert = builder.create();
         alert.setIcon(R.drawable.alert);
         alert.show();

     }
     */
protected String getfontName()
{
	 
	 String restoredText=null;
	 sharedpreferences=getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
      if (sharedpreferences.contains(name))
      {
    	  restoredText = sharedpreferences.getString("fontsize", name); 
       
    	 
      
      }


return restoredText;
}

  
 
	@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
		getMenuInflater().inflate(R.menu.main_menu, menu);
        return true; 
         
    } 
	 
	 

	@SuppressLint("NewApi")
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()== android.R.id.home)
		 {
	            Intent int_home = new Intent(this, MercuryRetrogradeActivity.class);
	            int_home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(int_home);
		 }
		 else if(item.getItemId()== R.id.icon_abt)
	     {	 
		            Intent int_abt = new Intent(this, contentmercury.class);
		            int_abt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		            startActivity(int_abt);
		 }
		 else if(item.getItemId()==R.id.icon_find)
		 {	 
		 Intent int_tday = new Intent(this, todaymercury.class);
		        int_tday.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        startActivity(int_tday);
		 }
		 else if(item.getItemId()==R.id.icon_tday)
		 {
			            Intent int_cal = new Intent(this, calendar.class);
			            int_cal.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			            startActivity(int_cal);
		 }       
		 else if(item.getItemId()==R.id.icon_sign)
		 {
			            Intent int_sign = new Intent(this, zodiacsign.class);
			            int_sign.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			            startActivity(int_sign);
		 } 
		 
		 
		
		
		 else if(item.getItemId()==R.id.late)
		 {
			 
			/* Intent intent = new Intent(Intent.ACTION_VIEW);
			    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			    startActivity(intent);
			  */
		 } 
		 else if (item.getItemId() == R.id.cfont) { 
				initiatePopupWindow();
			}
		 else if(item.getItemId()==R.id.icon_exit)
		 {
	    			
			 
		         AlertDialog.Builder builder = new AlertDialog.Builder(this);
		         builder.setMessage("Are you sure you want to leave this App.")
		                .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.setTitle("Close App.");
		         alert.setIcon(R.drawable.alert);
		         alert.show();

		      
			 
			 
		 }
		 else if(item.getItemId()==R.id.icon_set)
		 {
		        	Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
	        	 emailIntent.setType("text/plain");
	        	 emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Mercury Retrograde - findyourfate.com");
	        	 emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hi,I would like to suggest, this Mercury Retrograde App to you.Click this link to download App http://goo.gl/5IZD3z"+"\n\n"+" Regards");
		     	     	 startActivity(Intent.createChooser(emailIntent, "Share via")); 
		 }
		 else
		 {
		 }
		        	 
		            return true;
	}
	 

	 private void initiatePopupWindow() {
	  		
 		 LayoutInflater layoutInflater 
 	     = (LayoutInflater)getBaseContext()
 	      .getSystemService(LAYOUT_INFLATER_SERVICE);  
 	    final View popupView = layoutInflater.inflate(R.layout.fontchange, null);  
 	             final PopupWindow popupWindow = new PopupWindow(
 	               popupView, 
 	               LayoutParams.WRAP_CONTENT,  
 	                     LayoutParams.WRAP_CONTENT);  
 	           
 	             Button btnDismiss = (Button)popupView.findViewById(R.id.btncancel);
 	           final RadioGroup radioSexGroup = (RadioGroup)popupView.findViewById(R.id.rgOpinion);
 	         Button btnDisplay = (Button)popupView.findViewById(R.id.btnDisplay); 
 	     	RadioButton s = (RadioButton)popupView.findViewById(R.id.small);
 	     	RadioButton m= (RadioButton)popupView.findViewById(R.id.normal);
 	     	RadioButton l= (RadioButton)popupView.findViewById(R.id.large);
 	     	RadioButton ll= (RadioButton)popupView.findViewById(R.id.larger);
 	     	RadioButton xl= (RadioButton)popupView.findViewById(R.id.xlarge);
 	     	sharedpreferences=getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
		      if (sharedpreferences.contains(name))
		      {
		       String fontsize=sharedpreferences.getString("fontsize", name);
		        if(fontsize.equals("Small"))
		        {
		        	s.setChecked(true);
		        }
		        else if(fontsize.equals("Medium"))
		        {
		        	m.setChecked(true);
		        }
		        else if(fontsize.equals("Large"))
		        {
		        	l.setChecked(true);
		        }
		        else if(fontsize.equals("Larger"))
		        {
		        	ll.setChecked(true);
		        }
		        else if(fontsize.equals("X-Large"))
		        {
		        	xl.setChecked(true);
		        }
		    	  
		        else
		        {
		        	m.setChecked(true);
		        }
		      } 

 	             btnDismiss.setOnClickListener(new Button.OnClickListener(){

 	     @Override
 	     public void onClick(View v) {
 	      // TODO Auto-generated method stub
 	      popupWindow.dismiss();
 	     }});
 	             
 	           btnDisplay.setOnClickListener(new OnClickListener() {
			    	  
			  		@Override
			  		public void onClick(View v) {
			  			 
			  			 
			  			int selectedId = radioSexGroup.getCheckedRadioButtonId();  
			  			RadioButton radioSexButton = (RadioButton)popupView.findViewById(selectedId);
			   String fontsize=radioSexButton.getText().toString();
			  			//Toast.makeText(fontchange.this,"vv"+fontsize, Toast.LENGTH_SHORT).show();
			  			
			   Editor editor = sharedpreferences.edit(); 
			  			 editor.putString(name, fontsize); 
			  		      editor.commit();
			  		    popupWindow.dismiss();
			  		  Intent icon_home1 = new Intent(contentmercury.this, contentmercury.class);
				    	icon_home1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			            startActivity(icon_home1);
			  			 
			  		}
			   
			  	});
 	               
 	           popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
 	             popupWindow.showAsDropDown(popupView, 50, -30);
	 }
}
