package org.codiki.posts;

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

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

import org.codiki.core.entities.dto.PostDTO;
import org.codiki.core.entities.dto.View;
import org.codiki.core.entities.persistence.Post;
import org.codiki.core.entities.persistence.User;
import org.codiki.core.repositories.PostRepository;
import org.codiki.core.services.ParserService;
import org.codiki.core.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;

@RestController
@RequestMapping("/api/posts")
public class PostController {

	private static final int LIMIT_POSTS_HOME = 20;
	
	@Autowired
	private ParserService parserService;
	
	@Autowired
	private PostRepository postRepository;
	
	@Autowired
	private PostService postService;
	
	@Autowired
	private UserService userService;
	
	@GetMapping
	public List<PostDTO> getAll() {
		return StreamSupport.stream(postRepository.findAll().spliterator(), false)
				.map(PostDTO::new).collect(Collectors.toList());
	}

	@JsonView(View.PostDTO.class)
	@GetMapping("/{postKey}")
	public PostDTO getByKey(@PathVariable("postKey") final String pPostKey, 
			final HttpServletResponse response) {
		final PostDTO result = getByKeyAndSource(pPostKey, response);
		if(result != null) {
			result.setText(parserService.parse(result.getText()));
		}
		return result;
	}

	@JsonView(View.PostDTO.class)
	@GetMapping("/{postKey}/source")
	public PostDTO getByKeyAndSource(@PathVariable("postKey")final String pPostKey,
			final HttpServletResponse response) {
		PostDTO result = null;
		
		final Optional<Post> post = postRepository.getByKey(pPostKey);
		if(post.isPresent()) {
			result = new PostDTO(post.get());
		} else {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}
		
		return result;
	}

	@JsonView(View.PostDTO.class)
	@GetMapping("/last")
	public List<Post> getLast() {
		return postRepository.getLast(PageRequest.of(0, LIMIT_POSTS_HOME));
	}

	@JsonView(View.PostDTO.class)
	@GetMapping("/search/{searchCriteria}")
	public List<PostDTO> search(@PathVariable("searchCriteria") final String pSearchCriteria) {
		return postService.search(pSearchCriteria);
	}

	@JsonView(View.PostDTO.class)
	@GetMapping("/byCategory/{categoryId}")
	public List<PostDTO> getByCategory(@PathVariable("categoryId") final Long pCategoryId) {
		return postRepository.getByCategoryId(pCategoryId).stream()
				.map(PostDTO::new).collect(Collectors.toList());
	}

	@JsonView(View.PostDTO.class)
	@GetMapping("/myPosts")
	public List<Post> getMyPosts(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
			final Principal pPrincipal) throws IOException {
		List<Post> result = new LinkedList<>();
		
		final Optional<User> connectedUser = userService.getUserByPrincipal(pPrincipal);
		if(connectedUser.isPresent()) {
//			result = postRepository.getByCreator(connectedUser.get().getId())
//					.stream().map(PostDTO::new).collect(Collectors.toList());
			result = postRepository.getByCreator(connectedUser.get().getId());
		} else {
			pResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
		}
		
		return result;
	}

	@JsonView(View.PostDTO.class)
	@PostMapping("/preview")
	public PostDTO preview(@RequestBody final PostDTO pPost) {
		final PostDTO result = new PostDTO();
		
		result.setTitle(pPost.getTitle());
		result.setImage(pPost.getImage() == null || "".equals(pPost.getImage())
				? "https://news-cdn.softpedia.com/images/news2/this-is-the-default-wallpaper-of-the-gnome-3-20-desktop-environment-500743-2.jpg"
				: pPost.getImage());
		result.setDescription(pPost.getDescription());
		result.setText(parserService.parse(pPost.getText()));
		
		return result;
	}

	@JsonView(View.PostDTO.class)
	@PostMapping("/")
	public PostDTO insert(@RequestBody final PostDTO pPost, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse, final Principal pPrincipal) {
		PostDTO result = null;
		
		Optional<Post> postCreated = postService.insert(pPost, pRequest, pResponse, pPrincipal);
		
		if(postCreated.isPresent()) {
			result = new PostDTO(postCreated.get());
		}
		
		return result;
	}
	
	@PutMapping("/")
	public void update(@RequestBody final PostDTO pPost, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse, final Principal pPrincipal) throws IOException {
		postService.update(pPost, pRequest, pResponse, pPrincipal);
	}
	
	@DeleteMapping("/{postKey}")
	public void delete(@PathVariable("postKey") final String pPostKey, final HttpServletRequest pRequest,
			final HttpServletResponse pResponse, final Principal pPrincipal) throws IOException {
		postService.delete(pPostKey, pRequest, pResponse, pPrincipal);
	}
}
