/*
 * Copyright (C) 2017 LibreCCM Foundation.
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package org.librecms.pagemodel.contentitems;

import org.librecms.contentsection.ContentItem;
import org.librecms.contenttypes.MultiPartArticle;
import org.librecms.contenttypes.MultiPartArticleSection;
import org.librecms.pagemodel.assets.AssetRenderers;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;

/**
 * Renderer for {@link MultiPartArticle} items.
 *
 * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
 */
@ContentItemRenderer(renders = MultiPartArticle.class)
public class MultiPartArticleRenderer extends AbstractContentItemRenderer {

    private static final long serialVersionUID = -4298383182795585868L;

    @Inject
    private AssetRenderers assetRenderers;
    
//    @Inject
//    public MultiPartArticleRenderer(final AssetRenderers assetRenderers) {
//        super(assetRenderers);
//    }

    /**
     * Renders the provided {@link MultiPartArticle}. The following values are
     * put into {@code result}:
     *
     * <pre>
     *  {
     *      "summary": {@link MultiPartArticle#getSummary()}
     *      "sections": {@link MultiPartArticle#getSections()}
     *  }
     * </pre>
     *
     * The value of {@code sections} is a list containing a {@link Map}
     * generated by
     * {@link #renderSection(org.librecms.contenttypes.MultiPartArticleSection, java.util.Locale)}
     * for each section.
     *
     * @param item     The item to render.
     * @param language The current language.
     * @param result   The map into which the result is placed.
     */
    @Override
    public void renderItem(final ContentItem item,
                           final Locale language,
                           final Map<String, Object> result) {

        final MultiPartArticle article;
        if (item instanceof MultiPartArticle) {
            article = (MultiPartArticle) item;
        } else {
            return;
        }

        result.put("summary", article.getSummary().getValue(language));
        result.put("sections",
                   article
                       .getSections()
                       .stream()
                       .map(section -> renderSection(section, language))
                       .collect(Collectors.toList()));

    }

    /**
     * Renders a {@link MultiPartArticleSection}. The generated map contains the
     * following values:
     *
     * <pre>
     *  {
     *      "sectionId": {@link MultiPartArticleSection#getSectionId()}
     *      "title": {@link MultiPartArticleSection#getTitle()}
     *      "rank": {@link MultiPartArticleSection#getRank()}
     *      "pageBreak": {@link MultiPartArticleSection#isPageBreak()}
     *      "text": {@link MultiPartArticleSection#getText()}
     *  }
     * </pre>
     *
     * @param section  The section to render.
     * @param language The current language.
     *
     * @return A map with the data of the section.
     */
    protected Map<String, Object> renderSection(
        final MultiPartArticleSection section, final Locale language) {

        final Map<String, Object> result = new HashMap<>();

        result.put("sectionId", section.getSectionId());
        result.put("title", section.getTitle().getValue(language));
        result.put("rank", section.getRank());
        result.put("pageBreak", section.isPageBreak());
        result.put("text", section.getText().getValue(language));

        return result;
    }
    
     @Override
    protected AssetRenderers getAssetRenderers() {
        return assetRenderers;
    }


}
