package io.xpipe.app.hub.comp;

import io.xpipe.app.comp.SimpleRegionBuilder;
import io.xpipe.app.comp.base.PrettyImageHelper;
import io.xpipe.app.comp.base.StackComp;
import io.xpipe.app.core.AppResources;
import io.xpipe.app.process.SystemState;
import io.xpipe.app.util.FilePath;
import io.xpipe.app.util.OsType;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.Region;

import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OsLogoComp extends SimpleRegionBuilder {

    private static final Map<String, String> ICONS = new HashMap<>();
    private static final String LINUX_DEFAULT_24 = "linux-24.png";
    private final StoreEntryWrapper wrapper;
    private final ObservableValue<SystemStateComp.State> state;

    public OsLogoComp(StoreEntryWrapper wrapper, ObservableValue<SystemStateComp.State> state) {
        this.wrapper = wrapper;
        this.state = state;
    }

    @Override
    protected Region createSimple() {
        var img = Bindings.createObjectBinding(
                () -> {
                    if (state.getValue() != SystemStateComp.State.SUCCESS) {
                        return null;
                    }

                    var ps = wrapper.getPersistentState().getValue();
                    if (!(ps instanceof SystemState ons)) {
                        return null;
                    }

                    return getImage(ons.getOsName(), ons.getOsType());
                },
                wrapper.getPersistentState(),
                state);
        var hide = Bindings.createBooleanBinding(
                () -> {
                    return img.get() != null;
                },
                img);
        return new StackComp(List.of(
                        new SystemStateComp(state).hide(hide),
                        PrettyImageHelper.ofFixedSize(img, 24, 24).visible(hide)))
                .build();
    }

    public static String getImage(String name, OsType.Any type) {
        if (name == null) {
            return null;
        }

        if (name.contains("Cisco")) {
            return null;
        }

        if (ICONS.isEmpty()) {
            AppResources.with(AppResources.MAIN_MODULE, "os", file -> {
                try (var list = Files.list(file)) {
                    list.filter(path -> path.toString().endsWith(".png")
                                    && !path.toString().contains("-dark")
                                    && !path.toString().endsWith(LINUX_DEFAULT_24)
                                    && !path.toString().endsWith("-40.png"))
                            .map(path -> path.getFileName().toString())
                            .forEach(path -> {
                                var base = path.replace("-dark", "").replace("-24.png", ".svg");
                                ICONS.put(
                                        FilePath.of(base)
                                                .getBaseName()
                                                .toString()
                                                .split("-")[0],
                                        "os/" + base);
                            });
                }
            });
        }

        var found = ICONS.entrySet().stream()
                .filter(e -> name.toLowerCase().contains(e.getKey())
                        || name.toLowerCase().replaceAll("\\s+", "").contains(e.getKey()))
                .findAny()
                .map(e -> e.getValue());
        if (found.isPresent()) {
            return found.get();
        }

        if (type == OsType.SOLARIS) {
            return "os/illumos.svg";
        }

        if (type == OsType.UNIX) {
            return "os/unix.svg";
        }

        return "os/linux.svg";
    }
}
