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.filters.RicercaBancaFilterBean;
import common.controller.BaseController;
import dao.ApAbiDao;
import dao.ApCabDao;
import sicura.entities.ApAbi;
import sicura.entities.ApCab;

@Controller
@RequestMapping("abi")
public class ApAbiRestController extends BaseController {

	@Autowired
	ApAbiDao apAbiDao;
	@Autowired
	ApCabDao apCabDao;
		
	
	@RequestMapping("/autocomplete/{filter}")
	@ResponseBody
	public ResponseEntity<Object> autocomplete(@PathVariable("filter") String filter, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			
			List<ApAbi> res = apAbiDao.filterIstituto(filter, 100);
			
			return new ResponseEntity<Object>(res, HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("/filiali")
	@ResponseBody
	public ResponseEntity<Object> filterFiliali(@RequestBody  RicercaBancaFilterBean filterBean, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			
			List<ApCab> res = apCabDao.filterFiliale(filterBean);
			
			return new ResponseEntity<Object>(res, HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
			
	}
	
	
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResponseEntity<Object> read(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			
			ApAbi res = apAbiDao.read(id);
			
			return new ResponseEntity<Object>(res, HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/byAbi/{abi}")
	@ResponseBody
	public ResponseEntity<Object> read(@PathVariable("abi") String abi, HttpServletRequest request) {
		try{
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			
			ApAbi res = apAbiDao.getByAbi(abi);
			
			return new ResponseEntity<Object>(res, HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
			
	}

	@RequestMapping(value="/saveBanca", method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Object> saveBanca(@RequestBody ApAbi banca, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				String res = apAbiDao.saveBanca(banca, appVariables);
				
				return new ResponseEntity<Object>(res, HttpStatus.OK);		
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	
	@RequestMapping(value="/saveFiliale", method=RequestMethod.POST)
	@ResponseBody
	private ResponseEntity<Object> saveFiliale(@RequestBody ApCab filiale, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				String res = apCabDao.saveFiliale(filiale, appVariables);
				
				return new ResponseEntity<Object>(res, HttpStatus.OK);	
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
			
	}
	
	@RequestMapping("/eliminaFiliale/{idFiliale}")
	@ResponseBody
	public ResponseEntity<Object> eliminaFiliale(@PathVariable("idFiliale") Integer idFiliale, HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			else {
				AppVariables appVariables = (AppVariables) getSessionAttribute("appVariables");
				String res = apCabDao.deleteFiliale(idFiliale, appVariables);
			
				return new ResponseEntity<Object>(res, HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("/nuovaBanca")
	@ResponseBody
	public ResponseEntity<Object> nuovaBanca(HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			else {
				return new ResponseEntity<Object>(new ApAbi(), HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
	@RequestMapping("/nuovaFiliale")
	@ResponseBody
	public ResponseEntity<Object> nuovaFiliale(HttpServletRequest request) {
		try {
			setRequest(request);
			if (getUserData()==null)
				return new ResponseEntity<Object>(null, HttpStatus.UNAUTHORIZED);
			else {
				return new ResponseEntity<Object>(new ApCab(), HttpStatus.OK);
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<Object>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
}
