package com.itdominator.api.services;


import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.itdominator.api.dto.ThumbnailDto;
import com.itdominator.api.dto.ThumbnailSearchCriteria;
import com.itdominator.api.entities.Thumbnails;
import com.itdominator.api.repositories.ThumbnailRepository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;



@RequiredArgsConstructor
@Service
@Validated
@Slf4j
public class ThumbnailerService {

    private final ThumbnailRepository thumbnailRepository;


    public ThumbnailDto getThumbnailById(final int id) {
        return thumbnailRepository.findThumbnailById(id);
    }


    public ThumbnailDto getThumbnailByHash(final String fileHash) {
        return thumbnailRepository.findThumbnailByHash(fileHash);
    }

    public ThumbnailDto getThumbnailByIdOrHashQuery(final ThumbnailSearchCriteria criteria) {
        if (criteria.getId() != 0) {
            return thumbnailRepository.findThumbnailById(criteria.getId());
        } else if (criteria.getHash() != null) {
            return thumbnailRepository.findThumbnailByHash(criteria.getHash());
        }

        return new ThumbnailDto();
    }

    public List<ThumbnailDto> getAllThumbnails() {
        return thumbnailRepository.getAllThumbnails();
    }

    public void saveThumbnail(final Thumbnails thumbnail) {
        thumbnailRepository.save(thumbnail);
    }

}
