package io.xpipe.ext.system.incus;

import io.xpipe.app.action.AbstractAction;
import io.xpipe.app.core.AppI18n;
import io.xpipe.app.hub.action.HubLeafProvider;
import io.xpipe.app.hub.action.StoreAction;
import io.xpipe.app.platform.LabelGraphic;
import io.xpipe.app.storage.DataStoreEntryRef;
import io.xpipe.app.terminal.TerminalLaunch;

import javafx.beans.value.ObservableValue;

import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

public class IncusContainerEditConfigActionProvider implements HubLeafProvider<IncusContainerStore> {

    @Override
    public AbstractAction createAction(DataStoreEntryRef<IncusContainerStore> ref) {
        return Action.builder().ref(ref).build();
    }

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

    @Override
    public ObservableValue<String> getName(DataStoreEntryRef<IncusContainerStore> store) {
        return AppI18n.observable("editConfiguration");
    }

    @Override
    public LabelGraphic getIcon(DataStoreEntryRef<IncusContainerStore> store) {
        return new LabelGraphic.IconGraphic("mdi2f-file-document-edit");
    }

    @Override
    public Class<IncusContainerStore> getApplicableClass() {
        return IncusContainerStore.class;
    }

    @Override
    public String getId() {
        return "editIncusContainerConfig";
    }

    @Jacksonized
    @SuperBuilder
    public static class Action extends StoreAction<IncusContainerStore> {

        @Override
        public void executeImpl() throws Exception {
            var d = ref.getStore();
            var view = d.view();
            TerminalLaunch.builder()
                    .entry(ref.get())
                    .title("Config")
                    .command(view.configEdit(d.getName()))
                    .launch();
        }

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