package com.wizzardo.http.framework.template.taglib.g;

import com.wizzardo.http.framework.di.DependencyFactory;
import com.wizzardo.http.framework.message.MessageSource;
import com.wizzardo.http.framework.template.*;
import com.wizzardo.tools.evaluation.AsBooleanExpression;

import java.util.Map;

/**
 * Created by wizzardo on 24.05.15.
 */
public class FormatBoolean extends Tag {

    protected MessageSource messageSource = DependencyFactory.getDependency(MessageSource.class);

    @Override
    public Tag init(Map<String, String> attrs, Body body, String offset) {
        ExpressionHolder raw = asExpression(attrs, "boolean", false, true);

        Renderable trueString = getValueString("true", attrs);
        Renderable falseString = getValueString("false", attrs);

        append(offset);
        append(model -> {
            Object value = raw.getRaw(model);
            if (AsBooleanExpression.toBoolean(value))
                return trueString.get(model);
            else
                return falseString.get(model);
        });
        append("\n");
        return this;
    }

    protected Renderable getValueString(String name, Map<String, String> attrs) {
        String value = attrs.remove(name);
        if (value != null)
            return asExpression(value, true);

        value = messageSource.get("boolean." + name);
        if (value != null)
            return Renderable.create(value);

        value = messageSource.get("default.boolean." + name);
        return Renderable.create(value);
    }
}

@@ -376,18 +376,46 @@
 nds Tag 
+implements RenderableString 
 %7B%0A
-
 %0A    pro
@@ -712,22 +712,26 @@
 getValue
-String
+Renderable
 (%22true%22,
@@ -780,22 +780,26 @@
 getValue
-String
+Renderable
 (%22false%22
@@ -1164,22 +1164,26 @@
 getValue
-String
+Renderable
 (String 
@@ -1515,32 +1515,32 @@
 lean.%22 + name);%0A
-
         return R
@@ -1569,11 +1569,651 @@
 );%0A    %7D
+%0A%0A    protected String getValueString(String name, Map%3CString, Object%3E attrs) %7B%0A        Object value = attrs.remove(name);%0A        if (value != null)%0A            return String.valueOf(value);%0A%0A        String message = messageSource.get(%22boolean.%22 + name);%0A        if (message != null)%0A            return message;%0A%0A        message = messageSource.get(%22default.boolean.%22 + name);%0A        return message;%0A    %7D%0A%0A    @Override%0A    public String render(Map%3CString, Object%3E model) %7B%0A        if ((Boolean) model.get(%22boolean%22))%0A            return getValueString(%22true%22, model);%0A        else%0A            return getValueString(%22false%22, model);%0A    %7D
 %0A%7D%0A

implement RenderableString for FormatBoolean