/**
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */
package org.dspace.app.xmlui.aspect.administrative.eperson;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;

import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
import org.dspace.app.xmlui.wing.Message;
import org.dspace.app.xmlui.wing.WingException;
import org.dspace.app.xmlui.wing.element.Body;
import org.dspace.app.xmlui.wing.element.CheckBox;
import org.dspace.app.xmlui.wing.element.Division;
import org.dspace.app.xmlui.wing.element.Item;
import org.dspace.app.xmlui.wing.element.List;
import org.dspace.app.xmlui.wing.element.PageMeta;
import org.dspace.app.xmlui.wing.element.Para;
import org.dspace.app.xmlui.wing.element.Text;
import org.dspace.authorize.AuthorizeException;

/**
 * Present the user with all the eperson metadata fields so that they
 * can describe the new eperson before being created. If the user's 
 * input is incorrect in someway then they may be returning here with 
 * some fields in error. In particular there is a special case for the 
 * condition when the email-adress entered is allready in use by 
 * another user.
 * 
 * @author Alexey Maslov
 */
public class AddEPersonForm extends AbstractDSpaceTransformer   
{
	/** Language Strings */
	private static final Message T_dspace_home =
        message("xmlui.general.dspace_home");
	
	private static final Message T_eperson_trail =
		message("xmlui.administrative.eperson.general.epeople_trail");
	
	private static final Message T_title =
		message("xmlui.administrative.eperson.AddEPersonForm.title");

	private static final Message T_trail =
		message("xmlui.administrative.eperson.AddEPersonForm.trail");

	private static final Message T_head1 =
		message("xmlui.administrative.eperson.AddEPersonForm.head1");

	private static final Message T_email_taken =
		message("xmlui.administrative.eperson.AddEPersonForm.email_taken");

	private static final Message T_head2 =
		message("xmlui.administrative.eperson.AddEPersonForm.head2");

	private static final Message T_error_email_unique =
		message("xmlui.administrative.eperson.AddEPersonForm.error_email_unique");

	private static final Message T_error_email =
		message("xmlui.administrative.eperson.AddEPersonForm.error_email");

	private static final Message T_error_fname =
		message("xmlui.administrative.eperson.AddEPersonForm.error_fname");

	private static final Message T_error_lname =
		message("xmlui.administrative.eperson.AddEPersonForm.error_lname");

	private static final Message T_req_certs =
		message("xmlui.administrative.eperson.AddEPersonForm.req_certs");

	private static final Message T_can_log_in =
		message("xmlui.administrative.eperson.AddEPersonForm.can_log_in");

	private static final Message T_submit_create =
		message("xmlui.administrative.eperson.AddEPersonForm.submit_create");

	private static final Message T_submit_cancel =
		message("xmlui.general.cancel");
	
	
	/** Language string used from other aspects: */    
		
	private static final Message T_email_address = 
        message("xmlui.EPerson.EditProfile.email_address");
    
    private static final Message T_first_name = 
        message("xmlui.EPerson.EditProfile.first_name");
    
    private static final Message T_last_name = 
        message("xmlui.EPerson.EditProfile.last_name");
    
    private static final Message T_telephone =
        message("xmlui.EPerson.EditProfile.telephone");
    
    	
	public void addPageMeta(PageMeta pageMeta) throws WingException
    {
        pageMeta.addMetadata("title").addContent(T_title);
        pageMeta.addTrailLink(contextPath + "/", T_dspace_home);
        pageMeta.addTrailLink(contextPath + "/admin/epeople",T_eperson_trail);
        pageMeta.addTrail().addContent(T_trail);
    }
	
	
	public void addBody(Body body) throws WingException, SQLException, AuthorizeException 
	{
		// Get all our parameters
		Request request = ObjectModelHelper.getRequest(objectModel);
		
		String errorString = parameters.getParameter("errors",null);
		ArrayList<String> errors = new ArrayList<String>();
		if (errorString != null)
		{
			for (String error : errorString.split(","))
            {
                errors.add(error);
            }
		}
	
		String emailValue = request.getParameter("email_address");
		String firstValue = request.getParameter("first_name");
		String lastValue  = request.getParameter("last_name");
		String phoneValue = request.getParameter("phone");
		boolean canLogInValue    = (request.getParameter("can_log_in") == null)  ? false : true;
		boolean certificateValue = (request.getParameter("certificate") == null) ? false : true;
	    		 
		
		
		// DIVISION: eperson-add
	    Division add = body.addInteractiveDivision("eperson-add",contextPath+"/admin/epeople",Division.METHOD_POST,"primary administrative eperson");
	    
	    add.setHead(T_head1);
	    
	    if (errors.contains("eperson_email_key")) {
	    	Para problem = add.addPara();
	    	problem.addHighlight("bold").addContent(T_email_taken);
	    }
                
        List identity = add.addList("identity",List.TYPE_FORM);
        identity.setHead(T_head2);       
        
        Text email = identity.addItem().addText("email_address");
        email.setRequired();
        email.setLabel(T_email_address);
        email.setValue(emailValue);
        if (errors.contains("eperson_email_key")) {
        	email.addError(T_error_email_unique);
        }
        else if (errors.contains("email_address")) {
        	email.addError(T_error_email);
        }
        
        Text firstName = identity.addItem().addText("first_name");
        firstName.setRequired();
        firstName.setLabel(T_first_name);
        firstName.setValue(firstValue);
        if (errors.contains("first_name")) {
        	firstName.addError(T_error_fname);
        }
        
        Text lastName = identity.addItem().addText("last_name");
        lastName.setRequired();
        lastName.setLabel(T_last_name);
        lastName.setValue(lastValue);
        if (errors.contains("last_name")) {
        	lastName.addError(T_error_lname);
        }
        
        Text phone = identity.addItem().addText("phone");
        phone.setLabel(T_telephone);
        phone.setValue(phoneValue);
        
        CheckBox canLogIn = identity.addItem().addCheckBox("can_log_in");
        canLogIn.setLabel(T_can_log_in);
        canLogIn.addOption(canLogInValue, "yes");
        
        CheckBox certificate = identity.addItem().addCheckBox("certificate");
        certificate.setLabel(T_req_certs);
        certificate.addOption(certificateValue,"yes");
        
        Item buttons = identity.addItem();
        buttons.addButton("submit_save").setValue(T_submit_create);
        buttons.addButton("submit_cancel").setValue(T_submit_cancel);
        
        add.addHidden("administrative-continue").setValue(knot.getId());
	}
	
}
