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.ResponseBody;

import bean.AppVariables;
import bean.BeanPostPianoEstrazione;
import common.controller.BaseController;
import dao.IstruzioniEstrazioneDistinteBancaDAO;
import dao.PianiEstrazioneDistinteBancaDAO;
import sicura.entities.IstruzioniEstrazioneDistinteBanca;
import sicura.entities.PianiEstrazioneDistinteBanca;

@Controller
@RequestMapping("/pianiEstrazione")
public class PianiEstrazioneRestController extends BaseController {

	@Autowired
	PianiEstrazioneDistinteBancaDAO pianiEstrazioneDAO;
	@Autowired
	IstruzioniEstrazioneDistinteBancaDAO istruzioniEstrazioneDistinteBancaDAO;
	
	@RequestMapping("/list")
	@ResponseBody
	private ResponseEntity<Object> list(HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				return new ResponseEntity<Object>(pianiEstrazioneDAO.list(),HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("/{id}")
	@ResponseBody
	private ResponseEntity<Object> get(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				return new ResponseEntity<Object>(pianiEstrazioneDAO.read(id),HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/new")
	@ResponseBody
	private ResponseEntity<Object> newInstance(HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				PianiEstrazioneDistinteBanca piano = new PianiEstrazioneDistinteBanca();
				piano.setId(0);
				return new ResponseEntity<Object>(piano,HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
			
	}
	
	@RequestMapping("/exec/{id}")
	@ResponseBody
	private ResponseEntity<Object> execute(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				return new ResponseEntity<Object>(pianiEstrazioneDAO.execute(id, appVariables),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 BeanPostPianoEstrazione pianoEstrazione, HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				boolean tx = pianiEstrazioneDAO.startTransaction();			
				
				try {
					Integer id = pianiEstrazioneDAO.saveNoTransaction(pianoEstrazione.getPianoEstrazione(), appVariables);
	
					if (id!=null) {
						istruzioniEstrazioneDistinteBancaDAO.zapIstruzioni(id, pianoEstrazione.getIstruzioniCancellate(),appVariables);
	
						for (IstruzioniEstrazioneDistinteBanca istruzione: pianoEstrazione.getIstruzioni())
							istruzioniEstrazioneDistinteBancaDAO.saveNoTransaction(istruzione, appVariables);
	
						if (tx)
							pianiEstrazioneDAO.commit();
					
						return new ResponseEntity<Object>(id,HttpStatus.OK);
					} else {
						return new ResponseEntity<Object>("Salvataggio piano fallito", HttpStatus.INTERNAL_SERVER_ERROR);
					}
				} catch (Exception ex) {
					if (tx)
						pianiEstrazioneDAO.rollback();
					return new ResponseEntity<Object>(ex.getLocalizedMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
				}
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>("Si è verificato un errore imprevisto.  L'errore è stato registrato nei log di Sicura", 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(HttpStatus.FORBIDDEN);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				return new ResponseEntity<Object>(pianiEstrazioneDAO.delete(id, appVariables),HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("/istruzioni/{id}")
	@ResponseBody
	private ResponseEntity<Object> getIstruzioni(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				return new ResponseEntity<Object>(pianiEstrazioneDAO.getIstruzioni(id),HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
			
	}
	
	
	@RequestMapping("/istruzioni/new/{id}")
	@ResponseBody
	private ResponseEntity<Object> newIstruzione(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			setRequest(request);
			
			if (getUserData()==null)
				return new ResponseEntity(HttpStatus.FORBIDDEN);
			else {
				return new ResponseEntity<Object>(pianiEstrazioneDAO.getNewIstruzione(id),HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}

