package io.xpipe.ext.base.desktop;

import io.xpipe.app.ext.DataStore;
import io.xpipe.app.ext.DataStoreDependencies;
import io.xpipe.app.process.CommandBuilder;
import io.xpipe.app.storage.DataStoreEntryRef;
import io.xpipe.app.util.Validators;

import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Value;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

import java.util.List;

@Value
@SuperBuilder
@Jacksonized
@JsonTypeName("desktopApplication")
public class DesktopApplicationStore implements DataStore {

    DataStoreEntryRef<DesktopBaseStore> desktop;
    String path;
    String arguments;

    @Override
    public List<DataStoreEntryRef<?>> getDependencies() {
        return DataStoreDependencies.of(desktop);
    }

    @Override
    public void checkComplete() throws Throwable {
        Validators.nonNull(desktop);
        Validators.isType(desktop, DesktopBaseStore.class);
        desktop.checkComplete();
        Validators.nonNull(path);
    }

    public CommandBuilder getFullCommand() {
        var builder = CommandBuilder.of().addFile(path).add(arguments != null ? " " + arguments : "");
        builder = desktop.getStore().getUsedDesktopDialect().launchAsync(builder, true);
        return builder;
    }
}
