package controller.rest;

import java.util.List;

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

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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 bean.AppVariables;
import bean.FileBean;
import bean.filters.DisposizioniFiltroBean;
import common.controller.BaseController;
import dao.ServiceUIDAO;
import sicura.entities.DisposizioniAea;
import sicura.entities.ServiceUI;

@Controller
@RequestMapping("reportPagamenti")
public class ReportPagamentiRestController extends BaseController {

	@Autowired
	ServiceUIDAO serviceUIDao;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	private ResponseEntity<List<ServiceUI>> list(				
			@RequestParam("start") int startFrom, 
			@RequestParam("size") int length,
			HttpServletRequest request
	){
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<List<ServiceUI>>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<List<ServiceUI>>(serviceUIDao.list(getUserData(), appVariables),HttpStatus.OK);
			
		}catch(Exception ex) {
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			ex.printStackTrace();
			Logger.getRootLogger().error((appVariables!=null? appVariables.getUserId(): "utente sconosciuto: ") + ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/count")
	@ResponseBody
	private ResponseEntity<Integer> count(HttpServletRequest request) {
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<Integer>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Integer>(serviceUIDao.count(getUserData(), appVariables), HttpStatus.OK);
		} catch(Exception ex) {
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			ex.printStackTrace();
			Logger.getRootLogger().error((appVariables!=null? appVariables.getUserId(): "utente sconosciuto: ") + ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}	
	}
	
	@RequestMapping(value="uploadFile",  method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Boolean> uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
	
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<Boolean>(false,HttpStatus.UNAUTHORIZED);
		else {
			byte[] bytes = file.getBytes();
			
			boolean res = serviceUIDao.addFile(getUserData(), appVariables, bytes, file.getOriginalFilename());
			
			
			
			return new ResponseEntity<>(res, HttpStatus.OK);
			}
		} catch (Exception ex) {
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			ex.printStackTrace();
			Logger.getRootLogger().error((appVariables!=null? appVariables.getUserId(): "utente sconosciuto: ") + ex);
			return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
		}
		
	}
	
	@RequestMapping(value="downloadFile/{idServiceUI}",  method=RequestMethod.GET)
	private void download(@PathVariable("idServiceUI") Integer idServiceUI, HttpServletRequest request,HttpServletResponse response) throws Exception {
		setRequest(request);
		if(getUserData()==null)
			response.sendError(401); // UNAUTHORIZED
		else {
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
	
			FileBean res = serviceUIDao.getFile(idServiceUI);
			
			if (res!=null) {
				
				ServletOutputStream out = response.getOutputStream();
				
				out.write(res.getBytes());
				response.setContentType("text/plain");
				response.setHeader("Content-Disposition","attachment;filename="+ res.getFilename());
				
			    out.flush();
			    out.close();
			} else {
				response.setStatus(404); // not found
			}
		}
	}
}
