package findyourfate.com.porutham;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import android.annotation.SuppressLint;
import androidx.appcompat.widget.Toolbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivityTamil extends AppCompatActivity implements OnClickListener {
	private Spinner spinner1;
	private Spinner spinner123;
	TextView mbutton1;

	DBHelper DB = null;
	ImageView image;
	TextView txt, txt1, txt2;
	Typeface tfavv;
	String girltstar, boytstar, star1, star1123;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String userAgent = new WebView(MainActivityTamil.this).getSettings().getUserAgentString();
		if (userAgent.contains("Mobile")) {
			setContentView(R.layout.activity_tamil);

		} else {
			setContentView(R.layout.activity_tamiltab);
		}

		Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

		// Initialize Firebase
		FirebaseDatabase database = FirebaseDatabase.getInstance();
		DatabaseReference ref = database.getReference("Description");
		ref.setValue("Description");

		// testad id
		// mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
 

		txt = (TextView) findViewById(R.id.textView2);
		txt1 = (TextView) findViewById(R.id.textView1);
		txt2 = (TextView) findViewById(R.id.textViews);

		Typeface fontface = Typeface.createFromAsset(getAssets(), "fonts/Bamini.ttf");

		txt.setTypeface(fontface, Typeface.BOLD);
		String tamilString = TamilUtil.convertToTamil(TamilUtil.BAMINI, "ஆண் மற்றும் பெண் நட்சத்திரம்");
		txt.setText(tamilString);

		txt1.setTypeface(fontface, Typeface.BOLD);
		String tamilString1 = TamilUtil.convertToTamil(TamilUtil.BAMINI, "பெண் நட்சத்திரம்");
		txt1.setText(tamilString1);

		txt2.setTypeface(fontface, Typeface.BOLD);
		String tamilString2 = TamilUtil.convertToTamil(TamilUtil.BAMINI, "ஆண் நட்சத்திரம்");
		txt2.setText(tamilString2);

		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner123 = (Spinner) findViewById(R.id.spinner123);
		String array1[] = { "--Nju;e;njL--",
				"mD\\k;",
				"m];tpdp",
				"mtpl;lk;",
				"Mapy;ak;",
				"guzp",
				"rpj;jpiu",
				"m];jk;",
				"fhu;j;jp",
				"Nfl;il",
				"kpUfrPuplk;",
				"%yk;",
				"kfk;",
				"G+uhlk;",
				"G+uk;",
				"G+ul;lhjp",
				"G+rk;",
				"Gdu;G+rk;",
				"Nutjp",
				"Nuhfpzp",
				"rjak;",
				"Rthjp",
				"jpUthjpiu",
				"jpUNthzk;",
				"cj;jpuk;",
				"cj;jpuhlk;",
				"cj;jpul;lhjp",
				"tprhfk;"
		};
		String array123[] = { "--Nju;e;njL--",
				"mD\\k;",
				"m];tpdp",
				"mtpl;lk;-kfu",
				"mtpl;lk;-Fk;g",
				"Mapy;ak;",
				"guzp",
				"rpj;jpiu-fd;dp",
				"rpj;jpiu-Jyh",
				"m];jk;",
				"fhu;j;jp-Nk\\",
				"fhu;j;jp-up\\g",
				"Nfl;il",
				"kpUfrPupl-up\\g",
				"kpUfrPupl-kpJd",
				"%yk;",
				"kfk;",
				"G+uhlk;",
				"G+uk;",
				"G+ul;lhjp-Fk;g",
				"G+ul;lhjp-kPd",
				"G+rk;",
				"Gdu;G+-kpJd",
				"Gdu;G+-flf",
				"Nutjp",
				"Nuhfpzp",
				"rjak;",
				"Rthjp",
				"jpUthjpiu",
				"jpUNthzk;",
				"cj;jpuk;-rpk;kk;",
				"cj;jpuk;-fd;dp",
				"cj;jpuhl-jdR",
				"cj;jpuhl-kfu",
				"cj;jpul;lhjp",
				"tprhf-Jyh",
				"tprhf-tpUr;rp"
		};

		Spinner sp = (Spinner) findViewById(R.id.spinner1);
		Spinner sp123 = (Spinner) findViewById(R.id.spinner123);

		ArrayAdapter<String> sp_adapter = new ArrayAdapter<String>(this,
				R.layout.spinnertext, array1) {
			public View getView(int position, View convertView, android.view.ViewGroup parent) {
				tfavv = Typeface.createFromAsset(getAssets(), "fonts/Bamini.ttf");
				TextView v = (TextView) super.getView(position, convertView, parent);
				v.setTypeface(tfavv);
				// v.setTextColor(Color.BLACK);
				// v.setTextSize(35);
				return v;
			}

			public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) {
				TextView v = (TextView) super.getView(position, convertView, parent);
				v.setTypeface(tfavv);
				v.setTextColor(Color.BLACK);
				// v.setTextSize(35);
				return v;
			}
		};

		ArrayAdapter<String> sp_adapter123 = new ArrayAdapter<String>(this, R.layout.spinnertext, array123) {
			public View getView(int position, View convertView, android.view.ViewGroup parent) {
				tfavv = Typeface.createFromAsset(getAssets(), "fonts/Bamini.ttf");
				TextView v = (TextView) super.getView(position, convertView, parent);
				v.setTypeface(tfavv);
				// v.setTextColor(Color.BLACK);
				// v.setTextSize(35);
				return v;
			}

			public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) {
				TextView v = (TextView) super.getView(position, convertView, parent);
				v.setTypeface(tfavv);
				v.setTextColor(Color.BLACK);
				// v.setTextSize(35);
				return v;
			}
		};
		sp_adapter.setDropDownViewResource(R.layout.spinner_selector);
		sp_adapter123.setDropDownViewResource(R.layout.spinner_selector);

		sp.setAdapter(sp_adapter);
		sp123.setAdapter(sp_adapter123);
		mbutton1 = (TextView) findViewById(R.id.button1);

		mbutton1.setTypeface(fontface, Typeface.BOLD);
		String tamilt = TamilUtil.convertToTamil(TamilUtil.BAMINI, (getResources().getString(R.string.Text3)));
		mbutton1.setText(tamilt);
		mbutton1.setOnClickListener(this);

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

		int titleId = getResources().getIdentifier("action_bar_title", "id",
				"android");
		TextView yourTextView = (TextView) findViewById(titleId);

		if (yourTextView != null) {
           yourTextView.setText("10 பொருத்தம்"); // Tamil text
		   //yourTextView.setTypeface(fontface, Typeface.BOLD);
			// Optionally, configure toolbar settings
			if (getSupportActionBar() != null) {
				getSupportActionBar().setTitle(yourTextView.getText().toString());
			}
        }
		
	}

	@SuppressWarnings("deprecation")
	private boolean validateLogin(String star, Context baseContext) {
		DB = new DBHelper(getBaseContext());
		SQLiteDatabase db = DB.getReadableDatabase();
		String[] columns = { "_id" };
		String selection = "girlstar=?";
		String[] selectionArgs = { star };
		Cursor cursor = null;
		try {
			cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection, selectionArgs, null, null, null);
			startManagingCursor(cursor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	@SuppressWarnings("deprecation")
	private boolean validateLogin123(String star123, Context baseContext) {
		DB = new DBHelper(getBaseContext());
		SQLiteDatabase db = DB.getReadableDatabase();
		String[] columns = { "_id" };
		String selection = "girlstar=?";
		String[] selectionArgs = { star123 };
		Cursor cursor = null;
		try {
			cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection, selectionArgs, null, null, null);
			startManagingCursor(cursor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.activity_main, menu);
		MenuItem item = menu.findItem(R.id.cfont);
		item.setVisible(false);
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.button1) {
				final String star = (String) spinner1.getSelectedItem();
				final String star123 = (String) spinner123.getSelectedItem();
				girltstar = star123;
				boytstar = star;

				// Toast.makeText(getApplicationContext(), star, Toast.LENGTH_SHORT).show();

				if ((star.equals("") || star == null || star.equals("--Nju;e;njL--"))
						&& (star123.equals("") || star123 == null || star123.equals("--Nju;e;njL--"))) {
					Toast.makeText(getApplicationContext(), "Please enter both star", Toast.LENGTH_SHORT).show();
				} else if (star.equals("--Nju;e;njL--")) {
					Toast.makeText(getApplicationContext(), "Please enter Boy star", Toast.LENGTH_SHORT).show();
				} else if (star123.equals("--Nju;e;njL--")) {
					Toast.makeText(getApplicationContext(), "Please enter Girl star", Toast.LENGTH_SHORT).show();
				} else {
					if (star.equals("mD\\k;")) {
						star1 = "Anusham";
					} else if (star.equals("m];tpdp")) {
						star1 = "Aswini";
					} else if (star.equals("mtpl;lk;")) {
						star1 = "Avittam";
					} else if (star.equals("Mapy;ak;")) {
						star1 = "Ayilayam";
					} else if (star.equals("guzp")) {
						star1 = "Barani";
					} else if (star.equals("rpj;jpiu")) {
						star1 = "Chithirai";
					} else if (star.equals("m];jk;")) {
						star1 = "Hastham";
					} else if (star.equals("fhu;j;jp")) {
						star1 = "Karthi";
					} else if (star.equals("Nfl;il")) {
						star1 = "Kettai";
					} else if (star.equals("kpUfrPuplk;")) {
						star1 = "Mirga";
					}

					else if (star.equals("%yk;")) {
						star1 = "Moolam";
					}

					else if (star.equals("kfk;")) {
						star1 = "Makam";
					} else if (star.equals("G+uhlk;")) {
						star1 = "Pooradam";
					} else if (star.equals("G+uk;")) {
						star1 = "Pooram";
					} else if (star.equals("G+ul;lhjp")) {
						star1 = "Poorat";
					} else if (star.equals("G+rk;")) {
						star1 = "Poosam";
					} else if (star.equals("Gdu;G+rk;")) {
						star1 = "Punarap";
					} else if (star.equals("Nutjp")) {
						star1 = "Reva";
					} else if (star.equals("Nuhfpzp")) {
						star1 = "Rohi";
					} else if (star.equals("rjak;")) {
						star1 = "Sathayam";
					} else if (star.equals("Rthjp")) {
						star1 = "Swathi";
					} else if (star.equals("jpUthjpiu")) {
						star1 = "Thiruv";
					} else if (star.equals("jpUNthzk;")) {
						star1 = "Thiruvonam";
					} else if (star.equals("cj;jpuk;")) {
						star1 = "Uthiram";
					} else if (star.equals("cj;jpuhlk;")) {
						star1 = "Uthiradam";
					} else if (star.equals("cj;jpul;lhjp")) {
						star1 = "Uthiratathi";
					} else if (star.equals("tprhfk;")) {
						star1 = "Visakam";
					}

					if (star123.equals("mD\\k;")) {
						star1123 = "Anusham";
					} else if (star123.equals("m];tpdp")) {
						star1123 = "Aswini";
					} else if (star123.equals("mtpl;lk;-kfu")) {
						star1123 = "Avittam-Makar";
					} else if (star123.equals("mtpl;lk;-Fk;g")) {
						star1123 = "Avittam-Kumba";
					} else if (star123.equals("Mapy;ak;")) {
						star1123 = "Ayilayam";
					} else if (star123.equals("guzp")) {
						star1123 = "Barani";
					} else if (star123.equals("rpj;jpiu-fd;dp")) {
						star1123 = "Chithirai-Kani";
					} else if (star123.equals("rpj;jpiu-Jyh")) {
						star1123 = "Chithirai-Thul";
					} else if (star123.equals("m];jk;")) {
						star1123 = "Hastham";
					} else if (star123.equals("fhu;j;jp-Nk\\")) {
						star1123 = "Karthi-Mes";
					} else if (star123.equals("fhu;j;jp-up\\g")) {
						star1123 = "Karthi-Ris";
					} else if (star123.equals("Nfl;il")) {
						star1123 = "Kettai";
					} else if (star123.equals("kpUfrPupl-up\\g")) {
						star1123 = "Mirga-Rish";
					} else if (star123.equals("kpUfrPupl-kpJd")) {
						star1123 = "Mirga-Midh";
					} else if (star123.equals("%yk;")) {
						star1123 = "Moolam";
					} else if (star123.equals("kfk;")) {
						star1123 = "Makam";
					} else if (star123.equals("G+uhlk;")) {
						star1123 = "Pooradam";
					} else if (star123.equals("G+uk;")) {
						star1123 = "Pooram";
					} else if (star123.equals("G+ul;lhjp-Fk;g")) {
						star1123 = "Parat-Kum";
					} else if (star123.equals("G+ul;lhjp-kpd")) {
						star1123 = "Purat-Min";
					} else if (star123.equals("G+rk;")) {
						star1123 = "Poosam";
					} else if (star123.equals("Gdu;G+-kpJd")) {
						star1123 = "Puna-Midh";
					} else if (star123.equals("Gdu;G+-flf")) {
						star1123 = "Puna-Kada";
					} else if (star123.equals("Nutjp")) {
						star1123 = "Revathi";
					} else if (star123.equals("Nuhfpzp")) {
						star1123 = "Rohini";
					} else if (star123.equals("rjak;")) {
						star1123 = "Sathayam";
					} else if (star123.equals("Rthjp")) {
						star1123 = "Swathi";
					} else if (star123.equals("jpUthjpiu")) {
						star1123 = "Thirvadhir";
					} else if (star123.equals("jpUNthzk;")) {
						star1123 = "Thiruvonam";
					} else if (star123.equals("cj;jpuk;-rpk;kk;")) {
						star1123 = "Uthira-Sim";
					} else if (star123.equals("cj;jpuk;-fd;dp")) {
						star1123 = "Uthira-Kan";
					} else if (star123.equals("cj;jpuhl-jDR")) {
						star1123 = "Utra-Danus";
					} else if (star123.equals("cj;jpuhl-kfu")) {
						star1123 = "Utra-Makar";
					} else if (star123.equals("cj;jpul;lhjp")) {
						star1123 = "Uthrattath";
					} else if (star123.equals("tprhf-Jyh")) {
						star1123 = "Visak-Thul";
					} else if (star123.equals("tprhf-tpUr;rp")) {
						star1123 = "Visak-Viru";
					}

					boolean validLogin = validateLogin(star, getBaseContext());
					boolean validLogin123 = validateLogin123(star123, getBaseContext());

					if (validLogin && validLogin123) {

						   
							Intent in123 = new Intent(getBaseContext(), tamilsample.class);
							in123.putExtra("Star123", star123);
							in123.putExtra("Star", star);
							in123.putExtra("tamilgStar", girltstar);
							in123.putExtra("tamilbStar", boytstar);
							startActivity(in123);
						 
					}
				}

		}
	}

	public boolean onOptionsItemSelected(MenuItem item) {

			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) {
				Intent icon_calc = new Intent(this, tamilporuthamcalculator.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, tamilfindnakshatra.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.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 int_home4 = new Intent(MainActivityTamil.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 alert1 = builder.create();
				alert1.setTitle("Close App");
				alert1.setIcon(R.drawable.alert);
				alert1.show();
				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 {
				return super.onOptionsItemSelected(item);
			}


	}

}