/**
 * Copyright (c) 2018-2024 Linagora
 * 
 * This program/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 program/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 program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.binding.rest.utils;

import java.util.Optional;

import javax.xml.XMLConstants;
import javax.xml.namespace.QName;

import org.ow2.petals.binding.rest.RESTConstants.CommonServicesParameter;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.w3c.dom.Element;

import com.ebmwebsourcing.easycommons.json.Json2XmlConverter;
import com.ebmwebsourcing.easycommons.xml.QNameHelper;

import de.odysseus.staxon.json.JsonXMLConfig;

public class JsonXMLConfigBuilder {

    private JsonXMLConfigBuilder() {
        // Utility class --> No constructor
    }

    public static JsonXMLConfig build(final Element parentElt) throws PEtALSCDKException {

        final String virtRootStr = DOMUtils.getSubElementTextContent(parentElt,
                CommonServicesParameter.HTTP_BODY_TO_JSON_VIRTUAL_ROOT, false);
        final QName virtRoot;
        if (virtRootStr != null) {
            // we don't want to use the namespace of the jbi document!
            virtRoot = QNameHelper.fromString(parentElt, virtRootStr, XMLConstants.NULL_NS_URI);
        } else {
            virtRoot = null;
        }

        final String multiplePiStr = DOMUtils.getSubElementTextContent(parentElt,
                CommonServicesParameter.HTTP_BODY_TO_JSON_MULTIPLE_PI, false);
        final Optional<Boolean> multiplePi;
        if (multiplePiStr != null) {
            multiplePi = Optional.of(Boolean.parseBoolean(multiplePiStr));
        } else {
            multiplePi = Optional.empty();
        }

        final String autoArrayStr = DOMUtils.getSubElementTextContent(parentElt,
                CommonServicesParameter.HTTP_BODY_TO_JSON_AUTO_ARRAY, false);
        final Optional<Boolean> autoArray;
        if (autoArrayStr != null) {
            autoArray = Optional.of(Boolean.parseBoolean(autoArrayStr));
        } else {
            autoArray = Optional.empty();
        }

        final String autoPrimitiveStr = DOMUtils.getSubElementTextContent(parentElt,
                CommonServicesParameter.HTTP_BODY_TO_JSON_AUTO_PRIMITIVE, false);
        final Optional<Boolean> autoPrimitive;
        if (autoPrimitiveStr != null) {
            autoPrimitive = Optional.of(Boolean.parseBoolean(autoPrimitiveStr));
        } else {
            autoPrimitive = Optional.empty();
        }

        final String prettyPrintStr = DOMUtils.getSubElementTextContent(parentElt,
                CommonServicesParameter.HTTP_BODY_TO_JSON_PRETTY_PRINT, false);
        final Optional<Boolean> prettyPrint;
        if (prettyPrintStr != null) {
            prettyPrint = Optional.of(Boolean.parseBoolean(prettyPrintStr));
        } else {
            prettyPrint = Optional.empty();
        }

        final String nsDeclStr = DOMUtils.getSubElementTextContent(parentElt,
                CommonServicesParameter.HTTP_BODY_TO_JSON_NS_DECL, false);
        final Optional<Boolean> nsDecl;
        if (nsDeclStr != null) {
            nsDecl = Optional.of(Boolean.parseBoolean(nsDeclStr));
        } else {
            nsDecl = Optional.empty();
        }

        return Json2XmlConverter.buildConfiguration(virtRoot, multiplePi, autoArray, autoPrimitive, prettyPrint,
                nsDecl);
    }

}
