package controller.rest;

import java.sql.SQLException;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import common.controller.BaseController;
import dao.LogSicuraSchedulerDAO;
import sicura.entities.LogSicuraScheduler;

@Controller
@RequestMapping("logSicura")
public class LogSicuraRestController extends BaseController {

	@Autowired
	LogSicuraSchedulerDAO logSicuraSchedulerDAO;
	
	@RequestMapping(value="/list/{start:[\\d]+}-{length:[\\d]+}")
	@ResponseBody
	private ResponseEntity<List<LogSicuraScheduler>> list(HttpServletRequest request, 
				@PathVariable("start") int startFrom, 
				@PathVariable("length") int length,
				@RequestParam(value="inputFile", required=false) String inputFile,
				@RequestParam(value="orderBy", required=false) String orderBy
				) {
		try {
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<List<LogSicuraScheduler>>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<List<LogSicuraScheduler>>(logSicuraSchedulerDAO.list(startFrom,length,inputFile, 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="inputFile", required=false) String inputFile) {
		try {
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<Integer>(HttpStatus.UNAUTHORIZED);
			else
				return new ResponseEntity<Integer>(logSicuraSchedulerDAO.count(inputFile), HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(0, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	@RequestMapping("details/{id}")
	@ResponseBody
	private ResponseEntity<Object> details(@PathVariable("id") Integer id,HttpServletRequest request) {
		try {
			setRequest(request);
			if(getUserData()==null)
				return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
			
			LogSicuraScheduler log = logSicuraSchedulerDAO.read(id);
			String res="";
			try {
				if (log.getLogServizio()!=null && log.getLogServizio().length()>0 )
					res = log.getLogServizio().getSubString(1, (int) log.getLogServizio().length()-1).replace("\r\n","<br>").replace("\n\n","<br>");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				res = "";
			} 
					
			return new ResponseEntity<Object>(res, HttpStatus.OK);
		} catch(Exception ex) {
			ex.printStackTrace();
			Logger.getRootLogger().error(ex);
			return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
	
}
