package controller.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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 common.controller.BaseController;
import dao.ContiDestinatariDao;
import dao.OrdinantiDao;
import sicura.entities.ContiDestinatari;

@Controller
@RequestMapping("contiDestinatario")
public class ContiDestinatarioRestController extends BaseController {

	@Autowired
	ContiDestinatariDao contiDestinatariDao;
		
	@RequestMapping(value="/list/{idDestinatario}/{start:[\\d]+}-{length:[\\d]+}")
	@ResponseBody
	private ResponseEntity<List<ContiDestinatari>> list(@PathVariable("idDestinatario") Integer idDestinatario, HttpServletRequest request,
			@PathVariable("start") int startFrom, 
			@PathVariable("length") int length,
			@RequestParam(value="filter",required=false) String filter,
			@RequestParam(value="orderBy",required=false) String orderField) {
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<List<ContiDestinatari>>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<List<ContiDestinatari>>(contiDestinatariDao.listForDestinatario(idDestinatario, startFrom, length, orderField), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/count/{idDestinatario}")
	@ResponseBody
	private ResponseEntity<Integer> count(@PathVariable("idDestinatario") Integer idDestinatario, HttpServletRequest request) {
		try{
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<Integer>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Integer>(contiDestinatariDao.countForDestinatario(idDestinatario), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}	
	
	@RequestMapping(value="/{idContoDestinatario}", method=RequestMethod.DELETE)
	@ResponseBody
	private ResponseEntity<Object> deleteContoDestinatario(@PathVariable("idContoDestinatario") Integer idContoDestinatario, HttpServletRequest request) {
		try{
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else  {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");	
				contiDestinatariDao.delete(idContoDestinatario, appVariables);
				return new ResponseEntity<Object>(HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}	
	
	@RequestMapping(value="", method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Object> count(@RequestBody ContiDestinatari conto, HttpServletRequest request) {
		try{
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else {
				try {
					AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");	
					Integer res = contiDestinatariDao.save(conto, appVariables);
					return new ResponseEntity<Object>(res, 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<Object>(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>(HttpStatus.UNAUTHORIZED);
			else {
				try {
					ContiDestinatari conto = new ContiDestinatari();
					conto.setId(0);
					return new ResponseEntity<Object>(conto, 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<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}
