package com.javahonk.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {
    @RequestMapping(value = "/helloWorld.web", 
	    method = RequestMethod.GET)
    public String printWelcome(ModelMap model, HttpServletRequest
	    request, HttpServletResponse response) {

	model.addAttribute("message", "Spring MVC Multi File upload");
	return "helloWorld";

    }

    @RequestMapping(value = "/multiFileUpload.web", 
	    method = RequestMethod.POST)
    public String save(@ModelAttribute("multiFileUpload") 
    MultiFileUpload multiFileUpload,BindingResult bindingResult, Model model) throws IOException {

	List<MultipartFile> files = multiFileUpload
		.getMultiUploadedFileList();
	StringBuffer uploadedfile = 
		new StringBuffer("Below files uploaded successfully");
	
	if (files.isEmpty() ) {
	    bindingResult.reject("noFile", "Please select file to upload");
	    return "helloWorld";
	}
	
	if (null != files && files.size() > 0) {
	    
		
	    for (MultipartFile multipartFile : files) {
		
		ByteArrayOutputStream baos = 
			new ByteArrayOutputStream();
		FileOutputStream output = null;
		File outputfile = null;		
		String fileName = multipartFile.getOriginalFilename();
		uploadedfile.append("<h4>"+fileName+"</h4>");
		String extensionOfFileName = fileName.
			substring(fileName.indexOf(".")+1,
				fileName.length());
		outputfile = new File("C:\\Image\\" + fileName);
		InputStream inputStream = multipartFile
			.getInputStream();
		
		if (null!=extensionOfFileName && 
			extensionOfFileName.equalsIgnoreCase("pdf")) {
		    if (!outputfile.isDirectory()) {
			try {
			    output = new FileOutputStream(outputfile);
			    IOUtils.copy(inputStream, output);
			} catch (Exception e) {
			    e.printStackTrace();
			} finally {
			    baos.close();
			    output.close();

			}
		    }
		} else if (null != extensionOfFileName 
			&& extensionOfFileName
			.equalsIgnoreCase("png")) {
		    BufferedImage imBuff = ImageIO.read(multipartFile
			    .getInputStream());
		    ImageIO.write(imBuff, extensionOfFileName,
			    outputfile);
		} else if (null != extensionOfFileName 
			&& extensionOfFileName
			.equalsIgnoreCase("jpg")) {
		    BufferedImage imBuff = ImageIO
			    .read(multipartFile.getInputStream());
		    ImageIO.write(imBuff, extensionOfFileName,
			    outputfile);
		}else {
		    System.out.println("Unknown file extension"
		+extensionOfFileName);
		}

	    }
	}

	model.addAttribute("message", uploadedfile);
	return "Upload";
    }

}