package controller.rest;

import java.util.List;

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 bean.AppVariables;
import bean.filters.DisposizioniFiltroBean;
import common.controller.BaseController;
import dao.TipologiaDao;
import sicura.entities.Tipologia;

@Controller
@RequestMapping("tipologie")
public class TipologiaRestController extends BaseController {

	@Autowired
	TipologiaDao tipologiaDAO;
	
	@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> (tipologiaDAO.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);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<Integer>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Integer>(tipologiaDAO.count(), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}	
	
	@RequestMapping(value="", method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Object> save(@RequestBody Tipologia tipologia,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>(tipologiaDAO.save(tipologia, 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 non previsto.  L'errore è stato registrato nei log di Sicura", HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/new")
	@ResponseBody
	private ResponseEntity<Object> newInstance(HttpServletRequest request) {
		try{
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new  ResponseEntity<Object>(false,HttpStatus.UNAUTHORIZED);
			else {
				Tipologia tipologia = new Tipologia();
				tipologia.setId(0);
				return new  ResponseEntity<Object>(tipologia,HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
			
	
}

