/*
 * 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.macro.dashboard;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.GroupBlock;
import org.xwiki.rendering.block.MacroBlock;
import org.xwiki.rendering.block.RawBlock;
import org.xwiki.rendering.block.WordBlock;
import org.xwiki.rendering.macro.dashboard.Gadget;
import org.xwiki.rendering.renderer.BlockRenderer;
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
import org.xwiki.rendering.renderer.printer.WikiPrinter;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.transformation.RenderingContext;

/**
 * Editable gadget renderer, that renders the gadget as in view mode, but with additional metadata that allows editing
 * the gadget parameters.
 * 
 * @version $Id$
 * @since 3.0rc1
 */
@Component
@Named("edit")
@Singleton
public class EditableGadgetRenderer extends DefaultGadgetRenderer
{
    /**
     * The identifier of the metadata block for this dashboard (class parameter of the generated XDOM container that
     * holds the rest of the metadata).
     */
    protected static final String METADATA = "metadata";

    /**
     * The macro content renderer, to render the macro as annotated XHTML to be editable.
     */
    @Inject
    @Named("annotatedxhtml/1.0")
    protected BlockRenderer defaultGadgetContentRenderer;
    
    protected ComponentManager componentManager;
    
    @Inject
    protected RenderingContext renderingContext;
    
    @Inject
    protected Logger logger;

    /** 
     * @return the renderer corresponding to the current target syntax
     */
    protected BlockRenderer getGadgetContentRenderer()
    {
        // Get the current syntax
        Syntax currentTargetSyntax = renderingContext.getTargetSyntax();
        if (currentTargetSyntax == null) {
            // (it should never happen actually)
            return defaultGadgetContentRenderer;
        }

        // Get the annotated syntax corresponding to the current target syntax
        String annotatedTargetSyntax = currentTargetSyntax.toIdString();
        if (!StringUtils.startsWith(annotatedTargetSyntax, "annotated")) {
            annotatedTargetSyntax = "annotated" + annotatedTargetSyntax;
        }
        
        try {
            return componentManager.getInstance(BlockRenderer.class, annotatedTargetSyntax);
        } catch (ComponentLookupException e) {
            logger.warn("Failed to load the syntax [{}].", annotatedTargetSyntax);
            // Failback to the default renderer
            return defaultGadgetContentRenderer;
        }
    }

    /**
     * @param gadget the gadget to decorate
     * @return the block containing the metadata that will allow clients to edit this gadget
     */
    protected Block getGadgetEditMetadata(Gadget gadget)
    {
        GroupBlock metadataBlock = new GroupBlock();
        metadataBlock.setParameter(CLASS, METADATA);

        // look at the content of the gadget and store whether it's a macro or not
        boolean isMacro = gadget.getContent().size() == 1 && gadget.getContent().get(0) instanceof MacroBlock;
        GroupBlock isMacroBlock = new GroupBlock();
        isMacroBlock.setParameter(CLASS, "isMacro");
        isMacroBlock.addChild(new WordBlock(Boolean.toString(isMacro)));

        metadataBlock.addChild(isMacroBlock);

        if (isMacro) {
            // Get the renderer corresponding to the current target syntax
            BlockRenderer gadgetContentRenderer = getGadgetContentRenderer();

            // render the annotated macro call in the page, to be able to edit it. Only the macro call comments will be
            // rendered, since transformations are not ran, so there is no content in the macro. But annotation is
            // enough.
            GroupBlock renderedContentBlock = new GroupBlock();
            renderedContentBlock.setParameter(CLASS, "content");
            WikiPrinter printer = new DefaultWikiPrinter();
            gadgetContentRenderer.render(gadget.getContent(), printer);
            RawBlock rawBlock = new RawBlock(printer.toString(), renderingContext.getTargetSyntax());
            renderedContentBlock.addChild(rawBlock);

            // render the title in the page as well, to be edited as source
            GroupBlock gadgetTitleBlock = new GroupBlock();
            gadgetTitleBlock.setParameter(CLASS, "title");
            // even if it's not a word, it's fine since it will be rendered in one piece
            gadgetTitleBlock.addChild(new WordBlock(gadget.getTitleSource()));

            metadataBlock.addChild(renderedContentBlock);
            metadataBlock.addChild(gadgetTitleBlock);
        }

        return metadataBlock;
    }

    @Override
    public List<Block> decorateGadget(Gadget gadget)
    {
        List<Block> viewBlock = super.decorateGadget(gadget);

        if (viewBlock.size() > 0) {
            viewBlock.get(0).addChild(getGadgetEditMetadata(gadget));
        }

        return viewBlock;
    }
}

@@ -2354,16 +2354,94 @@
 tadata%22;
+%0A    %0A    private static final String ANNOTATED_SYNTAXES_PREFIX = %22annotated%22;
 %0A%0A    /*
@@ -2644,24 +2644,36 @@
 derer;%0A    %0A
+    @Inject%0A
     protecte
@@ -3441,27 +3441,41 @@
 Syntax, 
-%22annotated%22
+ANNOTATED_SYNTAXES_PREFIX
 )) %7B%0A   
@@ -3507,27 +3507,41 @@
 yntax = 
-%22annotated%22
+ANNOTATED_SYNTAXES_PREFIX
  + annot
@@ -5383,95 +5383,354 @@
     
-RawBlock
+// If we use the fallback (defaultGadgetContentRenderer), we set the
  raw
-B
+ b
 lock 
-= new RawBlock(printer.toString(), renderingContext.getTargetSyntax()
+syntax to XHTML 1.0.%0A            Syntax rawBlockSyntax = gadgetContentRenderer == defaultGadgetContentRenderer%0A                    ? Syntax.XHTML_1_0 : renderingContext.getTargetSyntax();%0A            RawBlock rawBlock = new RawBlock(printer.toString(), rawBlockSyntax
 );%0A 

Introduce a configuration to select the HTML version to use according to the current skin.