package lan.dk.podcastserver.service;

import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Podcast;
import lan.dk.podcastserver.manager.worker.downloader.Downloader;
import lan.dk.podcastserver.manager.worker.finder.Finder;
import lan.dk.podcastserver.manager.worker.selector.DownloaderSelector;
import lan.dk.podcastserver.manager.worker.selector.UpdaterSelector;
import lan.dk.podcastserver.manager.worker.updater.AbstractUpdater;
import lan.dk.podcastserver.manager.worker.updater.Updater;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.toSet;


@Component("WorkerService")
public class WorkerService implements ApplicationContextAware {

    protected ApplicationContext context = null;
    
    @Resource UpdaterSelector updaterSelector;
    @Resource DownloaderSelector downloaderSelector;
    @Resource List<Updater> updaters;

    public Set<AbstractUpdater.Type> types() {
        return updaters
                .stream()
                .map(Updater::type)
                .collect(toSet());
    }

    public Updater updaterOf(Podcast podcast) {
        return (Updater) context.getBean(updaterSelector.of(podcast.getUrl()).getSimpleName());
    }

    public Downloader getDownloaderByType(Item item) {
        return ((Downloader) context.getBean(downloaderSelector.of(item.getUrl().toLowerCase()).getSimpleName()))
                .setItem(item);
    }

    public Finder getFinderByUrl(String url) {
        
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        
        String finderName = "RSS";
        
        if (isYoutubeUrl(url, "youtube.com/channel/", "youtube.com/user/", "youtube.com/", "gdata.youtube.com/feeds/api/playlists/")) {
            finderName = "Youtube";
        }
            
        return context.getBean(finderName + "Finder", Finder.class);
    }

    private Boolean isYoutubeUrl(String url, String ... strings) {
        return Arrays.stream(strings)
                .anyMatch(url::contains);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}

@@ -604,16 +604,79 @@
 eption;%0A
+import org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -722,16 +722,16 @@
 ontext;%0A
-
 import o
@@ -836,42 +836,8 @@
 t;%0A%0A
-import javax.annotation.Resource;%0A
 impo
@@ -1105,25 +1105,21 @@
    %0A    
-@Resource
+final
  Updater
@@ -1152,84 +1152,351 @@
     
-@Resource DownloaderSelector downloaderSelector;%0A    @Resource List%3CU
+final DownloaderSelector downloaderSelector;%0A    final List%3CUpdater%3E updaters;%0A%0A    @Autowired%0A    public WorkerService(UpdaterSelector updaterSelector, DownloaderSelector downloaderSelector, List%3CUpdater%3E updaters) %7B%0A        this.updaterSelector = updaterSelector;%0A        this.downloaderSelector = downloaderSelector;%0A        this.u
 pdater
-%3E
+s =
  upd
@@ -1497,24 +1497,30 @@
  = updaters;
+%0A    %7D
 %0A%0A    public

add the forgotten class