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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bean.AppVariables;
import common.controller.BaseController;
import dao.VdelegatoFirmaEstrattoreDistinteDao;
import sicura.entities.VdelegatoFirmaEstrattoreDistinte;
import sicura.entities.VdelegatoFirmaEstrattoreDistinteId;

@Controller
@RequestMapping("delegato")
public class VdelegatoFirmaEstrattoreDistinteRestController extends BaseController {
	
	@Autowired
	VdelegatoFirmaEstrattoreDistinteDao vdelegatoDAO;
	
	@RequestMapping("/list")
	@ResponseBody
	public ResponseEntity<Object> list(HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			
			List<VdelegatoFirmaEstrattoreDistinteId> res = vdelegatoDAO.list();
			
			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("/setStatus/{id}/{status}")
	@ResponseBody
	public ResponseEntity<Object> setStatus(@PathVariable("id") Integer id, @PathVariable("status") Boolean status, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				Integer res = vdelegatoDAO.setActive(id, status, appVariables);
				return new ResponseEntity<Object>(res, HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}		
		
	}

}
