/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This 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 software 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.rendering.html5;

import java.util.HashMap;
import java.util.Map;

import org.xwiki.rendering.internal.renderer.xhtml.XHTMLChainingRenderer;
import org.xwiki.rendering.internal.renderer.xhtml.image.XHTMLImageRenderer;
import org.xwiki.rendering.internal.renderer.xhtml.link.XHTMLLinkRenderer;
import org.xwiki.rendering.listener.Format;
import org.xwiki.rendering.listener.chaining.ListenerChain;

/**
 * Convert listener events to HTML5.
 *
 * @version $Id$
 * @since 6.4M3
 */
public class HTML5ChainingRenderer extends XHTMLChainingRenderer
{
    private static final String ELEM_STRONG = "strong";

    private static final String ELEM_EM = "em";

    private static final String ELEM_DEL = "del";

    private static final String ELEM_INS = "ins";

    private static final String ELEM_SUP = "sup";

    private static final String ELEM_SUB = "sub";

    private static final String ELEM_SPAN = "span";

    /**
     * @param linkRenderer the object to render link events into XHTML. This is done so that it's pluggable because link
     * rendering depends on how the underlying system wants to handle it. For example for XWiki we check if the document
     * exists, we get the document URL, etc.
     * @param imageRenderer the object to render image events into XHTML. This is done so that it's pluggable because
     * image rendering depends on how the underlying system wants to handle it. For example for XWiki we check if the
     * image exists as a document attachments, we get its URL, etc.
     * @param listenerChain the chain of listener filters used to compute various states
     */
    public HTML5ChainingRenderer(XHTMLLinkRenderer linkRenderer,
            XHTMLImageRenderer imageRenderer,
            ListenerChain listenerChain)
    {
        super(linkRenderer, imageRenderer, listenerChain);
    }

    @Override
    public void beginFormat(Format format, Map<String, String> parameters)
    {
        switch (format) {
            case BOLD:
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_STRONG);
                break;
            case ITALIC:
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_EM);
                break;
            case STRIKEDOUT:
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_DEL);
                break;
            case UNDERLINED:
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_INS);
                break;
            case SUPERSCRIPT:
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_SUP);
                break;
            case SUBSCRIPT:
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_SUB);
                break;
            case MONOSPACE:
                Map<String, String> attributes = new HashMap<>();
                attributes.putAll(parameters);
                attributes.put("class", "monospace");
                getXHTMLWikiPrinter().printXMLStartElement(ELEM_SPAN, attributes);
                break;
            case NONE:
            default:
                break;
        }
        if (!parameters.isEmpty()) {
            getXHTMLWikiPrinter().printXMLStartElement(ELEM_SPAN, parameters);
        }
    }

    @Override
    public void endFormat(Format format, Map<String, String> parameters)
    {
        if (!parameters.isEmpty()) {
            getXHTMLWikiPrinter().printXMLEndElement(ELEM_SPAN);
        }
        switch (format) {
            case BOLD:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_STRONG);
                break;
            case ITALIC:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_EM);
                break;
            case STRIKEDOUT:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_DEL);
                break;
            case UNDERLINED:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_INS);
                break;
            case SUPERSCRIPT:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_SUP);
                break;
            case SUBSCRIPT:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_SUB);
                break;
            case MONOSPACE:
                getXHTMLWikiPrinter().printXMLEndElement(ELEM_SPAN);
                break;
            case NONE:
            default:
                break;
        }
    }
}

@@ -2860,767 +2860,124 @@
     
-switch (format) %7B%0A            case BOLD:%0A                getXHTMLWikiPrinter().printXMLStartElement(ELEM_STRONG);%0A                break;%0A            case ITALIC:%0A                getXHTMLWikiPrinter().printXMLStartElement(ELEM_EM);%0A                break;%0A            case STRIKEDOUT:%0A                getXHTMLWikiPrinter().printXMLStartElement(ELEM_DEL);%0A                break;%0A           
+// Right now, the only difference with the super
  c
+l
 as
-e UNDERLINED:%0A                getXHTMLWikiPrinter().printXMLStartElement(ELEM_INS);%0A                break;%0A            case SUPERSCRIPT:%0A                getXHTMLWikiPrinter().printXMLStartElement(ELEM_SUP);%0A                break;%0A            case SUBSCRIPT:%0A                getXHTMLWikiPrinter().printXMLStartElement(ELEM_SUB);%0A                break;%0A            case 
+s is about the %22monospace%22 format%0A        if (format == Format.
 MONO
@@ -2973,38 +2973,36 @@
 Format.MONOSPACE
-:%0A    
+) %7B%0A
             Map%3C
@@ -3051,36 +3051,32 @@
 ();%0A            
-  
-  
 attributes.putAl
@@ -3086,28 +3086,24 @@
 arameters);%0A
-    
             
@@ -3144,36 +3144,32 @@
 %22);%0A            
- 
-   
 getXHTMLWikiPrin
@@ -3231,201 +3231,89 @@
     
-        break;%0A            case NONE:%0A            default:%0A                break;%0A        %7D%0A        if (!parameters.isEmpty()) %7B%0A            getXHTMLWikiPrinter().printXMLStartElement(ELEM_SPAN
+%7D else %7B%0A            // Call the super class%0A            super.beginFormat(format
 , pa
@@ -3329,24 +3329,25 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A    @
@@ -3558,291 +3558,137 @@
     
-switch (format) %7B%0A            case BOLD:%0A                getXHTMLWikiPrinter().printXMLEndElement(ELEM_STRONG);%0A                break;%0A           
+// Right now, the only difference with the super
  c
+l
 as
-e ITALIC:%0A                getXHTMLWikiPrinter().printXMLEndElement(ELEM_EM);%0A                break;%0A            case STRIKEDOUT:%0A    
+s is about the %22monospace%22 format%0A        if (format == Format.MONOSPACE) %7B%0A
     
@@ -3741,19 +3741,20 @@
 nt(ELEM_
-DEL
+SPAN
 );%0A     
@@ -3760,568 +3760,189 @@
     
-        break;%0A            case UNDERLINED:%0A                getXHTMLWikiPrinter().printXMLEndElement(ELEM_INS);%0A                break;%0A            case SUPERSCRIPT:%0A                getXHTMLWikiPrinter().printXMLEndElement(ELEM_SUP);%0A                break;%0A            case SUBSCRIPT:%0A                getXHTMLWikiPrinter().printXMLEndElement(ELEM_SUB);%0A                break;%0A            case MONOSPACE:%0A                getXHTMLWikiPrinter().printXMLEndElement(ELEM_SPAN);%0A                break;%0A            case NONE:%0A            default:%0A                break
+%7D else %7B%0A            // Call the super class, with an empty parameters map to avoid closing the span element twice%0A            super.endFormat(format, new HashMap%3CString, String%3E())
 ;%0A  

Add a renderer for HTML 5.