package com.composum.sling.cpnl;

import com.composum.sling.clientlibs.handle.Clientlib;
import com.composum.sling.clientlibs.handle.ClientlibElement;
import com.composum.sling.clientlibs.handle.ClientlibRef;
import com.composum.sling.clientlibs.processor.RendererContext;
import com.composum.sling.clientlibs.service.ClientlibService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;

/**
 * A tag build references to styles and script files. Renders either links to individual files,
 * client libraries containing several embedded files or client library categories containing embedded files
 * for a category of client libraries.
 */
public class ClientlibTag extends CpnlBodyTagSupport {

    private static final Logger LOG = LoggerFactory.getLogger(ClientlibTag.class);

    public static final String ALREADY_EMBEDDED = "clientlib.alreadyEmbedded";

    protected Clientlib.Type type;
    protected String path;
    protected String category;

    public void setType(String type) {
        this.type = Clientlib.Type.valueOf(type);
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    @Override
    protected void clear() {
        super.clear();
        type = null;
        path = null;
    }

    protected Clientlib.Type getType() {
        if (type == null) {
            String ext = StringUtils.substringAfterLast(path, ".").toLowerCase();
            try {
                ext = ext.replaceAll("(png|jpg)", "img");
                type = Clientlib.Type.valueOf(ext);
            } catch (Exception ex) {
                type = Clientlib.Type.link;
            }
        }
        return type;
    }

    @Override
    public int doEndTag() throws JspException {
        try {
            RendererContext rendererContext = RendererContext.instance(context, request);

            Clientlib.Type type = getType();
            ClientlibRef ref = null;

            if (StringUtils.isNotBlank(path)) {
                ref = new ClientlibRef(type, path, false, null);
            } else if (StringUtils.isNotBlank(category)) {
                ref = ClientlibRef.forCategory(type, category, false, null);
            } else {
                LOG.error("No path nor category attribute was given!");
            }

            ClientlibService service = context.getService(ClientlibService.class);
            ClientlibElement clientlib = service.resolve(ref, request.getResourceResolver());

            if (null != clientlib) { // if this is a clientlib or category
                JspWriter writer = this.pageContext.getOut();
                service.renderClientlibLinks(clientlib, writer, rendererContext);
            } else {
                LOG.error("No clientlib found for path {} / category {} ");
            }
        } catch (IOException | RepositoryException e) {
            LOG.error(e.getMessage(), e);
        }
        super.doEndTag();
        return EVAL_PAGE;
    }
}

@@ -2278,32 +2278,102 @@
 , false, null);%0A
+                LOG.debug(%22%3Ccpn:clientlib.%7B%7D path=%7B%7D/%3E%22, type, path);%0A
             %7D el
@@ -2484,32 +2484,110 @@
 , false, null);%0A
+                LOG.debug(%22%3Ccpn:clientlib.%7B%7D category=%7B%7D/%3E%22, type, category);%0A
             %7D el

add debug logging to the clientlib tag