package de.thpeetz.kontor.media.services;

import de.thpeetz.kontor.common.views.SearchFilter;
import de.thpeetz.kontor.media.data.MediaActor;
import de.thpeetz.kontor.media.data.MediaActorFile;
import de.thpeetz.kontor.media.data.MediaFile;
import de.thpeetz.kontor.media.repository.MediaActorFileRepository;
import de.thpeetz.kontor.media.repository.MediaActorRepository;
import de.thpeetz.kontor.media.repository.MediaFileRepository;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.stereotype.Service;


@Slf4j
@Service
public class MediaFileService {

    private final MediaFileRepository mediaFileRepository;
    private final MediaActorRepository mediaActorRepository;
    private final MediaActorFileRepository mediaActorFileRepository;

    public MediaFileService(MediaFileRepository mediaFileRepository, MediaActorRepository mediaActorRepository, MediaActorFileRepository mediaActorFileRepository) {
        this.mediaFileRepository = mediaFileRepository;
        this.mediaActorRepository = mediaActorRepository;
        this.mediaActorFileRepository = mediaActorFileRepository;
    }

    public List<MediaFile> findAllMediaFilesByString(String stringFilter) {
        List<MediaFile> results;
        if (stringFilter == null || stringFilter.isEmpty()) {
            results = mediaFileRepository.findAll();
        } else {
            results = mediaFileRepository.search(stringFilter);
        }
        log.debug("Found " + results.size() + " entries");
        return results;
    }

    public MediaFile findAllMediaFilesByUrl(String url) {
        return mediaFileRepository.findByUrl(url);
    }

    public List<MediaFile> findAllMediaFiles(SearchFilter searchFilter) {
        if (searchFilter == null) {
            return mediaFileRepository.findAll();
        } else {
            if (searchFilter.getSearchTerm() != null && searchFilter.getFilterOptions().isEmpty()) {
                log.info("find MediaFiles by using searchTerm: {}", searchFilter.getSearchTerm());
                List<MediaFile> results =  mediaFileRepository.search(searchFilter.getSearchTerm());
                log.info("found {} entries", results.size());
                return results;
            }
            if (searchFilter.getFilterOptions().size() == 1) {
                log.info("using searchFilter: {}", searchFilter);
                String filter = searchFilter.getFilterOptions().get(0).getName();
                Boolean filterValue = searchFilter.getFilterOptions().get(0).getValue();
                if (filter == "Überprüfung") {
                    List<MediaFile> results = mediaFileRepository.findByReview(filterValue);
                    log.info("found {} entries", results.size());
                    return results;
                }
                if (filter == "Download") {
                    List<MediaFile> results = mediaFileRepository.findByShouldDownload(filterValue);
                    log.info("found {} entries", results.size());
                    return results;
                }
            }
            if (searchFilter.getFilterOptions().size() == 2) {
                log.info("using searchFilter: {}", searchFilter);
                List<MediaFile> results = mediaFileRepository.search(searchFilter.getSearchTerm(), searchFilter.getFilterOptions().get(0).getValue(), searchFilter.getFilterOptions().get(1).getValue());
                log.info("found {} entries", results.size());
                return results;
            }
        }
        log.info("noch filter used");
        return mediaFileRepository.findAll();
    }

    public MediaFile saveMediaFile(MediaFile mediaFile) {
        if (mediaFile == null) {
            log.warn("MediaFile is null. Are you sure you have connected your form to the application?");
            return null;
        }
        return mediaFileRepository.save(mediaFile);
    }

    public void deleteMediaFile(MediaFile mediaFile) {
        mediaFileRepository.delete(mediaFile);
    }

    public List<MediaActor> findAllMediaActors(String stringFilter) {
        List<MediaActor> results;
        if (stringFilter == null || stringFilter.isEmpty()) {
            results = mediaActorRepository.findAll();
        } else {
            results = mediaActorRepository.search(stringFilter);
        }
        log.debug("Found " + results.size() + " entries");
        return results;
    }

    public void saveMediaActor(MediaActor mediaActor) {
        if (mediaActor == null) {
            log.warn("MediaActor is null. Are you sure you have connected your form to the application?");
            return;
        }
        mediaActorRepository.save(mediaActor);
    }

    public void deleteMediaActor(MediaActor mediaActor) {
        mediaActorRepository.delete(mediaActor);
    }

    public List<MediaActorFile> findAllMediaActorFiles() {
        return mediaActorFileRepository.findAll();
    }

    public void saveMediaActorFile(MediaActorFile mediaActorFile) {
        if (mediaActorFile == null){
            log.warn("MediaActorFile is null. Are you sure you have connected your form to the application?");
            return;
        }
        mediaActorFileRepository.save(mediaActorFile);
    }

    public void deleteMediaActorFile(MediaActorFile mediaActorFile) {
        MediaFile mediaFile = mediaActorFile.getMedia_file();
        mediaFile.getMediaActorFiles().remove(mediaActorFile);
        mediaFileRepository.save(mediaFile);
        MediaActor mediaActor = mediaActorFile.getMedia_actor();
        mediaActor.getMediaActorFiles().remove(mediaActorFile);
        mediaActorRepository.save(mediaActor);
        mediaActorFileRepository.delete(mediaActorFile);
    }
}
