package com.intellier;

/**
 * @author james
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import com.primix.tapestry.*;
import java.sql.*;

public class Home extends BasePage {

	private String username = null;
	private String password = null;
	private String message = " ";


	/**
	 * @see AbstractPage#beginResponse(IResponseWriter, IRequestCycle)
	 */
	public void beginResponse(IResponseWriter arg0, IRequestCycle cycle)
		throws RequestCycleException {
		AdminVisit visit = (AdminVisit) getVisit();
		if (!visit.isBeenHere()) {
			visit.setBeenHere(true);
			boolean check = false;
			Connection conn = null;
			Statement stmt = null;
			String ip = cycle.getRequestContext().getRequest().getRemoteAddr();
			try {
				conn = visit.getConnection();
				stmt = conn.createStatement();
				String query = "insert into admin_ips values ('" + ip + "',1)";
				stmt.executeUpdate(query);
			} catch (Exception e) {
				
				check = true;
			}
			if (check) {
				try {
					String query =
						"update admin_ips set hits = hits + 1 where ip = '"
							+ ip
							+ "'";
					stmt.executeUpdate(query);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		super.beginResponse(arg0, cycle);
	}




	/**
	 * @see AbstractPage#detach()
	 */
	public void detach() {
		username = null;
		password = null;
		message = " ";
		super.detach();
	}

	public void formSubmit(IRequestCycle cycle) {

		AdminVisit visit = (AdminVisit) getVisit();
		try {
			Connection conn = visit.getConnection();
			Statement stmt = conn.createStatement();
			String query =
				"select * from violations where ip = '"
					+ cycle.getRequestContext().getRequest().getRemoteAddr()
					+ "' and admin = 'C'";
					
			ResultSet rs = stmt.executeQuery(query);
			if (rs.next()) {
				username = null;
				password = null;
				message =
					"Sorry your IP has been blocked...(blocked IP's last 3 days)<BR>For assistance please email <a href='mailto:jsherwood@romulin.com'>help@intellier.com</a>";
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("ERROR in client login(HOME)");
		}
		if (visit.getLoginAttempts() > 2) {

			message =
				"Your IP, "
					+ cycle.getRequestContext().getRequest().getRemoteAddr()
					+ " has been LOGGED, access has now been blocked...";
			username = null;
			password = null;
			return;

		} else {
			try {
				Connection conn = visit.getConnection();
				Statement stmt = conn.createStatement();
				String query = "select * from client where username = '";
				query += username + "' and password = '" + password + "'";
				ResultSet rs = stmt.executeQuery(query);
				if (rs.next()) {
					username = null;
					password = null;
					message = " ";
					visit.setLoginAttempts(0);
					visit.setClientID(rs.getInt("clientid"));
					visit.setLoggedOn(true);
					cycle.setPage("Campaigns");
				} else {
					username = null;
					password = null;
					visit.setLoggedOn(false);
					visit.setLoginAttempts(visit.getLoginAttempts() + 1);
					if (visit.getLoginAttempts() < 3) {
						message =
							"Invalid username or password, attempt " + visit.getLoginAttempts() + " FAILED";
					} else {
						message =
							"Your IP, "
								+ cycle
									.getRequestContext()
									.getRequest()
									.getRemoteAddr()
								+ " has been LOGGED, access is now blocked...";
						query =
							"insert into violations (IP,admin) values('"
								+ cycle
									.getRequestContext()
									.getRequest()
									.getRemoteAddr()
								+ "','C')";
								
						stmt.executeUpdate(query);
						query =
							"select * from violation_records where ip = '"
								+ cycle
									.getRequestContext()
									.getRequest()
									.getRemoteAddr()
								+ "' and site = 'C'";
						rs = stmt.executeQuery(query);
						if (rs.next()) {
							int amount = rs.getInt("amount") + 1;
							query =
								"update violation_records set amount = "
									+ amount
									+ " where recordid = "
									+ rs.getString("recordid");
							stmt.executeUpdate(query);
						} else {
							query =
								"insert into violation_records values ('"
									+ cycle
										.getRequestContext()
										.getRequest()
										.getRemoteAddr()
									+ "','C',1)";
							stmt.executeUpdate(query);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("ERROR in login page!!");
			}

		}
	}

	/**
	 * Gets the password.
	 * @return Returns a String
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Gets the username.
	 * @return Returns a String
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Sets the password.
	 * @param password The password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Sets the username.
	 * @param username The username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * Gets the message.
	 * @return Returns a String
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * Sets the message.
	 * @param message The message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

}