package org.codiki.images;

import java.io.IOException;
import java.security.Principal;
import java.util.List;

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

import org.codiki.core.entities.dto.ImageDTO;
import org.codiki.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/images")
public class ImageController {
	
	@Autowired
	private ImageService imageService;
	
	@PostMapping("/uploadAvatar")
	public ResponseEntity<String> uploadAvatar(@RequestParam("file") MultipartFile pFile,
			final HttpServletRequest pRequest, final HttpServletResponse pResponse, final Principal pPrincipal) {
		ResponseEntity<String> result;
		try {
			result = ResponseEntity.status(HttpStatus.OK)
					.body(imageService.uploadAvatar(pFile, pRequest, pResponse, pPrincipal));
		} catch(final Exception pEx) {
			result = ResponseEntity.status(HttpStatus.EXPECTATION_FAILED)
					.body(StringUtils.concat("Fail to upload ", pFile.getOriginalFilename() + "."));
		}
		return result;
	}
	
	@PostMapping
	public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile pFile,
			final HttpServletRequest pRequest, final HttpServletResponse pResponse, final Principal pPrincipal) {
		ResponseEntity<String> result;
		try {
			result = ResponseEntity.status(HttpStatus.OK)
					.body(imageService.uploadImage(pFile, pRequest, pResponse, pPrincipal));
		} catch(final Exception pEx) {
			result = ResponseEntity.status(HttpStatus.EXPECTATION_FAILED)
					.body(StringUtils.concat("Fail to upload ", pFile.getOriginalFilename() + "."));
		}
		return result;
	}
	
	@GetMapping("/loadAvatar/{avatarFileName}")
	public ResponseEntity<Resource> loadAvatar(@PathVariable("avatarFileName") final String pAvatarFileName) {
		 return buildLoadImageResponse(imageService.loadAvatar(pAvatarFileName));
	}
	
	@GetMapping("/{imageLink}")
	public ResponseEntity<Resource> loadImage(@PathVariable("imageLink") final String pImageLink) {
		return buildLoadImageResponse(imageService.loadImage(pImageLink));
	}
	
	private ResponseEntity<Resource> buildLoadImageResponse(final Resource pImageFile) {
		return ResponseEntity.ok()
				.header(HttpHeaders.CONTENT_DISPOSITION, StringUtils.concat("attachment; filename=\"", pImageFile.getFilename(), "\""))
				.body(pImageFile);
	}
	
	@GetMapping("/myImages")
	public List<ImageDTO> myImages(final HttpServletRequest pRequest, final HttpServletResponse pResponse,
			final Principal pPrincipal) throws IOException {
		return imageService.getUserImages(pRequest, pResponse, pPrincipal);
	}
	
	@GetMapping("/{imageLink}/details")
	public ImageDTO getImageDetails(@PathVariable("imageLink") final String pImageLink, final HttpServletResponse pResponse) throws IOException {
		return imageService.getImageDetails(pImageLink, pResponse);
	}
}
