package de.htwsaar.webshop.service.impl;

import de.htwsaar.webshop.model.ImageModel;
import de.htwsaar.webshop.repository.ImageRepository;
import de.htwsaar.webshop.repository.entities.Article;
import de.htwsaar.webshop.repository.entities.Image;
import de.htwsaar.webshop.service.ArticleService;
import de.htwsaar.webshop.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
@Transactional
public class ImageServiceImpl implements ImageService {
    private final ImageRepository imageRepository;
    private final ArticleService articleService;

    @Autowired
    public ImageServiceImpl(ImageRepository imageRepository, ArticleService articleService) {
        this.imageRepository = imageRepository;
        this.articleService = articleService;
    }

    @Override
    public List<Image> getImagesByUUID(UUID uuid) {
        return imageRepository.findImagesByArticle_Uuid(uuid);
    }

    @Override
    public Image getImageByUUID(UUID uuid) {
        return imageRepository.findImageByArticle_Uuid(uuid);
    }

    @Override
    public Image getImageById(Long imageId) {
        return imageRepository.findImageById(imageId);
    }

    @Override
    @Transactional
    public Image save(Image image) {
        return imageRepository.save(image);
    }

    @Override
    public Image save(ImageModel model) {
        return save(new Image(model.getId(), articleService.findByUUID(model.getArticleUuid()), model.getBase64()));
    }

    @Override
    public Image save(UUID uuid, MultipartFile file) {
        if (uuid == null) {
            log.warn("Got no UUID, aborting");
            return null;
        }
        Article article = articleService.findByUUID(uuid);
        if (article == null) {
            log.warn("Could not find article with id {}", uuid);
            return null;
        }
        if (file == null) {
            log.warn("Got no file for {}", uuid);
            return null;
        }

        try {
            String based64 = Base64.getEncoder().encodeToString(file.getBytes());
            if (based64 == null || based64.isEmpty()) {
                log.warn("Could not save image with id {} and file size {}", uuid, file.getSize());
                return null;
            }
            return imageRepository.save(new Image(null, article, based64));
        } catch (Exception e) {
            log.warn("Error saving image {} for article {}", file, uuid, e);
        }
        return null;
    }

    @Override
    public Image save(UUID uuid, String base64) {
        if (uuid == null) {
            log.warn("Got no UUID, aborting");
            return null;
        }
        Article article = articleService.findByUUID(uuid);
        if (article == null) {
            log.warn("Could not find article with id {}", uuid);
            return null;
        }
        if (base64 == null || base64.isEmpty()) {
            log.warn("Got no base64 for {}", uuid);
            return null;
        }
        return imageRepository.save(new Image(null, article, base64));
    }

    @Override
    @Transactional
    public void deleteById(Long imageId) {
        imageRepository.deleteById(imageId);
    }
}
