package pl.hypeapp.episodie.core.entity.database;

import pl.hypeapp.episodie.core.entity.TvShowEntity;
import pl.hypeapp.episodie.core.entity.api.tvmaze.EpisodeRemote;
import pl.hypeapp.episodie.core.entity.api.tvmaze.SeasonRemote;
import pl.hypeapp.episodie.core.entity.api.tvmaze.TvShowRemote;

import java.util.List;
import java.util.stream.Collectors;

public class TvShowDatabaseAdapter implements TvShowEntity<SeasonLocal, EpisodeLocal> {

    private TvShowRemote tvShowRemote;

    public TvShowDatabaseAdapter(TvShowRemote tvShowRemote) {
        this.tvShowRemote = tvShowRemote;
    }

    @Override
    public String getTvShowApiId() {
        return this.tvShowRemote.getTvShowApiId();
    }

    @Override
    public String getImdbId() {
        return this.tvShowRemote.getImdbId();
    }

    @Override
    public String getName() {
        return this.tvShowRemote.getName();
    }

    @Override
    public String getStatus() {
        return this.tvShowRemote.getStatus();
    }

    @Override
    public String getOfficialSite() {
        return this.tvShowRemote.getOfficialSite();
    }

    @Override
    public Integer getRuntime() {
        return this.tvShowRemote.getRuntime();
    }

    @Override
    public Integer getFullRuntime() {
        return this.tvShowRemote.getFullRuntime();
    }

    @Override
    public String getPremiered() {
        return this.tvShowRemote.getPremiered();
    }

    @Override
    public String getNetworkName() {
        return this.tvShowRemote.getNetworkName();
    }

    @Override
    public String getGenre() {
        return this.tvShowRemote.getGenre();
    }

    @Override
    public String getSummary() {
        return this.tvShowRemote.getSummary();
    }

    @Override
    public String getImageMedium() {
        return this.tvShowRemote.getImageMedium();
    }

    @Override
    public String getImageOriginal() {
        return this.tvShowRemote.getImageOriginal();
    }

    @Override
    public Integer getUpdated() {
        return this.tvShowRemote.getUpdated();
    }

    public TvShowLocal apply() {
        return TvShowLocal.builder()
                .tvShowApiId(getTvShowApiId())
                .imdbId(getImdbId())
                .name(getName())
                .status(getStatus())
                .officialSite(getOfficialSite())
                .runtime(getRuntime())
                .fullRuntime(getFullRuntime())
                .premiered(getPremiered())
                .networkName(getNetworkName())
                .genre(getGenre())
                .summary(getSummary())
                .imageMedium(getImageMedium())
                .imageOriginal(getImageOriginal())
                .updated(getUpdated())
                .seasons(getSeasons())
                .episodes(getEpisodes())
                .build();
    }

    @Override
    public List<SeasonLocal> getSeasons() {
        return tvShowRemote.getSeasons()
                .stream()
                .map(this::convertToSeasonLocal)
                .collect(Collectors.toList());
    }

    @Override
    public List<EpisodeLocal> getEpisodes() {
        return tvShowRemote.getEpisodes()
                .stream()
                .map(this::convertToEpisodeLocal)
                .collect(Collectors.toList());
    }


    private SeasonLocal convertToSeasonLocal(SeasonRemote seasonRemote) {
        return SeasonLocal.builder()
                .seasonApiId(seasonRemote.getSeasonApiId())
                .runtime(calculateSeasonRuntime(seasonRemote.getSeasonNumber()))
                .url(seasonRemote.getUrl())
                .seasonNumber(seasonRemote.getSeasonNumber())
                .episodeOrder(seasonRemote.getEpisodeOrder())
                .premiereDate(seasonRemote.getPremiereDate())
                .endDate(seasonRemote.getEndDate())
                .summary(seasonRemote.getSummary())
                .imageMedium(seasonRemote.getImageMedium())
                .imageOriginal(seasonRemote.getImageOriginal())
                .build();
    }

    private Integer calculateSeasonRuntime(Integer seasonNumber) {
        return tvShowRemote.getEpisodes().stream()
                .filter(episodeRemote -> episodeRemote.getSeasonNumber().equals(seasonNumber))
                .mapToInt(EpisodeRemote::getRuntime)
                .sum();
    }

    private EpisodeLocal convertToEpisodeLocal(EpisodeRemote episodeRemote) {
        return EpisodeLocal.builder()
                .episodeApiId(episodeRemote.getEpisodeApiId())
                .url(episodeRemote.getUrl())
                .name(episodeRemote.getName())
                .seasonNumber(episodeRemote.getSeasonNumber())
                .episodeNumber(episodeRemote.getEpisodeNumber())
                .premiereDate(episodeRemote.getPremiereDate())
                .runtime(episodeRemote.getRuntime())
                .imageMedium(episodeRemote.getImageMedium())
                .imageOriginal(episodeRemote.getImageOriginal())
                .summary(episodeRemote.getSummary())
                .build();
    }

}

@@ -40,24 +40,83 @@
 .database;%0A%0A
+import org.jsoup.Jsoup;%0Aimport org.jsoup.safety.Whitelist;%0A
 import pl.hy
@@ -156,16 +156,16 @@
 Entity;%0A
-
 import p
@@ -1740,32 +1740,47 @@
 %0A        return 
+escapeHtmlTags(
 this.tvShowRemot
@@ -1793,16 +1793,17 @@
 ummary()
+)
 ;%0A    %7D%0A
@@ -3920,16 +3920,31 @@
 summary(
+escapeHtmlTags(
 seasonRe
@@ -3953,32 +3953,33 @@
 te.getSummary())
+)
 %0A               
@@ -5083,16 +5083,31 @@
 summary(
+escapeHtmlTags(
 episodeR
@@ -5117,32 +5117,33 @@
 te.getSummary())
+)
 %0A               
@@ -5160,10 +5160,230 @@
 %0A    %7D%0A%0A
+    private String escapeHtmlTags(String input) %7B%0A        if (input != null) %7B%0A            return Jsoup.clean(this.tvShowRemote.getSummary(), Whitelist.none());%0A        %7D else %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

clean html tags.