package findyourfate.com.porutham;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import android.content.SharedPreferences;
import android.annotation.SuppressLint;
import android.view.View; // Import for the View class
//import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import androidx.appcompat.widget.Toolbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;

import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class contentdetails extends AppCompatActivity {
	
	public static final String MyPREFERENCES = "MyPrefs";
    public static final String name = "fontsize";
    SharedPreferences sharedpreferences;  
	private static Typeface mTypeface;
	final contentdetails context = this;

	ImageView image;

	TextView t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23,
			t24;
	int fsize, ssize;

	@SuppressLint("NewApi")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.details);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
		// Initialize Firebase
		FirebaseDatabase database = FirebaseDatabase.getInstance();
		DatabaseReference ref = database.getReference("Description");
		ref.setValue("Description");

		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 = 13;

		} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_HIGH) {
			// Toast.makeText(this, "high screen",Toast.LENGTH_LONG).show();
			ssize = 17;

		} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM) {
			// Toast.makeText(this, "medium screen",Toast.LENGTH_LONG).show();
			ssize = 20;

		} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XHIGH) {
			// Toast.makeText(this, "xLarge screen",Toast.LENGTH_LONG).show();
			ssize = 23;

		} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXHIGH) {
			// Toast.makeText(this, "xxLarge screen",Toast.LENGTH_LONG).show();
			ssize = 25;

		} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_XXXHIGH) {

			ssize = 28;

		} else {

			ssize = 17;

		}

		String fontkey = getfontName();
		if (fontkey != null) {
			if (fontkey.equals("Small")) {
				fsize = 13;
			} else if (fontkey.equals("Medium")) {
				fsize = 17;
			} else if (fontkey.equals("Large")) {
				fsize = 21;
			}
			if (fontkey.equals("Larger")) {
				fsize = 25;
			}
			if (fontkey.equals("X-Large")) {
				fsize = 28;
			}
		} else {
			fsize = ssize;
		}

		t1 = (TextView) findViewById(R.id.textView1);
		t2 = (TextView) findViewById(R.id.textView2);
		t3 = (TextView) findViewById(R.id.textView3);
		t4 = (TextView) findViewById(R.id.textView4);
		t5 = (TextView) findViewById(R.id.textView5);
		t6 = (TextView) findViewById(R.id.textView6);
		t7 = (TextView) findViewById(R.id.textView7);
		t8 = (TextView) findViewById(R.id.textView8);
		t9 = (TextView) findViewById(R.id.textView9);
		t10 = (TextView) findViewById(R.id.textView10);
		t11 = (TextView) findViewById(R.id.textView11);
		t12 = (TextView) findViewById(R.id.textView12);
		t13 = (TextView) findViewById(R.id.textView13);
		t14 = (TextView) findViewById(R.id.textView14);
		t15 = (TextView) findViewById(R.id.textView15);
		t16 = (TextView) findViewById(R.id.textView16);
		t17 = (TextView) findViewById(R.id.textView17);
		t18 = (TextView) findViewById(R.id.textView18);
		t19 = (TextView) findViewById(R.id.textView19);
		t20 = (TextView) findViewById(R.id.textView20);
		t21 = (TextView) findViewById(R.id.textView21);
		t22 = (TextView) findViewById(R.id.textView22);
		t23 = (TextView) findViewById(R.id.textView23);
		t24 = (TextView) findViewById(R.id.textView24);


		// Optionally, configure toolbar settings
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

		mTypeface = Typeface.createFromAsset(contentdetails.this.getAssets(), "fonts/ARIAL.TTF");
		t1.setTextSize(fsize);
		t1.setTypeface(mTypeface);
		t2.setTextSize(fsize);
		t2.setTypeface(mTypeface);
		t3.setTextSize(fsize);
		t3.setTypeface(mTypeface);
		t4.setTextSize(fsize);
		t4.setTypeface(mTypeface);
		t5.setTextSize(fsize);
		t5.setTypeface(mTypeface);
		t6.setTextSize(fsize);
		t6.setTypeface(mTypeface);
		t7.setTextSize(fsize);
		t7.setTypeface(mTypeface);
		t8.setTextSize(fsize);
		t8.setTypeface(mTypeface);
		t9.setTextSize(fsize);
		t9.setTypeface(mTypeface);
		t10.setTextSize(fsize);
		t10.setTypeface(mTypeface);
		t11.setTextSize(fsize);
		t11.setTypeface(mTypeface);
		t12.setTextSize(fsize);
		t12.setTypeface(mTypeface);
		t13.setTextSize(fsize);
		t13.setTypeface(mTypeface);
		t14.setTextSize(fsize);
		t14.setTypeface(mTypeface);
		t15.setTextSize(fsize);
		t15.setTypeface(mTypeface);
		t16.setTextSize(fsize);
		t16.setTypeface(mTypeface);
		t17.setTextSize(fsize);
		t17.setTypeface(mTypeface);
		t18.setTextSize(fsize);
		t18.setTypeface(mTypeface);
		t19.setTextSize(fsize);
		t19.setTypeface(mTypeface);
		t20.setTextSize(fsize);
		t20.setTypeface(mTypeface);
		t21.setTextSize(fsize);
		t21.setTypeface(mTypeface);
		t22.setTextSize(fsize);
		t22.setTypeface(mTypeface);
		t23.setTextSize(fsize);
		t23.setTypeface(mTypeface);
		t24.setTextSize(fsize);
		t24.setTypeface(mTypeface);

	}



	
	 

	protected String getfontName() {
	    String restoredText = null;
	    /*
	    // Declare the SharedPreferences variable
	    SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
	    
	    // Check if the preferences contain the key
	    if (sharedPreferences.contains("fontsize")) {
	        // Retrieve the stored font size; "name" is used as a default value here
	        restoredText = sharedPreferences.getString("fontsize", "defaultFontSize");
	        
	        // Optional: Display a Toast message for debugging
	        // Toast.makeText(this, "FontSize Changed: " + restoredText, Toast.LENGTH_LONG).show();
	    }
	    */
	    return restoredText;
	}


	/*
	 * @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_home4 = new Intent(contentdetails.this,index.class);
	 * int_home4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	 * startActivity(int_home4);
	 * }
	 * })
	 * .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();
	 * }
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu from XML
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(@NonNull MenuItem item) {
		// Handle item selection
		int itemId = item.getItemId();

		if (itemId == android.R.id.home) {
			Intent int_abt = new Intent(this, index.class);
			int_abt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(int_abt);
			return true;
		} else if (itemId == R.id.claculator){ // Correct the ID spelling here
			Intent icon_calc = new Intent(this, poruthamcalculator.class);
			icon_calc.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(icon_calc);
			return true;
		} else if (itemId == R.id.Nakshatra) {
			Intent icon_find = new Intent(this, findnakshatra.class);
			icon_find.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(icon_find);
			return true;
		} else if (itemId == R.id.guru) {
			AlertDialog.Builder alt_compat = new AlertDialog.Builder(this);
			String desc = "App to find out when Guru Peyarchi or Jupiter transit takes place.";
			alt_compat.setTitle("Guru Peyarchi");
			alt_compat.setMessage(desc)
					.setCancelable(false)
					.setPositiveButton("Later", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					})
					.setNegativeButton("Never/Already", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					})
					.setNeutralButton("Open", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							Intent browserIntent = new Intent(Intent.ACTION_VIEW,
									Uri.parse("market://details?id=findyourfate.com.gurupeyarchi"));
							startActivity(browserIntent);
						}
					});

			AlertDialog alert = alt_compat.create();
			alert.show();
			return true;
		} else if (itemId == R.id.cfont) {
			initiatePopupWindow();
			return true;
		} else if (itemId == R.id.share) {
			Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
			emailIntent.setType("text/plain");
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "10 Porutham by findyourfate.com");
			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
					"Hi, I would like to suggest this 10 Porutham App to you. Click this link to download the app http://goo.gl/38FZui"
							+ "\n\n" + " Regards");
			startActivity(Intent.createChooser(emailIntent, "Share using"));
			return true;
		} else if (itemId == 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 alert1 = builder.create();
			alert1.setTitle("Close App.");
			alert1.setIcon(R.drawable.alert);
			alert1.show();
			return true;
		} else {
			return super.onOptionsItemSelected(item);
		}


	}

	private void initiatePopupWindow() {

		LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		final android.view.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() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				popupWindow.dismiss();
			}


		});

		btnDisplay.setOnClickListener(new OnClickListener() {

			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(contentdetails.this, contentdetails.class);
				icon_home1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(icon_home1);

			}



		});

		popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
		popupWindow.showAsDropDown(popupView, 50, -30);
	}

	/*
	 * try {
	 * LayoutInflater inflater =
	 * (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
	 * View popupView = inflater.inflate(R.layout.fontchange, null);
	 * final PopupWindow popupWindow = new PopupWindow(
	 * popupView,
	 * LayoutParams.WRAP_CONTENT,
	 * LayoutParams.WRAP_CONTENT);
	 * 
	 * 
	 * popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
	 * 
	 * 
	 * } catch (Exception e) {
	 * e.printStackTrace();
	 * }
	 * }
	 */

}