package findyourfate.com.porutham;


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

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class sample extends ListActivity {

	ImageView image;
	protected TextView str;
	protected TextView str123;
	protected SQLiteDatabase DB;
	protected Cursor cursor;
	protected ListAdapter adapter;

	@SuppressLint("NewApi")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String userAgent = new WebView(sample.this).getSettings().getUserAgentString();
		if (userAgent.contains("Mobile")) {
			setContentView(R.layout.sample);
		} else {
			setContentView(R.layout.tabsample);
		}

		FirebaseDatabase database = FirebaseDatabase.getInstance();
		DatabaseReference ref = database.getReference("Description");
		ref.setValue("result-bothstar");

		DB = (new DBHelper(this)).getWritableDatabase();
		str = (TextView) findViewById(R.id.textView1);
		Bundle bundle = getIntent().getExtras();
		String Starname = bundle.getString("Star");
		str.setText(Starname);
		str123 = (TextView) findViewById(R.id.textView2);
		Bundle bundle123 = getIntent().getExtras();
		String Starname123 = bundle123.getString("Star123");
		str123.setText(Starname123);

		// Toast.makeText(sample.this,
		// ""+str.getText().toString()+str123.getText().toString(),
		// Toast.LENGTH_LONG).show();
		if (userAgent.contains("Mobile")) {
			cursor = DB.rawQuery(
					"SELECT _id, Girlstar, Dhinam,Ganam,Makendiram, Deerkam,Yoni,Rasi,Athipathi,Vasiyam,Rajju,Naadi,Score,RMK,Special FROM poo1 WHERE Boystar Like ? AND Girlstar Like ?",
					new String[] { "%" + str.getText().toString() + "%", "%" + str123.getText().toString() + "%" });
			adapter = new SimpleCursorAdapter(
					this,
					R.layout.engtable,
					cursor,
					new String[] { "Girlstar", "Dhinam", "Ganam", "Makendiram", "Deerkam", "Yoni", "Rasi", "Athipathi",
							"Vasiyam", "Rajju", "Naadi", "Score", "RMK", "Special" },
					new int[] { R.id.dbgirlstar, R.id.dbdhinam, R.id.dbganam, R.id.dbmakendiram, R.id.dbdeerkam,
							R.id.dbyoni, R.id.dbrasi, R.id.dbathipathu, R.id.dbvasiyam, R.id.dbrajju, R.id.dbnaadi,
							R.id.dbscore, R.id.dbrmk, R.id.dbspecial });
			setListAdapter(adapter);
		} else {
			cursor = DB.rawQuery(
					"SELECT _id, Girlstar, Dhinam,Ganam,Makendiram, Deerkam,Yoni,Rasi,Athipathi,Vasiyam,Rajju,Naadi,Score,RMK,Special FROM poo1 WHERE Boystar Like ? AND Girlstar Like ?",
					new String[] { "%" + str.getText().toString() + "%", "%" + str123.getText().toString() + "%" });
			adapter = new SimpleCursorAdapter(
					this,
					R.layout.engtabtable,
					cursor,
					new String[] { "Girlstar", "Dhinam", "Ganam", "Makendiram", "Deerkam", "Yoni", "Rasi", "Athipathi",
							"Vasiyam", "Rajju", "Naadi", "Score", "RMK", "Special" },
					new int[] { R.id.dbgirlstar, R.id.dbdhinam, R.id.dbganam, R.id.dbmakendiram, R.id.dbdeerkam,
							R.id.dbyoni, R.id.dbrasi, R.id.dbathipathu, R.id.dbvasiyam, R.id.dbrajju, R.id.dbnaadi,
							R.id.dbscore, R.id.dbrmk, R.id.dbspecial });
			setListAdapter(adapter);
		}
		ActionBar ab = getActionBar();
		if (ab != null) {
			ab.setDisplayHomeAsUpEnabled(true);
			ab.setHomeButtonEnabled(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;
	}

	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, 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.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 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);
		}
	}


}