/**
 * Copyright (C) 2010 Orbeon, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation; either version
 * 2.1 of the License, or (at your option) any later version.
 *
 * 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 Lesser General Public License for more details.
 *
 * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xforms.processor.handlers.xhtml;

import org.orbeon.oxf.xml.XMLReceiver;
import org.orbeon.oxf.xforms.control.XFormsControl;
import org.orbeon.oxf.xforms.control.controls.XFormsTriggerControl;
import org.orbeon.oxf.xml.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Minimal (AKA "link") appearance.
 */
public class XFormsTriggerMinimalHandler extends XFormsTriggerHandler {

    protected static final String ENCLOSING_ELEMENT_NAME = "a";

    protected void handleControlStart(String uri, String localname, String qName, Attributes attributes, String effectiveId, XFormsControl control) throws SAXException {

        final XFormsTriggerControl triggerControl = (XFormsTriggerControl) control;
        final XMLReceiver xmlReceiver = handlerContext.getController().getOutput();

        final AttributesImpl containerAttributes = getEmptyNestedControlAttributesMaybeWithId(uri, localname, attributes, effectiveId, triggerControl, true);

        // TODO: needs f:url-norewrite="true"?
        containerAttributes.addAttribute("", "href", "href", XMLReceiverHelper.CDATA, "#");

        // xhtml:a
        final String xhtmlPrefix = handlerContext.findXHTMLPrefix();
        final String aQName = XMLUtils.buildQName(xhtmlPrefix, ENCLOSING_ELEMENT_NAME);
        xmlReceiver.startElement(XMLConstants.XHTML_NAMESPACE_URI, ENCLOSING_ELEMENT_NAME, aQName, containerAttributes);
        {
            final String labelValue = getTriggerLabel(triggerControl);
            final boolean mustOutputHTMLFragment = triggerControl != null && triggerControl.isHTMLLabel();
            outputLabelText(xmlReceiver, triggerControl, labelValue, xhtmlPrefix, mustOutputHTMLFragment);
        }
        xmlReceiver.endElement(XMLConstants.XHTML_NAMESPACE_URI, ENCLOSING_ELEMENT_NAME, aQName);
    }

    @Override
    protected void addCustomClasses(StringBuilder classes, XFormsControl control) {
        // Ask super first
        super.addCustomClasses(classes, control);

        if (control != null) {
            final XFormsTriggerControl triggerControl = (XFormsTriggerControl) control;
            if (triggerControl.isReadonly()) {
                // Add a special class to facilitate styling of readonly links with IE 6
                classes.append(" xforms-trigger-readonly");
            }
        }
    }

    //    private void hrefExperiment() {
//        // TODO: Complete experimenting with outputting href value
//        String hrefValue = "#";
//        {
//            // Try to compute an href value right away if we detect just a nested xf:load
//            // TODO: Need to tell the client not to handle clicks on hyperlink
//            // TODO: This should probably be done at the control level
//            // TODO: This duplicates code in XFormsLoadAction
//            if (triggerControl != null && triggerControl.getControlElement() != null) {
//                final Element controlElement = triggerControl.getControlElement();
//                final Element loadElement = controlElement.element(XFormsConstants.XFORMS_LOAD_QNAME);
//                if (loadElement != null && XFormsEvents.XFORMS_DOM_ACTIVATE.equals(loadElement.attributeValue(XFormsConstants.XML_EVENTS_EVENT_ATTRIBUTE_QNAME))) {
//                    final String resource = loadElement.attributeValue(XFormsConstants.RESOURCE_QNAME);
//                    if (resource != null && resource.indexOf('{') == -1) {
//                        // Static resource URL
//                        hrefValue = resource;
//                    } else if (resource != null) {
//                        // Computed resource URL
//                        final BindingContext currentBindingContext = triggerControl.getBindingContext();
//                        if (currentBindingContext != null && currentBindingContext.getSingleNode() != null) {
//                            final Map prefixToURIMap = triggerControl.getNamespaceMappings();
//                            final XFormsContextStack contextStack = triggerControl.getContextStack();
//                            hrefValue = XFormsUtils.resolveAttributeValueTemplates(pipelineContext,
//                                    currentBindingContext.getNodeset(), currentBindingContext.getPosition(),
//                                    contextStack.getCurrentVariables(), XFormsContainingDocument.getFunctionLibrary(),
//                                    contextStack.getFunctionContext(), prefixToURIMap, triggerControl.getLocationData(), resource);
//                        }
//                    } else {
//                        // Assume single-node binding
//                        final BindingContext curBindingContext = triggerControl.getBindingContext();
//                        if (curBindingContext != null && curBindingContext.getSingleNode() != null) {
//                            hrefValue = XFormsInstance.getValueForNodeInfo(curBindingContext.getSingleNode());
//                        }
//                    }
//                }
//            }
//        }
//
//        newAttributes.addAttribute("", "href", "href", ContentHandlerHelper.CDATA, hrefValue);
//    }
}

@@ -3045,2835 +3045,7 @@
    %7D
-%0A%0A    //    private void hrefExperiment() %7B%0A//        // TODO: Complete experimenting with outputting href value%0A//        String hrefValue = %22#%22;%0A//        %7B%0A//            // Try to compute an href value right away if we detect just a nested xf:load%0A//            // TODO: Need to tell the client not to handle clicks on hyperlink%0A//            // TODO: This should probably be done at the control level%0A//            // TODO: This duplicates code in XFormsLoadAction%0A//            if (triggerControl != null && triggerControl.getControlElement() != null) %7B%0A//                final Element controlElement = triggerControl.getControlElement();%0A//                final Element loadElement = controlElement.element(XFormsConstants.XFORMS_LOAD_QNAME);%0A//                if (loadElement != null && XFormsEvents.XFORMS_DOM_ACTIVATE.equals(loadElement.attributeValue(XFormsConstants.XML_EVENTS_EVENT_ATTRIBUTE_QNAME))) %7B%0A//                    final String resource = loadElement.attributeValue(XFormsConstants.RESOURCE_QNAME);%0A//                    if (resource != null && resource.indexOf('%7B') == -1) %7B%0A//                        // Static resource URL%0A//                        hrefValue = resource;%0A//                    %7D else if (resource != null) %7B%0A//                        // Computed resource URL%0A//                        final BindingContext currentBindingContext = triggerControl.getBindingContext();%0A//                        if (currentBindingContext != null && currentBindingContext.getSingleNode() != null) %7B%0A//                            final Map prefixToURIMap = triggerControl.getNamespaceMappings();%0A//                            final XFormsContextStack contextStack = triggerControl.getContextStack();%0A//                            hrefValue = XFormsUtils.resolveAttributeValueTemplates(pipelineContext,%0A//                                    currentBindingContext.getNodeset(), currentBindingContext.getPosition(),%0A//                                    contextStack.getCurrentVariables(), XFormsContainingDocument.getFunctionLibrary(),%0A//                                    contextStack.getFunctionContext(), prefixToURIMap, triggerControl.getLocationData(), resource);%0A//                        %7D%0A//                    %7D else %7B%0A//                        // Assume single-node binding%0A//                        final BindingContext curBindingContext = triggerControl.getBindingContext();%0A//                        if (curBindingContext != null && curBindingContext.getSingleNode() != null) %7B%0A//                            hrefValue = XFormsInstance.getValueForNodeInfo(curBindingContext.getSingleNode());%0A//                        %7D%0A//                    %7D%0A//                %7D%0A//            %7D%0A//        %7D%0A//%0A//        newAttributes.addAttribute(%22%22, %22href%22, %22href%22, ContentHandlerHelper.CDATA, hrefValue);%0A//    %7D
 %0A%7D%0A

Remove obsolete comment