package server.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import shared.model.User;

public class UserManager extends DatabaseManager
{
	public UserManager()
	{
		
	}
	
	/*--API Functions--*/
	
	/**
	 * Checks whether a user is in the database.
	 * 
	 * @param conn
	 * 		Connection to use
	 * @param username
	 * 		username of user
	 * @param password
	 * 		password of user
	 * @return unique User matching username and password, User with all null values if 
	 * 		credentials invalid, or null if some error
	 */
	public User validateUser(Connection conn, String userName, String password)
	{
		User output = null;
		
		try
		{
			PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE " + 
															"userName = ? AND password = ?");
			
			stmt.setString(1, userName);
			stmt.setString(2, password);
			
			ResultSet results = stmt.executeQuery();
			if(results.next()) // If a match, return it.
			{
				output = new User(results.getInt("userID"),
									results.getString("userName"),
									results.getString("password"), 
									results.getString("firstName"), 
									results.getString("lastName"), 
									results.getString("eMail"), 
									results.getInt("recordsIndexed"), 
									results.getInt("batch"));
				
				if(results.next()) // If multiple matches, error.
				{
					output = null;
				}
			}
			else // If no matches, return User with null values.
			{
				output = User.INVALID_USER;
			}
			
			results.close();
			stmt.close();
		}
		catch(SQLException e)
		{
			output = null;
			System.out.println("WARNING: Failed API operation 1.");
		}
		
		return output;
	}
	
	/*--Import-related Functions--*/
	
	/**
	 * Adds a user to the database.
	 * 
	 * @param conn
	 * 		Connection to use
	 * @param toAdd
	 * 		User to add to database
	 * @return user ID > 0 if succeeded, int <= 0 otherwise
	 */
	public int addUser(Connection conn, User toAdd)
	{
		boolean check = true;
		int output = -1;
		
		try
		{
			PreparedStatement stmt = conn.prepareStatement("INSERT INTO users " + 
					"(userName, password, firstName, lastName, eMail, recordsIndexed, batch) " + 
					"VALUES (?,?,?,?,?,?,?)");
			
			stmt.setString(1, toAdd.getUserName());
			stmt.setString(2, toAdd.getPassword());
			stmt.setString(3, toAdd.getFirstName());
			stmt.setString(4, toAdd.getLastName());
			stmt.setString(5, toAdd.getEMail());
			stmt.setInt(6, toAdd.getRecordsIndexed());
			stmt.setInt(7, toAdd.getBatch());
			
			if(stmt.executeUpdate() > 0)
			{
				output = getLastID(conn);
			}
			stmt.close();
		}
		catch(SQLException e)
		{
			check = false;
			System.out.println("WARNING: Failed database edit 1.");
		}
		
		if(!check)
		{
			output = -1;
		}
		
		return output;
	}
	
	/*--Test-related functions--*/

	/**
	 * Gets a user from the database. This is almost the same function as
	 * validateUser(), but does not require uniqueness.
	 * 
	 * @param conn
	 * 		Connection to use
	 * @param userName
	 * 		username of user
	 * @param password
	 * 		password of user
	 * @return User matching username and password, or null if no match
	 */
	public User getUser(Connection conn, String userName, String password)
	{
		User output = null;
		
		try
		{
			PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE " + 
															"userName = ? AND password = ?");
			
			stmt.setString(1, userName);
			stmt.setString(2, password);
			
			ResultSet results = stmt.executeQuery();
			if(results.next())
			{
				output = new User(results.getInt("userID"),
									results.getString("userName"),
									results.getString("password"), 
									results.getString("firstName"), 
									results.getString("lastName"), 
									results.getString("eMail"), 
									results.getInt("recordsIndexed"), 
									results.getInt("batch"));
			}
			
			results.close();
			stmt.close();
		}
		catch(SQLException e)
		{
			System.out.println("WARNING: Failed database retrieval 1.");
		}
		
		return output;
	}
}
