package controller.rest;

import java.util.ArrayList;
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.FunzioniOrdinantiDAO;
import sicura.entities.FunzioniOrdinanti;

@Controller
@RequestMapping("funzioniOrdinanti")
public class FunzioniOrdinantiRestController extends BaseController {

	@Autowired
	FunzioniOrdinantiDAO funzioniOrdinantiDAO;
	
	@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> (funzioniOrdinantiDAO.listAll(), HttpStatus.OK);
			}	
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping(value="/list/{start:[\\d]+}-{length:[\\d]+}")
	@ResponseBody
	private ResponseEntity<List<FunzioniOrdinanti>> list(HttpServletRequest request, 
				@PathVariable("start") int startFrom, 
				@PathVariable("length") int length,
				@RequestParam(value="filter",required=false) String filter,
				@RequestParam(value="orderBy",required=false) String orderBy
				) {
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<List<FunzioniOrdinanti>>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<List<FunzioniOrdinanti>>(funzioniOrdinantiDAO.list(startFrom,length,filter, orderBy), 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, @RequestParam(value="filter", required=false) String filter) {
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if(getUserData()==null)
				return new ResponseEntity<Integer>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Integer>(funzioniOrdinantiDAO.count(filter), 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 FunzioniOrdinanti funzione,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>(funzioniOrdinantiDAO.save(funzione, 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<>(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 {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");	
				funzioniOrdinantiDAO.delete(id, appVariables);
				return new  ResponseEntity<Object>(true,HttpStatus.OK);
			}
		} 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(Integer id,HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new  ResponseEntity<Object>(false,HttpStatus.UNAUTHORIZED);
			else {
				FunzioniOrdinanti res = new FunzioniOrdinanti();
				res.setId(0);
				return new  ResponseEntity<Object>(res,HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}


	@RequestMapping(value="/utente")
	@ResponseBody
	private ResponseEntity<Object> listForUser(HttpServletRequest request) {
		try{
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if (getUserData()==null)
				return new  ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Object>(funzioniOrdinantiDAO.listForUser(appVariables.getUserId(), appVariables.getGuid()), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
			
	}
	

	@RequestMapping(value="/all")
	@ResponseBody
	private ResponseEntity<Object> listAllForUser(HttpServletRequest request) {
		try {
			setRequest(request);
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			if (getUserData()==null)
				return new  ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else {
				//List<FunzioniOrdinanti> res = funzioniOrdinantiDAO.listForUser(appVariables.getUserId(), appVariables.getGuid());
				List<FunzioniOrdinanti> res = new ArrayList<FunzioniOrdinanti>();
				for (FunzioniOrdinanti item: funzioniOrdinantiDAO.listAll()){
					res.add(item);
				}
				return new ResponseEntity<Object>(res, HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}

