/**********************************************************
  This servlet is used to verify a user's account.  It takes a username, email,
  and confirmation number from the user, and determines whether or not that 
  information consitutes a verified account.  
  There are three options:
      1. A valid account is entered with a valid confirmation number.
      2. A non-valid account (username/email combination) is entered.
      3. A valid account is entered but with a non-valid confirmation number.
      4. A valid account is entered, but the time limit for verification has expired.

  If the verification is successful, the user is forwarded to the login page.  Otherwise
  the user is sent back to the verification page with an appropriate error message.
 
  @author Anthony Furst
  @version CISC474 A10
*/
  
package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class Verify extends HttpServlet {

    /** Performs all the functionality for this servlet
	@param request A servlet request object.
	@param response A servlet response object.
    */
    public void doPost(HttpServletRequest request,
		       HttpServletResponse response)
	throws IOException, ServletException {
	String username;
	String password;
	String url;

	username = getServletContext().getInitParameter("user");
	password = getServletContext().getInitParameter("passwd");
	url = getServletContext().getInitParameter("url");

	Verifier verifier = new Verifier();

	verifier.makeConnection(username, password, url);

	String name = request.getParameter("username");
	String email = request.getParameter("email");
	String number = request.getParameter("number");
	boolean isValid = false;
	boolean isExpired = false;
	boolean isVerified = false;
	
	isValid = verifier.getIsValid(name, email);

	if (isValid) {
	    isExpired = verifier.getIsExpired(name, email);
	    
	    if (!isExpired) {
		isVerified = verifier.getIsVerified(name, email, number);
	
		//Valid, verified Account
		if (isVerified) {
		    verifier.closer();

		    RequestDispatcher view = request.getRequestDispatcher("login.html");
		    view.forward(request, response);
		}
		//Valid, non-expired, non-valid Account
		else {
		    verifier.closer();
		    
		    request.setAttribute("reason", "notVerified");
		    RequestDispatcher view = request.getRequestDispatcher("verificationError.jsp");
		    view.forward(request, response);
		}
	    }
	    //Valid, expired Account
	    else {
		verifier.closer();

		request.setAttribute("reason", "expired");
		RequestDispatcher view = request.getRequestDispatcher("verificationError.jsp");
		view.forward(request, response);
	    }
	}
	//Invalid Account
	else {
	    verifier.closer();

	    request.setAttribute("reason", "invalid");
	    RequestDispatcher view = request.getRequestDispatcher("verificationError.jsp");
	    view.forward(request, response);
	}	
    }	
}


	