package edu.fiu.models;

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Basic type for authentication and authorization
 *
 * @author Dan Herrera
 */
public class User {

  protected Integer id;   // User ID in data store
  protected String name;  // Should be provided from some file

  private String hash;    // Result of hashing name + password

  private Integer active;

  private String host;

  public static class Mapper implements RowMapper<User> {
    @Override public User map(final ResultSet rs, final StatementContext ctx) throws SQLException {
      return new User(rs.getInt("id"), rs.getString("name"), rs.getString("hash"), rs.getInt("active"), rs.getString("host"));
    }
  }

  public User(Integer id, String name, String hash, Integer active, String host) {
    this.id = id;
    this.name = name;
    this.hash = hash;
    this.active = active;
    this.host = host; 
  }

  /**
   * Returns whether or not this user's creds validate with the given hash
   * @param hash SHA-512 hash result of concatenating the user's name and
   *        password (i.e. sha512('jsmith'+'hunter2'))
   * @return true if the given hash matches the user hash, false otherwise
   */
  public boolean auth(String password) {
    //TODO: Hash password before comparing
    //hashing ain't implemented..!
    return this.hash.equals(password);
  }

  /**
   * Returns whether or not this user's cred's validate against the presumed
   * copy; this is a convenience function and is otherwise the same as .equals
   * @param user User instance that should have matching credentials
   * @return true if the given user equals the current user, false otherwise
   */
  public boolean auth(User user) {
    return this.equals(user);
  }

  public String toString() {
    return String.format("User<%d,%s,%s,%d,%s>", id, name, hash, active, host);
  }

  public boolean equals(Object o) {
    User other = (User) o;
    return this.id.equals(other.id) &&
      this.name.equals(other.name) &&
      this.hash.equals(other.hash);
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getHash() {
    return hash;
  }

  public void setHash(String hash) {
    this.hash = hash;
  }

 
  public Integer getActive() {
    return active;
  }

  
  public void setActive(Integer active){
    this.active = active; 
  } 

  
  public String getHost(){
    return host;
  }

  public void setHost(String host){
    this.host = host; 
  }
  /**
  **/
}
