package io.xpipe.app.vnc;

import io.xpipe.app.prefs.ExternalApplicationType;
import io.xpipe.app.process.CommandBuilder;
import io.xpipe.app.util.LocalFileTracker;
import io.xpipe.app.util.RemminaHelper;

import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Builder;
import lombok.extern.jackson.Jacksonized;

import java.util.Optional;

@Builder
@Jacksonized
@JsonTypeName("remmina")
public class RemminaVncClient implements ExternalApplicationType.LinuxApplication, ExternalVncClient {

    @Override
    public String getExecutable() {
        return "remmina";
    }

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

    @Override
    public void launch(VncLaunchConfig configuration) throws Exception {
        var pw = configuration.retrievePassword();
        var encrypted = pw.isPresent() ? RemminaHelper.encryptPassword(pw.get()) : Optional.<String>empty();
        var file = RemminaHelper.writeRemminaVncConfigFile(configuration, encrypted.orElse(null));
        launch(CommandBuilder.of().add("-c").addFile(file.toString()));
        LocalFileTracker.deleteOnExit(file);
    }

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

    @Override
    public String getWebsite() {
        return "https://remmina.org/";
    }

    @Override
    public String getFlatpakId() {
        return "org.remmina.Remmina";
    }
}
