package edu.gatech.nutrack;


import edu.gatech.nutrack.database.UserDataSource;
import edu.gatech.nutrack.model.Authenticator;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends FragmentActivity implements
		ActionBar.OnNavigationListener {

	/**
	 * The serialization (saved instance state) Bundle key representing the
	 * current dropdown position.
	 */
	private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
	private static final String TAG = "***LOGIN";	
	private static final int DURATION = Toast.LENGTH_LONG;
	private TextView un, pwd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// Set up the action bar to show a dropdown list.
		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		// Set up the dropdown list navigation in the action bar.
		actionBar.setListNavigationCallbacks(
		// Specify a SpinnerAdapter to populate the dropdown list.
				new ArrayAdapter<String>(getActionBarThemedContextCompat(),
						android.R.layout.simple_list_item_1,
						android.R.id.text1, new String[] {
								getString(R.string.title_section1),
								getString(R.string.title_section2),
								getString(R.string.title_section3), }), this);
		
		un = (TextView) findViewById(R.id.userNameText);
		pwd = (TextView) findViewById(R.id.passwordText);
		
		un.setText("");
		pwd.setText("");
	}

	/**
	 * Backward-compatible version of {@link ActionBar#getThemedContext()} that
	 * simply returns the {@link android.app.Activity} if
	 * <code>getThemedContext</code> is unavailable.
	 */
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	private Context getActionBarThemedContextCompat() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			return getActionBar().getThemedContext();
		} else {
			return this;
		}
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		// Restore the previously serialized current dropdown position.
		if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
			getActionBar().setSelectedNavigationItem(
					savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		// Serialize the current dropdown position.
		outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
				.getSelectedNavigationIndex());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onNavigationItemSelected(int position, long id) {
		// When the given dropdown item is selected, show its contents in the
		// container view.
		Fragment fragment = new DummySectionFragment();
		Bundle args = new Bundle();
		args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
		fragment.setArguments(args);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, fragment).commit();
		return true;
	}

	/**
	 * A dummy fragment representing a section of the app, but that simply
	 * displays dummy text.
	 */
	public static class DummySectionFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		public static final String ARG_SECTION_NUMBER = "section_number";

		public DummySectionFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_login_dummy,
					container, false);
			TextView dummyTextView = (TextView) rootView
					.findViewById(R.id.section_label);
			dummyTextView.setText(Integer.toString(getArguments().getInt(
					ARG_SECTION_NUMBER)));
			return rootView;
		}
	}
	
	public void callLogin(View view) {
		Authenticator a = new Authenticator(this);
		boolean success = a.isUserValid(un.getText().toString(), pwd.getText().toString());
		if(success) {
			Intent callLoginIntent = new Intent(this, Home.class);
			Log.d(TAG, "login success");
			Toast.makeText(this, "Login success.", DURATION).show();
			startActivity(callLoginIntent);
		} else {
			Log.d(TAG, "login failed");
			Toast.makeText(this, "Login failed.", DURATION).show();
		}
	}
	
	public void callSignUp(View view) {
		Log.d(TAG, "going to signup activity");
		Intent callSignUpIntent = new Intent(this, SignUp.class);
		startActivity(callSignUpIntent);
	}

}

@@ -121,16 +121,54 @@
 icator;%0A
+import edu.gatech.nutrack.model.User;%0A
 import a
@@ -4695,16 +4695,18 @@
 his);%0A%09%09
+//
 boolean 
@@ -4787,32 +4787,311 @@
 ;%0A%09%09
-if(success) %7B%0A%09%09%09Intent 
+User u = a.getLoginUser(un.getText().toString(), pwd.getText().toString());%0A%09%09%0A%09%09if(u != null) %7B%0A%09%09%09Intent callLoginIntent = null;%0A%09%09%09if(u.getType() == 0) %7B%0A%09%09%09%09Log.d(TAG, %22login physician%22);%0A%09%09%09%09callLoginIntent = new Intent(this, HomePhysician.class); %0A%09%09%09%7D else %7B%0A%09%09%09%09Log.d(TAG, %22login patient%22);%0A%09%09%09%09
 call
@@ -5134,16 +5134,25 @@
 class);%0A
+%09%09%09%7D%0A%09%09%09%0A
 %09%09%09Log.d
@@ -5554,14 +5554,13 @@
 ntent);%0A
+
 %09%7D%0A
-%0A
 %7D%0A

check by user object to determine user type