package com.huangzj.protoplugin;

/** 生成与 comm-framework 风格一致的 {@link com.huangzj.base.cmd.AbstractTypedPbCommand} 子类源码。 */
public final class JavaCommandGenerator {

    private JavaCommandGenerator() {}

    public static String generate(
            RpcMethodInfo rpc,
            String outputPackage,
            String protoJavaPackage,
            String pbServiceEnumConstant,
            String methodEnumConstant) {

        String cts = rpc.getRequestType();
        String stc = rpc.getResponseType();
        String className = rpc.getRpcName();
        String methodEnumClass = rpc.methodEnumClass();

        String imports =
                "import "
                        + protoJavaPackage
                        + "."
                        + cts
                        + ";\n"
                        + "import "
                        + protoJavaPackage
                        + "."
                        + stc
                        + ";\n"
                        + "import "
                        + protoJavaPackage
                        + ".PbService;\n"
                        + "import "
                        + protoJavaPackage
                        + "."
                        + methodEnumClass
                        + ";\n";

        return "package "
                + outputPackage
                + ";\n\n"
                + "import com.huangzj.base.cmd.AbstractTypedPbCommand;\n"
                + "import com.huangzj.base.cmd.Cmd;\n"
                + "import com.huangzj.base.cmd.CommandContext;\n"
                + "import com.huangzj.base.cmd.CommandSource;\n"
                + imports
                + "import org.springframework.stereotype.Component;\n\n"
                + "/**\n"
                + " * Generated from proto rpc: "
                + rpc.getServiceName()
                + "."
                + rpc.getRpcName()
                + "\n"
                + " * Request: "
                + cts
                + ", Response: "
                + stc
                + ".\n"
                + " */\n"
                + "@Component\n"
                + "@Cmd(source = CommandSource.Client_TCP, serviceId = PbService."
                + pbServiceEnumConstant
                + "_VALUE, methodId = "
                + methodEnumClass
                + "."
                + methodEnumConstant
                + "_VALUE)\n"
                + "public class "
                + className
                + " extends AbstractTypedPbCommand<"
                + cts
                + ", "
                + stc
                + ".Builder> {\n\n"
                + "    @Override\n"
                + "    protected void run(CommandContext ctx, "
                + cts
                + " cts, "
                + stc
                + ".Builder stcBuilder) throws Exception {\n"
                + "        // TODO: implement business logic\n"
                + "    }\n"
                + "}\n";
    }
}
