package io.xpipe.app.vnc;

import io.xpipe.app.ext.PrefsValue;
import io.xpipe.app.platform.ClipboardHelper;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.util.OsType;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import java.util.ArrayList;
import java.util.List;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface ExternalVncClient extends PrefsValue {

    static void launchClient(VncLaunchConfig configuration) throws Exception {
        var client = AppPrefs.get().vncClient.getValue();
        if (client == null) {
            return;
        }

        if (!client.supportsPasswords() && configuration.hasFixedPassword()) {
            var pw = configuration.retrievePassword();
            if (pw.isPresent()) {
                ClipboardHelper.copyPassword(pw.get(), false);
            }
        }

        client.launch(configuration);
    }

    static List<Class<?>> getClasses() {
        var l = new ArrayList<Class<?>>();
        l.add(InternalVncClient.class);
        switch (OsType.ofLocal()) {
            case OsType.Linux ignored -> {
                l.add(RemminaVncClient.class);
                l.add(TigerVncClient.Linux.class);
                l.add(RemoteViewerVncClient.Linux.class);
                l.add(RealVncClient.Linux.class);
                l.add(KrdcVncClient.class);
            }
            case OsType.MacOs ignored -> {
                l.add(ScreenSharingVncClient.class);
                l.add(TigerVncClient.MacOs.class);
                l.add(RemoteViewerVncClient.MacOs.class);
                l.add(RealVncClient.MacOs.class);
            }
            case OsType.Windows ignored -> {
                l.add(TigerVncClient.Windows.class);
                l.add(TightVncClient.class);
                l.add(RemoteViewerVncClient.Windows.class);
                l.add(RealVncClient.Windows.class);
            }
        }
        l.add(CustomVncClient.class);
        return l;
    }

    void launch(VncLaunchConfig configuration) throws Exception;

    boolean supportsPasswords();

    String getWebsite();
}
