package leigh.continuity.line;

import leigh.continuity.cmd.CommandProcessor;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

public class CommandLineProcessor implements LineProcessor {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(CommandLineProcessor.class);
    private final CommandProcessor proc;

    public CommandLineProcessor(CommandProcessor proc) {
        this.proc = proc;
    }

    public String buildMessage(String s) {
        StringBuilder buf = new StringBuilder();
        if (s != null && !s.isBlank()) {
            buf.append(s);
        }
        return buf.toString();
    }

    @Override
    public String handleLine(String input) throws Exception {
        if (input.isBlank()) {
            return buildMessage("");
        }

        String[] inArr = input.split(" ");
        String response = proc.handleCommand(inArr);
        if (response == null) {
            response = "";
        }
        return buildMessage(response);
    }
}
