package de.oaa.libredeck.web.service.streaming;

import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class StreamingProviderRegistry {

    private final Map<String, StreamingProvider> providers;

    public StreamingProviderRegistry(List<StreamingProvider> providers) {
        this.providers = providers.stream()
                .collect(Collectors.toMap(StreamingProvider::getId, Function.identity()));
    }

    public StreamingProvider get(String id) {
        StreamingProvider provider = providers.get(id);
        if (provider == null) {
            throw new IllegalArgumentException("Unknown streaming provider: " + id);
        }
        return provider;
    }

    public Optional<StreamingProvider> findForUrl(String url) {
        return providers.values().stream()
                .filter(p -> p.supportsUrl(url))
                .findFirst();
    }

    public Collection<StreamingProvider> getAll() {
        return providers.values();
    }
}
