/**
 * 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.artifactbrowser;

import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.apache.cocoon.util.HashUtil;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.excalibur.source.SourceValidity;
import org.apache.excalibur.source.impl.validity.NOPValidity;
import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
import org.dspace.app.xmlui.utils.UIException;
import org.dspace.app.xmlui.wing.element.*;
import org.dspace.app.xmlui.wing.Message;
import org.dspace.app.xmlui.wing.WingException;
import org.dspace.content.DSpaceObject;
import org.dspace.content.DCValue;
import org.dspace.content.Item;
import org.dspace.authorize.AuthorizeException;
import org.xml.sax.SAXException;
import org.dspace.app.xmlui.utils.HandleUtil;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;

/**
 * Display to the user a simple form letting the user give feedback.
 * 
 * @author Scott Phillips
 */
public class ILLForm extends AbstractDSpaceTransformer implements CacheableProcessingComponent
{
    /**
     * Language Strings
     */
    private static final Message T_title =
            message("xmlui.ArtifactBrowser.ILLForm.title");

    private static final Message T_dspace_home =
            message("xmlui.general.dspace_home");

    private static final Message T_trail =
            message("xmlui.ArtifactBrowser.ILLForm.trail");

    private static final Message T_head =
            message("xmlui.ArtifactBrowser.ILLForm.head");

    private static final Message T_para1 =
            message("xmlui.ArtifactBrowser.ILLForm.para1");

    private static final Message T_email =
            message("xmlui.ArtifactBrowser.ILLForm.email");

    private static final Message T_email_help =
            message("xmlui.ArtifactBrowser.ILLForm.email_help");

    private static final Message T_message =
            message("xmlui.ArtifactBrowser.ILLForm.message");

    private static final Message T_submit =
            message("xmlui.ArtifactBrowser.ILLForm.submit");

    private static final Message T_copyright_label =
            message("xmlui.ArtifactBrowser.ILLForm.copyright_label");

    private static final Message T_copyright =
            message("xmlui.ArtifactBrowser.ILLForm.copyright");

    private static final Message T_copyright_agree =
            message("xmlui.ArtifactBrowser.ILLForm.copyright_checkbox");

    /**
     * Generate the unique caching key.
     * This key must be unique inside the space of this component.
     */
    public Serializable getKey() {
        
        String email = parameters.getParameter("email","");
        String message = parameters.getParameter("message","");
        String page = parameters.getParameter("page", "unknown");

        return HashUtil.hash(email + "-" + message + "-" + page);
    }

    /**
     * Generate the cache validity object.
     */
    public SourceValidity getValidity() 
    {
        return NOPValidity.SHARED_INSTANCE;
    }
    
    
    public void addPageMeta(PageMeta pageMeta) throws SAXException,
            WingException, SQLException, IOException,
            AuthorizeException
    {       
        pageMeta.addMetadata("title").addContent(T_title);
 
        pageMeta.addTrailLink(contextPath + "/",T_dspace_home);
        pageMeta.addTrail().addContent(T_trail);
    }

    public void addBody(Body body) throws SAXException, WingException,
            UIException, SQLException, IOException, AuthorizeException
    {

        // Build the item viewer division.
        Division ill = body.addInteractiveDivision("ill-form",
                contextPath+"/ill",Division.METHOD_POST,"primary");
        
        ill.setHead(T_head);
        
        ill.addPara(T_para1);

        List form = ill.addList("form",List.TYPE_FORM);
        Text email = form.addItem().addText("email");
        email.setAutofocus("autofocus");
        email.setLabel(T_email);
        email.setHelp(T_email_help);
        email.setValue(parameters.getParameter("email",""));
        TextArea message = form.addItem().addTextArea("message");
        message.setLabel(T_message);
        message.setValue(parameters.getParameter("message",""));

        Division copyright = body.addDivision("copyright");
        copyright.setHead(T_copyright_label);
        Para copyright_notice = copyright.addPara();
        copyright_notice.addContent(T_copyright);

        CheckBox check = copyright.addPara().addCheckBox("accept");
        check.addOption("accept", T_copyright_agree);
        form.addItem().addButton("submit").setValue(T_submit);
        ill.addHidden("page").setValue(parameters.getParameter("page","unknown"));
    }
}