package com.bunjlabs.fugaframework.templates;

import com.bunjlabs.fugaframework.FugaApp;
import com.bunjlabs.fugaframework.foundation.Context;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import org.apache.commons.lang3.StringEscapeUtils;

public class TemplateApi {

    private final FugaApp app;
    private final Context ctx;

    public TemplateApi(FugaApp app, Context ctx) {
        this.app = app;
        this.ctx = ctx;

    }

    public String asset(Object... args) {
        String path = "";
        if (args.length > 0 && args[0] != null && args[0] instanceof String) {
            path = (String) args[0];
        }
        if (!app.getConfiguration().getBoolean("fuga.templates.assets.external", false)) {
            return that("assets/" + path);
        } else {
            return app.getConfiguration().get("fuga.templates.assets.path", that()) + path;
        }
    }

    public String that(Object... args) {
        String path = "";
        if (args.length > 0 && args[0] != null && args[0] instanceof String) {
            path = (String) args[0];
        }
        StringBuilder sb = new StringBuilder();
        sb.append("http://")
                .append(ctx.getRequest().getHost())
                .append("/")
                .append(path);
        return sb.toString();
    }

    public byte[] bytes(Object... args) {
        String str = "";
        if (args.length > 0 && args[0] != null && args[0] instanceof String) {
            str = (String) args[0];
        }

        return str.getBytes(Charset.forName("UTF-8"));
    }

    public String escape(Object... args) {
        StringBuilder sb = new StringBuilder();

        for (Object a : args) {
            sb.append(a != null ? a : "");
        }

        return StringEscapeUtils.escapeHtml4(sb.toString());
    }
    
    public String nltobr(Object... args) {
        StringBuilder sb = new StringBuilder();

        for (Object a : args) {
            sb.append(a != null ? a : "");
        }

        return sb.toString().replaceAll("\n", "<br>");
    }

    public String urlencode(Object... args) {
        StringBuilder sb = new StringBuilder();

        for (Object a : args) {
            sb.append(a != null ? a : "");
        }

        try {
            return URLEncoder.encode(sb.toString(), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return "";
        }
    }

}

@@ -243,16 +243,41 @@
 harset;%0A
+import java.util.Arrays;%0A
 import o
@@ -506,25 +506,24 @@
 .ctx = ctx;%0A
-%0A
     %7D%0A%0A    p
@@ -1890,20 +1890,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -2480,10 +2480,322 @@
 %0A    %7D%0A%0A
+    public String format(Object... args) %7B%0A        if (args.length == 0) %7B%0A            return %22%22;%0A        %7D%0A        if (args.length == 1) %7B%0A            return args%5B0%5D != null ? args%5B0%5D.toString() : %22%22;%0A        %7D%0A%0A        return String.format(args%5B0%5D.toString(), Arrays.copyOfRange(args, 1, args.length));%0A    %7D%0A%0A
 %7D%0A

Add format function to template api