import java.util.Date;

public class Loginout {
    //public static final String brn = "<br />\n"; // No longer used

    // Establish abbreviations for calling methods in other modules:
    private static Lab3Props out = new Lab3Props();
    /* Sbove for output to terminal or browser or other logging/display device
       only by test/debug rig. Output to regular user is queued in: */
    private StringBuffer details = new StringBuffer();
    /* After login returns, call getDetails() to see what was put
       there and delete that text so that it won't silt up with old text.
       Note: This is an instance variable! */

    public String getDetails() {
	String text = details.toString();
	details.setLength(0);
	return text;
    }

    /* Check if password is valid, and user not already logged in.
       If everything valid, perform login by computing login-expire time
        (convert parameter seconds to milliseconds, add to current time,
	set that as expire), add record to ActiveUsers, return true.
       If anything wrong, put explanation in instance variable details
        (accessible later by getDetails()), keep db unchnanged, return false.
       Note: This is an instance method, so that the instance variable
        details can be set differently for different callers. */
    public boolean login(String username, String password,
			 String sessionID, int seconds) {
	int userID = Users.findName(username);
	if (userID<=0) {
	    details.append("404 No such user, login rejected.\n");
	    return false;
	}
	boolean chkres = Users.check(username, password);
	if (!chkres) {
	    details.append("401 Wrong password, login rejected.\n");
	    return false;
	}
	int nlog = ActiveUsers.count(userID, -1, null);
	if (nlog>0) {
	    details.append("409 Already logged in, additional login " +
			   "rejected.\n");
	    return false;
	}
	Date d1 = new Date();
	long l1 = d1.getTime(); // Now
	//details.append("now=" + l1); // In the future
	long expire = l1+((long)seconds)*((long)1000);
	//out.printbr("exp=" + expire); // In the future
	ActiveUsers.makeActive(userID, expire, sessionID);
	return true;
    }

    /* Note: Static method because it doesn't need to update details. */
    public static void logout(String username) {
	logout(username, false); }
    public static void logout(String username, boolean trace) {
	int userID = Users.findName(username);
	logout(userID, trace); }
    public static void logout(int userID) {
	logout(userID, false); }
    public static void logout(int userID, boolean trace) {
	ActiveUsers.makeInactive(userID, trace);
    }


    public static void main(String args[]) {
	out.printbr("** Starting test rig for Loginout...");
	out.print("Session ID:");
	String sessionID = MyInput.readString();
	int nsec = 5*60; // 5 minutes
	while (true) {
	    out.print("Login:");
	    String userID = MyInput.readString();
	    out.print("Password (warning, it'll show during this test):");
	    String password = MyInput.readString();
	    Loginout l = new Loginout();
	    boolean suc = l.login(userID, password, sessionID, nsec);
	    if (suc) break;
	    out.printbr("** INVALID: " + l.getDetails());
	    Lab3Props.sleep(5000);
	}
	out.printbr("Congratulations, you are now logged in.");
    }

    /*
  logout(String userID, String sessionID)
    Verify userID matches sessionID in ActiveUsers.
    If so, remove that record, else throw unchecked exception.
    */
}
