package org.codiki.images;

import java.io.IOException;
import java.security.Principal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codiki.core.entities.dto.ImageDTO;
import org.codiki.core.entities.persistence.Image;
import org.codiki.core.entities.persistence.User;
import org.codiki.core.repositories.ImageRepository;
import org.codiki.core.repositories.UserRepository;
import org.codiki.core.services.FileUploadService;
import org.codiki.core.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ImageService {
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private FileUploadService fileUploadService;
	
	@Autowired
	private ImageRepository imageRepository;

	public String uploadAvatar(final MultipartFile pFile, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse, final Principal pPrincipal) throws IOException {
		final String avatarFileName = fileUploadService.uploadProfileImage(pFile);
		
		final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
		if(connectedUser.isPresent()) {
			final Optional<User> userFromDb = userRepository.findById(connectedUser.get().getId());
			if(userFromDb.isPresent()) {
				userFromDb.get().setImage(avatarFileName);
				userRepository.save(userFromDb.get());
			} else {
				pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
			}
		} else {
			pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		}
		
		return avatarFileName;
	}
	
	public String uploadImage(final MultipartFile pFile, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse, final Principal pPrincipal) throws IOException {
		final String imageFileName = fileUploadService.uploadImage(pFile);
		
		final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
		if(connectedUser.isPresent()) {
			final Optional<User> userFromDb = userRepository.findById(connectedUser.get().getId());
			if(userFromDb.isPresent()) {
				final Image image = new Image();
				image.setLink(imageFileName);
				image.setDate(new Date());
				image.setUser(userFromDb.get());
				imageRepository.save(image);
			} else {
				pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
			}
		} else {
			pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		}
		
		return imageFileName;
	}
	
	public Resource loadAvatar(final String pAvatarFileName) {
		return fileUploadService.loadAvatar(pAvatarFileName);
	}
	
	public Resource loadImage(final String pImageLink) {
		return fileUploadService.loadImage(pImageLink);
	}
	
	public List<ImageDTO> getUserImages(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
			final Principal pPrincipal) throws IOException {
		List<ImageDTO> result = new LinkedList<>();
		
		final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
		if(connectedUser.isPresent()) {
			result = imageRepository.getByUserId(connectedUser.get().getId())
					.stream().map(ImageDTO::new).collect(Collectors.toList());
		} else {
			pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		}
		
		return result;
	}

	public ImageDTO getImageDetails(final String pImageLink, final HttpServletResponse pResponse) throws IOException {
		ImageDTO result = null;
		
		final Optional<Image> imageFromDb = imageRepository.findByLink(pImageLink);
		if(imageFromDb.isPresent()) {
			result = new ImageDTO(imageFromDb.get());
		} else {
			pResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
		
		return result;
	}
}
