package io.xpipe.app.prefs;

import io.xpipe.app.comp.BaseRegionBuilder;
import io.xpipe.app.comp.base.ButtonComp;
import io.xpipe.app.comp.base.InputGroupComp;
import io.xpipe.app.comp.base.TextFieldComp;
import io.xpipe.app.core.AppProperties;
import io.xpipe.app.issue.TrackEvent;
import io.xpipe.app.platform.LabelGraphic;
import io.xpipe.app.platform.OptionsBuilder;
import io.xpipe.app.process.LocalShell;
import io.xpipe.app.process.ProcessOutputException;
import io.xpipe.app.util.ThreadHelper;

import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

import org.kordamp.ikonli.javafx.FontIcon;

import java.util.List;

public class DeveloperCategory extends AppPrefsCategory {

    @Override
    protected boolean show() {
        return AppPrefs.get().developerMode().getValue();
    }

    @Override
    protected String getId() {
        return "developer";
    }

    @Override
    protected LabelGraphic getIcon() {
        return new LabelGraphic.IconGraphic("mdi2c-code-tags");
    }

    @Override
    protected BaseRegionBuilder<?, ?> create() {
        var prefs = AppPrefs.get();
        var localCommand = new SimpleStringProperty();
        Runnable test = () -> {
            var cmd = localCommand.get();
            if (cmd == null) {
                return;
            }

            ThreadHelper.runFailableAsync(() -> {
                try {
                    TrackEvent.info(LocalShell.getShell().executeSimpleStringCommand(cmd));
                } catch (ProcessOutputException ex) {
                    TrackEvent.error(ex.getOutput());
                }
            });
        };

        var runLocalCommand = new InputGroupComp(List.of(
                        new TextFieldComp(localCommand)
                                .apply(struc -> struc.setPromptText("Local command"))
                                .hgrow(),
                        new ButtonComp(null, new FontIcon("mdi2p-play"), test)))
                .setMainReference(0)
                .padding(new Insets(15, 0, 0, 0))
                .apply(struc -> struc.setAlignment(Pos.CENTER_LEFT))
                .apply(struc -> struc.setFillHeight(true))
                .maxWidth(600);
        var sub = new OptionsBuilder()
                .nameAndDescription("developerDisableUpdateVersionCheck")
                .addToggle(prefs.developerDisableUpdateVersionCheck)
                .nameAndDescription("developerPrintInitFiles")
                .addToggle(prefs.developerPrintInitFiles)
                .nameAndDescription("developerShowSensitiveCommands")
                .addToggle(prefs.developerShowSensitiveCommands);
        if (AppProperties.get().isDevelopmentEnvironment()) {
            sub.nameAndDescription("developerForceSshTty").addToggle(prefs.developerForceSshTty);
            sub.nameAndDescription("developerDisableSshTunnelGateways")
                    .addToggle(prefs.developerDisableSshTunnelGateways);
        }
        sub.nameAndDescription("shellCommandTest").addComp(runLocalCommand);
        return new OptionsBuilder().title("developer").sub(sub).buildComp();
    }
}
