package com.ejie.ab63.control.leerExcel;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ejie.ab63.model.util.ErrorResultAjaxOperation;
import com.ejie.ab63.model.util.ResultAjaxOperation;
import com.ejie.ab63.service.leerExcel.LeerExcelService;
import com.ejie.ab63.tramita.adapter.PifAdapterService;

/**
 * LeerExcelController
 * 
 */

@Controller
@RequestMapping(value = "/leerExcel")
public class LeerExcelController {

	@Autowired
	private LeerExcelService leerExcelService;
	@Autowired
	private PifAdapterService pifAdapterService;

	@RequestMapping(value = "/form", method = RequestMethod.POST)
	public @ResponseBody ResultAjaxOperation addForm(
			@RequestParam(value = "tipoEstablecimientoUpload", required = false) String tipoEstablecimiento,
			@RequestParam(value = "idEstablecimientoUpload", required = true) String idEstablecimiento,
			@RequestParam(value = "file", required = false) MultipartFile file)
			throws NumberFormatException, Exception {

		InputStream inputStream = new ByteArrayInputStream(file.getBytes());
		String error = this.leerExcelService.masivoExcel(inputStream, Integer.valueOf(idEstablecimiento),
				tipoEstablecimiento);
		if (null != error) {
			return new ErrorResultAjaxOperation("", error);
		} else {
			return null;
		}
	}

	@RequestMapping(value = "/descargar", method = RequestMethod.POST)
	public @ResponseBody String descargar(HttpServletResponse httpResponse,
			@RequestParam(value = "tipoEstablecimientoDownload", required = false) String tipoEstablecimiento,
			@RequestParam(value = "idEstablecimientoDownload", required = true) String idEstablecimiento,
			@RequestParam(value = "isPlantilla", required = true) int isPlantilla) throws Exception {

		String rutaPIF = this.leerExcelService.recuperarExcel(Integer.valueOf(idEstablecimiento), tipoEstablecimiento,
				isPlantilla);
		BufferedInputStream contenidoXml = this.pifAdapterService.getFile(rutaPIF);
		int numRead;
		final int bufferSize = 1024;
		byte[] buffer = new byte[bufferSize];
		ByteArrayOutputStream outString = new ByteArrayOutputStream();
		try {
			while ((numRead = contenidoXml.read(buffer)) != -1) {
				outString.write(buffer, 0, numRead);
			}
		} finally {
			contenidoXml.close();
		}
		byte[] strbase64Doc1 = outString.toByteArray();

		String nombreDocumento = rutaPIF.substring(rutaPIF.indexOf("/"));
		httpResponse.setHeader("Content-Disposition", "attachment; filename=\"" + nombreDocumento + ".xlsx\"");
		Cookie cookie = new Cookie("fileDownload", "true");
		cookie.setMaxAge(2);
		cookie.setPath("/");
		httpResponse.addCookie(cookie);
		httpResponse.setContentType("application/vnd.ms-excel");

		httpResponse.setContentLength(strbase64Doc1.length);
		httpResponse.getOutputStream().write(strbase64Doc1);

		httpResponse.getOutputStream().flush();
		httpResponse.getOutputStream().close();
		return null;
	}

}