package controller.rest;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
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.ResponseBody;

import bean.AppVariables;
import bean.query.TabellaFirmeBean;
import bean.query.TabellaFlussiNonSedaBean;
import common.controller.BaseController;
import dao.DistintaFlussiInputNotSedaDAO;
import dao.FirmaDao;
import sicura.entities.DistintaFlussiInputRequiresFirmatario;

@Controller
@RequestMapping("flussiNonSeda")
public class FlussiNonSedaRestController extends BaseController {

	@Autowired
	DistintaFlussiInputNotSedaDAO flussiInputNotSedaDAO;
	
	@RequestMapping(value="/list")
	@ResponseBody
	private ResponseEntity<Object> list(HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new  ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else {
				return new ResponseEntity<Object> (flussiInputNotSedaDAO.list(), HttpStatus.OK);
			}	
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/count")
	@ResponseBody
	private ResponseEntity<Integer> count(HttpServletRequest request) {
		try {
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<Integer>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Integer>(flussiInputNotSedaDAO.count(), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(0, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}	
	
	@RequestMapping(value="", method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Object> save(@RequestBody TabellaFlussiNonSedaBean tabella,HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new  ResponseEntity<Object>(false,HttpStatus.UNAUTHORIZED);
			else {
				try {
					AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");	
					return new  ResponseEntity<Object>(flussiInputNotSedaDAO.save(tabella, appVariables),HttpStatus.OK);
				} catch (Exception ex) {
					return new  ResponseEntity<Object>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
				}
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>("Si è verificato un errore imprevisto.  L'errore è stato registrato nei log di Sicura", HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	@ResponseBody
	private ResponseEntity<Object> delete(@PathVariable("id") Integer id,HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new  ResponseEntity<Object>(false,HttpStatus.UNAUTHORIZED);
			else {
				try {
					AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");	
					flussiInputNotSedaDAO.delete(id, appVariables);
					return new  ResponseEntity<Object>(HttpStatus.OK);
				} catch (Exception ex) {
					return new  ResponseEntity<Object>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
				}
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/new", method=RequestMethod.GET)
	@ResponseBody
	private ResponseEntity<Object> newInstance(HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new  ResponseEntity<Object>(false,HttpStatus.UNAUTHORIZED);
			else {
				TabellaFlussiNonSedaBean tbl = new TabellaFlussiNonSedaBean();
				tbl.setId(0);
				return new  ResponseEntity<Object>(tbl, HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	
}

