package lan.dk.podcastserver.manager.worker.finder;

import lan.dk.podcastserver.entity.Cover;
import lan.dk.podcastserver.entity.Podcast;
import lan.dk.podcastserver.service.HtmlService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.NotEmpty;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.util.stream.Stream;

import static java.util.Objects.nonNull;

/**
 * Created by kevin on 22/02/15.
 */
@Slf4j
@Service("YoutubeFinder")
@RequiredArgsConstructor
public class YoutubeFinder implements Finder {

    private final HtmlService htmlService;

    @Override
    public Podcast find(String url) {
        return htmlService
                .get(url)
                .map(p -> podcastFromHtml(url, p))
                .getOrElse(Podcast.DEFAULT_PODCAST);
    }

    private Podcast podcastFromHtml(String url, Document p) {
        return Podcast.builder()
                .url(url)
                .type("Youtube")
                .title(getTitle(p))
                .description(getDescription(p))
                .cover(getCover(p))
                .build();
    }

    private String getDescription(Document page) {
        Element elementWithExternalId = page.select("meta[name=description]").first();
        if (elementWithExternalId != null) {
            return elementWithExternalId.attr("content");
        }

        return "";
    }

    private String getTitle(Document page) {
        Element elementWithExternalId = page.select("meta[name=title]").first();
        if (elementWithExternalId != null) {
            return elementWithExternalId.attr("content");
        }

        return "";
    }

    private Cover getCover(Document page) {
        Element elementWithExternalId = page.select("img.channel-header-profile-image").first();
        if (nonNull(elementWithExternalId)) {
            return Cover
                    .builder()
                    .url(elementWithExternalId.attr("src"))
                    .height(200)
                    .width(200)
                    .build();
        }

        return new Cover();
    }


    public Integer compatibility(@NotEmpty String url) {
        return isYoutubeUrl(url) ? 1 : Integer.MAX_VALUE;
    }

    private Boolean isYoutubeUrl(String url) {
        return Stream.of("youtube.com/channel/", "youtube.com/user/", "youtube.com/", "gdata.youtube.com/feeds/api/playlists/").anyMatch(url::contains);
    }
}

@@ -46,16 +46,85 @@
 inder;%0A%0A
+import javaslang.collection.Stream;%0Aimport javaslang.control.Option;%0A
 import l
@@ -413,40 +413,8 @@
 nt;%0A
-import org.jsoup.nodes.Element;%0A
 impo
@@ -478,63 +478,25 @@
 til.
-stream.Stream;%0A%0Aimport static java.util.Objects.nonNull
+function.Function
 ;%0A%0A/
@@ -529,17 +529,16 @@
 22/02/15
-.
 %0A */%0A@Sl
@@ -1084,15 +1084,29 @@
 (get
-T
+Meta(p).apply(%22t
 itle
-(p
+%22
 ))%0A 
@@ -1136,17 +1136,32 @@
 tion(get
-D
+Meta(p).apply(%22d
 escripti
@@ -1162,18 +1162,17 @@
 cription
-(p
+%22
 ))%0A     
@@ -1251,359 +1251,95 @@
 ate 
-String getDescription(Document page) %7B%0A        Element elementWithExternalId = page.select(%22meta%5Bname=description%5D%22).first();%0A        if (elementWithExternalId != null) %7B%0A            return elementWithExternalId.attr(%22content%22);%0A        %7D%0A%0A        return %22%22;%0A    %7D%0A%0A    private String getTitle(Document page) %7B%0A        Element elementWithExternalId = 
+Function%3CString, String%3E getMeta(Document page) %7B%0A        return metaName -%3E Option.of(
 page
@@ -1357,21 +1357,30 @@
 ta%5Bname=
-title
+%22+ metaName +%22
 %5D%22).firs
@@ -1374,33 +1374,33 @@
 me +%22%5D%22).first()
-;
+)
 %0A        if (ele
@@ -1396,85 +1396,27 @@
     
-if (elementWithExternalId != null) %7B%0A            return elementWithExternalId
+        .map(e -%3E e
 .att
@@ -1427,17 +1427,17 @@
 ontent%22)
-;
+)
 %0A       
@@ -1433,35 +1433,32 @@
 %22))%0A        
-%7D%0A%0A
         
 return %22%22;%0A 
@@ -1449,17 +1449,22 @@
     
-return 
+.getOrElse(
 %22%22
+)
 ;%0A  
@@ -1524,40 +1524,25 @@
     
-Element elementWithExternalId = 
+return Option.of(
 page
@@ -1592,17 +1592,17 @@
 .first()
-;
+)
 %0A       
@@ -1606,71 +1606,77 @@
     
-if (nonNull(elementWithExternalId)) %7B%0A            return
+        .map(e -%3E e.attr(%22src%22))%0A                .map(e -%3E
  Cover%0A
+    
     
@@ -1726,48 +1726,24 @@
     
+    
 .url(e
-lementWithExternalId.attr(%22src%22))%0A
+)%0A    
     
@@ -1791,16 +1791,20 @@
         
+    
 .width(2
@@ -1823,32 +1823,36 @@
             
+ 
+   
 .build()
 ;%0A        %7D%0A
@@ -1835,25 +1835,24 @@
     .build()
-;
 %0A        %7D%0A%0A
@@ -1852,36 +1852,64 @@
     
-%7D%0A%0A        return new Cover(
+        )%0A                .getOrElse(Cover.DEFAULT_COVER
 );%0A 
@@ -2215,17 +2215,32 @@
 s/%22)
-.anyMatch
+%0A                .exists
 (url

use Option and functional pattern to simplify code