package lan.dk.podcastserver.business;

import lan.dk.podcastserver.entity.Item;
import lan.dk.podcastserver.entity.Podcast;
import lan.dk.podcastserver.exception.PodcastNotFoundException;
import lan.dk.podcastserver.repository.PodcastRepository;
import lan.dk.podcastserver.service.JdomService;
import lan.dk.podcastserver.service.MimeTypeService;
import lan.dk.podcastserver.service.properties.PodcastServerParameters;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

@Slf4j
@Component
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PodcastBusiness {

    final PodcastServerParameters podcastServerParameters;
    final JdomService jdomService;
    final PodcastRepository podcastRepository;
    final TagBusiness tagBusiness;
    final CoverBusiness coverBusiness;
    final MimeTypeService mimeTypeService;

    //** Delegate du Repository **//
    public List<Podcast> findAll() {
        return podcastRepository.findAll();
    }

    public Podcast save(Podcast entity) {
        return podcastRepository.save(entity);
    }

    public Podcast findOne(UUID id) {
        return podcastRepository.findOne(id);
    }

    public void delete(UUID id) {
        podcastRepository.delete(id);
        //TODO : Delete the folder with java.nio.PATH and java.nio.FILES
    }

    public void delete(Podcast entity) {
        podcastRepository.delete(entity);
        //TODO : Delete the folder with java.nio.PATH and java.nio.FILES
    }

    public List<Podcast> findByUrlIsNotNull() {
        return podcastRepository.findByUrlIsNotNull();
    }

    //*****//
    public Podcast patchUpdate(Podcast patchPodcast) {
        Podcast podcastToUpdate = this.findOne(patchPodcast.getId());

        if (podcastToUpdate == null)
            throw new PodcastNotFoundException();

        /*
        // Move folder if name has change :
        if (StringUtils.equals(podcastToUpdate.getTitle(), patchPodcast.getTitle())) {
                TODO : Move Folder to new Location using java.nio.FILES and java.nio.PATH
                It must add modification on each item of the podcast (localUrl)

        }
        */

        podcastToUpdate.setTitle(patchPodcast.getTitle());
        podcastToUpdate.setUrl(patchPodcast.getUrl());
        podcastToUpdate.setSignature(patchPodcast.getSignature());
        podcastToUpdate.setType(patchPodcast.getType());

        if (!coverBusiness.hasSameCoverURL(patchPodcast, podcastToUpdate)) {
            patchPodcast.getCover().setUrl(coverBusiness.download(patchPodcast));
        }
        
        podcastToUpdate.setCover(
                coverBusiness.findOne(patchPodcast.getCover().getId())
                    .setHeight(patchPodcast.getCover().getHeight())
                    .setUrl(patchPodcast.getCover().getUrl())
                    .setWidth(patchPodcast.getCover().getWidth())
        );
        podcastToUpdate.setDescription(patchPodcast.getDescription());
        podcastToUpdate.setHasToBeDeleted(patchPodcast.getHasToBeDeleted());
        podcastToUpdate.setTags(patchPodcast.getTags());

        return this.reatachAndSave(podcastToUpdate);
    }

    @Transactional(readOnly = true)
    public String getRss(UUID id, Boolean limit, String domainName) {
        try {
            return jdomService.podcastToXMLGeneric(findOne(id), limit, domainName);
        } catch (IOException e) {
            log.error("Unable to generate RSS for podcast {} with limit {}", id, limit, e);
            return "";
        }
    }

    public Set<Item> getItems(UUID id){
        return podcastRepository.findOne(id).getItems();
    }

    public Podcast reatachAndSave(Podcast podcast) {
        podcast.setTags(tagBusiness.getTagListByName(podcast.getTags()));
        return save(podcast);
    }
    
    public Podcast create(Podcast podcast) {
        Podcast podcastSaved = reatachAndSave(podcast);

        if (!Objects.isNull(podcast.getCover())) {
            coverBusiness.save(podcast.getCover().setUrl(coverBusiness.download(podcast)));
        }

        return podcastSaved;
    }

    public Path coverOf(UUID id) {
        return coverBusiness.getCoverPathOf(findOne(id));
    }
}

@@ -2509,59 +2509,170 @@
     
-podcastToUpdate.setTitle(patchPodcast.getTitle(
+if (!coverBusiness.hasSameCoverURL(patchPodcast, podcastToUpdate)) %7B%0A            patchPodcast.getCover().setUrl(coverBusiness.download(patchPodcast
 ));%0A
+        %7D%0A%0A
     
@@ -2690,23 +2690,42 @@
 ToUpdate
-.setUrl
+%0A                .setTitle
 (patchPo
@@ -2737,15 +2737,16 @@
 .get
-Url
+Title
 ())
-;
 %0A   
@@ -2746,39 +2746,79 @@
 ())%0A        
-podcastToUpdate
+        .setUrl(patchPodcast.getUrl())%0A                
 .setSignatur
@@ -2847,17 +2847,16 @@
 ature())
-;
 %0A       
@@ -2852,39 +2852,32 @@
 ())%0A        
-podcastToUpdate
+        
 .setType(pat
@@ -2896,18 +2896,16 @@
 tType())
-;%0A
 %0A       
@@ -2909,142 +2909,185 @@
     
-if (!coverBusiness.hasSameCoverURL(patchPodcast, podcastToUpdate)) %7B%0A            patchPodcast.getCover().setUrl(coverBusiness.download
+        .setDescription(patchPodcast.getDescription())%0A                .setHasToBeDeleted(patchPodcast.getHasToBeDeleted())%0A                .setTags(tagBusiness.getTagListByName
 (pat
@@ -3087,35 +3087,44 @@
 ame(patchPodcast
+.getTags(
 ))
-;
+)
 %0A        %7D%0A     
@@ -3116,26 +3116,24 @@
         
-%7D%0A
         
 %0A       
@@ -3128,32 +3128,8 @@
     
-%0A        podcastToUpdate
 .set
@@ -3135,16 +3135,20 @@
 tCover(%0A
+    
         
@@ -3226,24 +3226,28 @@
             
+  
+  
 .setHeight(p
@@ -3294,32 +3294,36 @@
                 
+    
 .setUrl(patchPod
@@ -3340,32 +3340,36 @@
 ver().getUrl())%0A
+    
                 
@@ -3426,27 +3426,24 @@
         
-);%0A
         
 podcastT
@@ -3438,204 +3438,11 @@
     
-podcastToUpdate.setDescription(patchPodcast.getDescription());%0A        podcastToUpdate.setHasToBeDeleted(patchPodcast.getHasToBeDeleted());%0A        podcastToUpdate.setTags(patchPodcast.getTags()
 );
+%0A
 %0A%0A  
@@ -3458,24 +3458,9 @@
 urn 
-this.reatachAndS
+s
 ave(

simplify code and logic around the save operation