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 common.controller.BaseController;
import dao.FirmaDao;
import sicura.entities.DistintaFlussiInputRequiresFirmatario;

@Controller
@RequestMapping("firme")
public class FirmaRestController extends BaseController {

	@Autowired
	FirmaDao firmaDAO;
	
	@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> (firmaDAO.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>(firmaDAO.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 TabellaFirmeBean firma,HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new  ResponseEntity<Object>(false,HttpStatus.UNAUTHORIZED);
			else {
				try {
					AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
					String res = firmaDAO.save(firma, appVariables);
					try {
						Integer.parseInt(res);
						return new  ResponseEntity<Object>(res,HttpStatus.OK);
					} catch (Exception e) {
						return new  ResponseEntity<Object>("Si è verificato un errore durante il salvataggio: " +res, HttpStatus.INTERNAL_SERVER_ERROR);
					}
				} 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="/{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");	
					firmaDAO.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 {
				TabellaFirmeBean firma = new TabellaFirmeBean();
				firma.setId(0);
				return new  ResponseEntity<Object>(firma, HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	
}

