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

import bean.AppVariables;
import bean.PianoEsecuzioneOpzioniBean;
import common.controller.BaseController;
import dao.PianiEstrazioneTipoFlussoDao;
import sicura.entities.PianiEstrazioneDistinteBancaTipoFlusso;

@Controller
@RequestMapping("tipoFlusso")
public class PianiEstrazioneTipoFlussoRestController extends BaseController {
	
	@Autowired
	PianiEstrazioneTipoFlussoDao pianiEstrazioneTipoFlussoDao;
	
	@RequestMapping("/list/{idPiano}")
	@ResponseBody
	public ResponseEntity<Object> list(@PathVariable("idPiano") Integer idPiano, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			
			List<PianiEstrazioneDistinteBancaTipoFlusso> res = pianiEstrazioneTipoFlussoDao.listFor(idPiano);
			
			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("/new/{idPiano}")
	@ResponseBody
	public ResponseEntity<Object> newInstance(@PathVariable("idPiano") Integer idPiano, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			
			PianiEstrazioneDistinteBancaTipoFlusso res = pianiEstrazioneTipoFlussoDao.newInstance(idPiano);
			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="/{idPiano}",method=RequestMethod.DELETE)
	@ResponseBody
	public ResponseEntity<Object> delete(@PathVariable("idPiano") Integer idPiano, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			
			AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
			return new ResponseEntity<Object>(pianiEstrazioneTipoFlussoDao.delete(idPiano, appVariables), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}			
	}

	
	@RequestMapping(value="/save/{idPiano}",method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<Boolean> setStatus(@PathVariable("idPiano") Integer idPiano, @RequestBody PianoEsecuzioneOpzioniBean pianoEsecuzioneOpzioniBean,
				HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Boolean>(false,HttpStatus.UNAUTHORIZED);
			else {
				
				List<PianiEstrazioneDistinteBancaTipoFlusso> pianiEstrazioneDistinteBancaTipoFlusso = pianoEsecuzioneOpzioniBean.getOpzioni();
				List<Integer> idCancellati = pianoEsecuzioneOpzioniBean.getIdCancellati();
				
				Boolean res=false;
				try {
					AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
					res = pianiEstrazioneTipoFlussoDao.save(idPiano, pianiEstrazioneDistinteBancaTipoFlusso, idCancellati, appVariables);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return new ResponseEntity<Boolean>(res, HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
		}		
	}

}
