package io.xpipe.app.terminal;

import io.xpipe.app.prefs.ExternalApplicationType;
import io.xpipe.app.process.CommandBuilder;
import io.xpipe.app.process.LocalShell;
import io.xpipe.app.process.ShellDialects;

public class CmdTerminalType
        implements ExternalApplicationType.PathApplication, ExternalTerminalType, TrackableTerminalType {

    @Override
    public TerminalDockMode getDockMode() {
        return TerminalDockMode.WITH_BORDER;
    }

    @Override
    public TerminalOpenFormat getOpenFormat() {
        return TerminalOpenFormat.NEW_WINDOW;
    }

    @Override
    public boolean isRecommended() {
        return false;
    }

    @Override
    public boolean useColoredTitle() {
        return false;
    }

    @Override
    public boolean supportsEscapes() {
        return false;
    }

    @Override
    public void launch(TerminalLaunchConfiguration configuration) throws Exception {
        var args = toCommand(configuration);
        launch(args);
    }

    @Override
    public int getProcessHierarchyOffset() {
        var powershell = ShellDialects.isPowershell(LocalShell.getDialect());
        return powershell ? 0 : -1;
    }

    private CommandBuilder toCommand(TerminalLaunchConfiguration configuration) {
        var pane = configuration.single();
        if (pane.getScriptDialect() == ShellDialects.CMD) {
            return CommandBuilder.of().add("/c").addFile(pane.getScriptFile());
        }

        return CommandBuilder.of().add("/c").add(pane.getDialectLaunchCommand());
    }

    @Override
    public String getExecutable() {
        return "cmd.exe";
    }

    @Override
    public boolean detach() {
        return true;
    }

    @Override
    public String getId() {
        return "app.cmd";
    }
}
