package org.ros.chatto.service;

import java.util.List;
import java.util.stream.Collectors;

import org.modelmapper.ModelMapper;
import org.ros.chatto.dto.ChatMessageDTO;
import org.ros.chatto.dto.MessageCipherDTO;
import org.ros.chatto.dto.ReencryptionDTO;
import org.ros.chatto.model.ChatMessage;
import org.ros.chatto.model.MessageCipher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

//private PostDto convertToDto(Post post) {
//    PostDto postDto = modelMapper.map(post, PostDto.class);
//    postDto.setSubmissionDate(post.getSubmissionDate(), 
//        userService.getCurrentUser().getPreference().getTimezone());
//    return postDto;
//}
//
//	
//private Post convertToEntity(PostDto postDto) throws ParseException {
//    Post post = modelMapper.map(postDto, Post.class);
//    post.setSubmissionDate(postDto.getSubmissionDateConverted(
//      userService.getCurrentUser().getPreference().getTimezone()));
//  
//    if (postDto.getId() != null) {
//        Post oldPost = postService.getPostById(postDto.getId());
//        post.setRedditID(oldPost.getRedditID());
//        post.setSent(oldPost.isSent());
//    }
//    return post;
//}
@Service
public class MyConversionService {
	@Autowired
	ModelMapper modelMapper;

	public ChatMessageDTO convertToChatMessageDTO(
			final ChatMessage chatMessage) {
		final ChatMessageDTO chatMessageDTO = modelMapper.map(chatMessage,
				ChatMessageDTO.class);
		return chatMessageDTO;
	}

	public ChatMessage convertToChatMessage(
			final ChatMessageDTO chatMessageDTO) {
		final ChatMessage chatMessage = modelMapper.map(chatMessageDTO,
				ChatMessage.class);
		return chatMessage;
	}

	public MessageCipher convertToMessageCipher(
			final MessageCipherDTO messageCipherDTO) {
		final MessageCipher messageCipher = modelMapper.map(messageCipherDTO,
				MessageCipher.class);
		return messageCipher;
	}

	public List<ChatMessageDTO> convertToChatMessageDTOs(
			final List<ChatMessage> chatMessages) {
		return chatMessages.stream()
				.map(chatMessage -> convertToChatMessageDTO(chatMessage))
				.collect(Collectors.toList());
	}

	public ReencryptionDTO convertToReencryptionDTO(
			final ChatMessage chatMessage) {
		final ReencryptionDTO reencryptionDTO = modelMapper.map(chatMessage,
				ReencryptionDTO.class);
		return reencryptionDTO;
	}

	public ChatMessage convertToChatMessage(
			final ReencryptionDTO reencryptionDTO) {
		final ChatMessage chatMessage = modelMapper.map(reencryptionDTO,
				ChatMessage.class);
		return chatMessage;
	}

	public List<ReencryptionDTO> convertToReencryptionDTOs(
			final List<ChatMessage> chatMessages) {
		return chatMessages.stream()
				.map(chatMessage -> convertToReencryptionDTO(chatMessage))
				.collect(Collectors.toList());
	}

	public Iterable<ChatMessage> convertoToChatMessages(
			final List<ChatMessageDTO> chatMessageDTOs) {

		return chatMessageDTOs.stream()
				.map(chatMessageDTO -> convertToChatMessage(chatMessageDTO))
				.collect(Collectors.toList());
	}

	public List<ChatMessageDTO> convertToChatMessageDTOs(
			final Page<ChatMessage> chatMessages) {
		return chatMessages.stream()
				.map(chatMessage -> convertToChatMessageDTO(chatMessage))
				.collect(Collectors.toList());
	}

	public <T, O> O convert(T obj, Class<O> clazz) {
		return modelMapper.map(obj, clazz);
	}
}
