package io.xpipe.app.util;

import io.xpipe.app.core.AppSystemInfo;
import io.xpipe.app.process.CommandBuilder;
import io.xpipe.app.process.LocalShell;
import io.xpipe.app.process.PropertiesFormatsParser;

import lombok.Builder;
import lombok.Value;

import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

public class FlatpakCache {

    @Value
    @Builder
    public static class App {

        String id;
        String name;
        Path installDir;
    }

    private static final Map<String, App> apps = new LinkedHashMap<>();

    public static synchronized Optional<App> getApp(String id) throws Exception {
        if (apps.containsKey(id)) {
            return Optional.ofNullable(apps.get(id));
        }

        var info = LocalShell.getShell()
                .command(CommandBuilder.of().add("flatpak", "info").addQuoted(id))
                .readStdoutIfPossible();
        if (info.isEmpty()) {
            apps.put(id, null);
            return Optional.empty();
        }

        var props = PropertiesFormatsParser.parse(info.get(), ":");
        var name = props.get("Name");

        var loc = Path.of(LocalShell.getShell()
                .command(CommandBuilder.of()
                        .add("flatpak", "info", "--show-location")
                        .addQuoted(id))
                .readStdoutOrThrow());

        var app = App.builder().id(id).name(name).installDir(loc).build();
        apps.put(id, app);
        return Optional.ofNullable(app);
    }

    public static CommandBuilder getRunCommand(String id) {
        return CommandBuilder.of()
                .add("flatpak", "run")
                .add("--filesystem=" + AppSystemInfo.ofLinux().getTemp())
                .add("--filesystem=host")
                .addQuoted(id);
    }
}
