/*
 *	EasyScout Boy Scouts of America tracking software.
 *	http://www.easyscout.org/
 *
 *	Copyright (C) 2000 Conan C. Albrecht
 *	conan@warp.cx
 *
 *	This program is released under the GNU license so tech-savvy
 *	Boy Scouts and others can enhance the program for the use
 *	of all.  Please e-mail changes to my address above so I can
 *	reintegrate into the main program at http://www.easyscout.org/.
 *	This way, all users mutually benefit one another in the spirit of
 *	scouting.
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version 2
 *	of the License, or (at your option) any later version.
 *
 * 	Any and all changes to the program code must be documented with
 *  date, author name and e-mail, and a short description.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package cx.warp.scout;

import cx.warp.scout.lookup.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/** Static class for displaying Leader information */
public class LeaderUI {

	/** Returns a leader given the leader id, or null if no leader exists with the given id */
	public static Leader getLeader(HttpServletRequest request, Session session) throws DataException {
		if (session != null) {
			Troop troop = session.getTroop();
			if (troop != null) {
				return troop.getLeader(request.getParameter("leaderid"));
			}//if
		}//if
		return null;
	}//getleader

	/** Shows the create new leader screen */
	public static void createScreen(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Leader leader = getLeader(request, session);		

		// get parameters from the leader, if they exist
		String id = "new";
		String fname = "";
		String mname = "";
		String lname = "";
		Date birthdate = new Date();
		String address = "";
		String phone = "";
		String email = "";
		String notes = "";
		String position = "";
		if (leader != null) {
			Utils.printSection(out, "LEADER: " + leader.getName());
			fname = leader.getFirstName();
			mname = leader.getMiddleName();
			lname = leader.getLastName();
			birthdate = leader.getBirthDate();
			address = leader.getAddress();
			phone = leader.getPhone();
			email = leader.getEmail();
			notes = leader.getNotes();
			id = leader.getID();
			position = leader.getPosition();
		}else{
			Utils.printSection(out, "CREATE NEW LEADER");
		}//if
	
		out.println("<form action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=action value=createleader>");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=leaderid value=" + id + ">");
		out.println("<table border=0>");
		out.println("<tr>");
		out.println("<td>" + Utils.font() + "First Name:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=fname size=30 value=\"" + fname + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Middle Name:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=mname size=30 value=\"" + mname + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Last Name:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=lname size=30 value=\"" + lname + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Position:</td>");
		out.println("<td>" + Utils.font() + "<select name=position>");
		boolean regPosition = false;
		Vector vec = XMLLookup.getAdultLeadership();
		if (vec != null) {
			for (Enumeration enum = vec.elements(); enum.hasMoreElements(); ) {
				String s = (String)enum.nextElement();
				out.print("<option");
				if (s.equals(position) || (s.equals("Other") && !regPosition)) {
					out.print(" selected");
					if (!s.equals("Other")) {
					  regPosition = true;
					}//if
				}//if
				out.println(">" + s + "</option>");
			}//for
		}//if
		if (regPosition) {
			out.println("</select>&nbsp;If Other: <input type=text size=20 name=other></td>");
		}else{
			out.println("</select>&nbsp;If Other: <input type=text size=20 name=other value=\"" + position + "\"></td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Birthdate:</td>");
		out.println("<td>" + Utils.font() + "");
		Utils.printDateField(out, "", birthdate);
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Address:</td>");
		out.println("<td>" + Utils.font() + "<textarea name=address rows=3 cols=30>" + address + "</textarea></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Phone Number:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=phone size=30 value=\"" + phone + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "E-Mail:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=email size=30 value=\"" + email  + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Notes:</td>");
		out.println("<td>" + Utils.font() + "<textarea name=notes rows=5 cols=30 wrap=virtual>" + notes + "</textarea></td>");
		if (session.isWrite()) {
			out.println("</tr><tr>");
			out.println("<td>" + Utils.font() + "&nbsp;</td>");
			out.println("<td>" + Utils.font() + "<input type=submit value=Submit></td>");
		}//if
		out.println("</tr>");
		out.println("</table>");
		out.println("</form>");
		
	}//createScreen
	
	/** Creates/Updates a leader */
	public static void createLeader(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Troop troop = session.getTroop();
		
		// find or create the new leader
		Leader leader = getLeader(request, session);
		if (leader == null) { // i.e. create a new one
			leader = troop.createLeader();
		}//if
		
		// update the leader's information
		leader.setFirstName(request.getParameter("fname"));
		leader.setMiddleName(request.getParameter("mname"));
		leader.setLastName(request.getParameter("lname"));
		leader.setBirthDate(Utils.parseDateField(request, ""));
		leader.setAddress(request.getParameter("address"));
		leader.setPhone(request.getParameter("phone"));
		leader.setEmail(request.getParameter("email"));
		String position = request.getParameter("position");
		if (position != null && position.equalsIgnoreCase("Other")) {
			leader.setPosition(request.getParameter("other"));
		}else{
			leader.setPosition(position);
		}//if
		leader.setNotes(request.getParameter("notes"));
	}//createleader
	
	/** Deletes a leader */
	public static void deleteLeader(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Troop troop = session.getTroop();
		
		// find or create the new leader
		String id = request.getParameter("leaderid");
		if (id == null || !troop.deleteLeader(id)) { 
			Utils.error(out, "Could not delete leader from DB.");
		}//if
	}//deleteLeader		
	
	/** The main leader detail screen */
	public static void leaderDetail(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Leader leader = getLeader(request, session);		
		if (leader == null) {
			Utils.error(out, "Could not locate specified leader.");
			TroopUI.leaders(request, out, session);
		}else{
			createScreen(request, out, session);
			if (session.isWrite()) {
		  	out.println("<p>");
				printDeleteLink(request, out, session, "Delete This Leader", leader.getID());
			}//if
		}//if
	}//leaderDetail
	
	/** Prints the delete leader link */
	public static void printDeleteLink(HttpServletRequest request, PrintWriter out, Session session, String text, String leaderid) throws IOException, DataException {
			out.println("<a href=\"javascript:confirmURL('Delete this leader from the list?','" + 
			  Utils.servletdir() + 
			  "?z=" + session.getZ() +
				"&leaderid=" + leaderid +
				"&action=leaderdelete" +
				"')\">" + text + "</a>");
	}//printDeleteLink

}//LeaderUI
