package A10Park;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.lang.Math;
import java.util.*;
//import java.util.Random;
import java.util.Date;
//import sun.net.smtp.SmtpClient;

import javax.servlet.*;
import javax.servlet.http.*;

public class A10_VerifyUser extends HttpServlet {

    private Properties connectionProperties = new Properties();
    private Driver     driver               = null;
    private String     driverName           = null;
    private String     jdbcURL              = null;
  		

    public void init(ServletConfig cfg) throws ServletException {
        super.init(cfg);
        {
        	String user, pw;
	      ServletContext ctx = getServletContext();
         driverName = ctx.getInitParameter("gb.DBdriver");
         jdbcURL = ctx.getInitParameter("gb.DBconn_string");
         user = ctx.getInitParameter("gb.DBusername");
         if( user != null ) {
         	connectionProperties.put("user", user);
         }
         pw = ctx.getInitParameter("gb.DBpassword");
         if( pw != null ) {
         	connectionProperties.put("password", pw);
         }
         try {
         	driver = (Driver)Class.forName(driverName).newInstance();
        	}
         catch( Exception e ) {
         	throw new ServletException("Unable to load driver: " + e.getMessage());
         }
       }
   }


      public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
		  response.setContentType("text/html");
    	  PrintWriter out = response.getWriter();
        ResultSet results;
        Statement stmt;
		  Connection conn = null;

        try {
 	     		String errorMessage = null;
         	conn = DriverManager.getConnection(jdbcURL, connectionProperties);
         	stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

				//if(request.getParameter("username")!=null)
				//{
					String username = request.getParameter("username");
					String verifyID = request.getParameter("verifyID");

         		results = stmt.executeQuery("SELECT * FROM calendar WHERE username='" + username + "'");
			
					results.last();
					int numberOfRows = results.getRow();
					results.beforeFirst(); 
					if(numberOfRows == 1)
					{
	   				Date now = new Date();
            
                  float nowTime = now.getTime();
                  nowTime = nowTime/3600000;
                  //createTime = createTime/60;
                  //createTime = createTime/60;
                  nowTime = Math.round(nowTime);
                  out.println("Current time: " + nowTime);		
                  results.next();
                  float creationTime = results.getFloat("createTime");
                  
                  if( (nowTime-creationTime) <= 24 )
                  {
							stmt.executeUpdate("UPDATE calendar SET verified='verified' WHERE username='" + username + "'");
							stmt.close();                  	
                  	out.println("Account has been verified");
                  }
                  

					}
					else
					{
						out.println("Couldn't find that username");
					}

	 			

					
				                 
     	}
      catch( SQLException e ) {
      	out.println("A database error occurred: " + e.getMessage());
      }
      finally {
      	if( conn != null ) {
         	try { conn.close(); }
            catch( SQLException e ) { }	
				out.println("</body></html>");
         }
     }
        
  }
}
