//NewComplaint.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.*;

public class NewComplaint extends HttpServlet
{
	// define a few things we'll need
	private HttpSession session = null;
	private Connection conn = null;
	private Statement stmt = null;
	Configure cfg = new Configure();

	public void init (ServletConfig config)
		throws ServletException
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection (cfg.dbPath, cfg.dbUsername, cfg.dbPassword);
			stmt = conn.createStatement();
		}

		catch (SQLException sqe)
		{
			sqe.printStackTrace();
		}

		catch (ClassNotFoundException ce)
		{
			System.out.println(ce);
		}
		System.out.println("Init Successful");
	}
	
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		String username = req.getParameter ("username");
		String password = req.getParameter ("password");
		String select = req.getParameter ("select");
		
		res.setContentType ("text/html");

		try
		{

			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");

			String temp="";
			while(rs.next())
			temp=rs.getString(1);	
			if (password.equals (temp))
			{
				if (select == null)
				{
				out.println("<html><head><title>Register New Complaint "+username+
					"</title></head><body bgcolor=\"#ffffff\">"+
					"<h1>Register New Complaint</h1>"+
					"<form action=\"" + cfg.ServletDir + "NewComplaint\" method=\"GET\">" +
					"<table border=0>"+
					"<tr><td>Description:</td><td>" +
					"<textarea cols=50 rows=4 name=\"description\"></textarea></td></tr>"+ 
					"<input type=\"hidden\" name=\"select\" value=\"select\">"+
					"<input type=\"hidden\" name=\"username\" value=\""+username+"\">"+
					"<input type=\"hidden\" name=\"password\" value=\""+password+"\">"+
					"<tr><td>Location:</td><td><input type=\"TEXT\" name=\"location\"></td></tr>"+
					"<tr><td>Type</td><td><select name=\"type\">");
					rs = stmt.executeQuery ("SELECT distinct type_id,description from TYPE");
					
					while (rs.next())
					{
						out.println("<option value=\""+rs.getString(1)+"\">" + rs.getString(2));
					}
					out.println("</select></td></tr>"+
					"<tr><td colspan=2><input type=\"submit\" value=\"Register Complaint\"></td></tr>"+
					"</table></form></body></html>");
				}
				else
				{
				String description = req.getParameter ("description");
				String location = req.getParameter ("location");
				String type = req.getParameter ("type");
				stmt = conn.createStatement();
				rs = stmt.executeQuery ("SELECT max(complaint_id) from COMPLAINT group by complaint_id");
				int cid = 0;
				while (rs.next())
					cid = Integer.parseInt(rs.getString(1));
				stmt = conn.createStatement();
				cid++;
				//Get date
				Date d = new Date(System.currentTimeMillis());
				String cur=d.toString();
				//Update database with the complaint
				stmt.executeQuery ("insert into COMPLAINT values (" + cid  +",'','" 
				+ description + "',''," + "TO_DATE('"+d+"','YYYY-MM-DD')" + ",'','','','','" + location + "')");
				
				//Enter into the complaint type relation
				stmt.executeQuery ("insert into REGISTER values ('" + username + "'," + cid + ")");
				
				//Show message to user
				out.println("<html><head><title>Complaint registered</title></head>"+
					"<body bgcolor=#ffffff><h2>Your Complaint has been registered. Your complaint id is "+
					cid +".</h2>"+
				"<form action=\"" + cfg.ServletDir + "Complaintee\" method=\"POST\">"+
				"<input type=\"hidden\" name=\"username\" value=\""+username+"\">"+
				"<input type=\"hidden\" name=\"password\" value=\""+password+"\">"+
				"<input type=\"submit\" value=\"Return to main page\"></form></body></html>");
				
				}
			}
			else
			{
				//Authentication failure
				out.println("Authentication Failure");
			}
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}

	// Frankly i dont know what this does. but what the heck
	public void doDelete (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
	
	}
	
	// This updates database with complaint
	public void doPost (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		doGet (req, res);
	}
}
