/**
 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easyagents.flow.core.node;

import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.easyagents.flow.core.chain.Chain;
import com.easyagents.flow.core.chain.Parameter;
import com.easyagents.flow.core.util.Maps;
import com.easyagents.flow.core.util.StringUtil;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

public class TemplateNode extends BaseNode {
    private static final long serialVersionUID = 1L;


    private static final Engine engine;
    private String template;

    static {
        // Enjoy 默认仅识别 ASCII 变量名，工作流参数需要支持中文名称。
        Engine.setChineseExpression(true);
        engine = Engine.create("template", e -> {
            e.addSharedStaticMethod(StringUtil.class);
        });
    }

    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }


    @Override
    public Map<String, Object> execute(Chain chain) {
        Map<String, Object> parameters =
                chain.getExecutionState().resolveParameters(this);

        ByteArrayOutputStream result = new ByteArrayOutputStream();

        Template templateByString = engine.getTemplateByString(template);
        templateByString.render(parameters, result);

        String outputDef = "output";
        List<Parameter> outputDefs = getOutputDefs();
        if (outputDefs != null && !outputDefs.isEmpty()) {
            String parameterName = outputDefs.get(0).getName();
            if (StringUtil.hasText(parameterName)) outputDef = parameterName;
        }

        return Maps.of(outputDef, result.toString());
    }


    @Override
    public String toString() {
        return "TemplateNode{" +
                "template='" + template + '\'' +
                ", parameters=" + parameters +
                ", outputDefs=" + outputDefs +
                ", id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", condition=" + condition +
                ", validator=" + validator +
                ", loopEnable=" + loopEnable +
                ", loopIntervalMs=" + loopIntervalMs +
                ", loopBreakCondition=" + loopBreakCondition +
                ", maxLoopCount=" + maxLoopCount +
                ", retryEnable=" + retryEnable +
                ", resetRetryCountAfterNormal=" + resetRetryCountAfterNormal +
                ", maxRetryCount=" + maxRetryCount +
                ", retryIntervalMs=" + retryIntervalMs +
                ", computeCostExpr='" + computeCostExpr + '\'' +
                '}';
    }
}
