package com.eactive.eai.rms.onl.manage.serviceinfo.itsm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eactive.eai.rms.common.interceptor.InterceptorSkipController;
import com.eactive.eai.rms.onl.manage.serviceinfo.itsm.vo.AdapterStatus;
import com.eactive.eai.rms.onl.manage.serviceinfo.itsm.vo.ErrorEvent;
import com.eactive.eai.rms.onl.manage.serviceinfo.itsm.vo.ItsmRows;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class ItsmRestController implements InterceptorSkipController {
	
	@Autowired
	private ItsmOnlineService onlineService;
	
	@Autowired
	private ItsmBatchService batchService;
	
	@Autowired
	private ItsmAdapterStatusService adapterStatusService;
	
	@Autowired
	private ItsmErrorEventService errorEventService;
	
	
	@RequestMapping(value= "/api/v1/itsm/xml/online.do", produces = { "application/xml;charset=utf-8" })
	public @ResponseBody ItsmRows onlineXml() {
		return onlineService.getStatus();
	}
	
	@RequestMapping(value= "/api/v1/itsm/json/online.do", produces = { "application/json;charset=utf-8" })
	public @ResponseBody String onlineJson() throws JsonProcessingException {
		
		ItsmRows online = onlineService.getStatus();
		
		ObjectMapper jacksonMapper = new ObjectMapper();
		jacksonMapper.setSerializationInclusion(Include.NON_NULL);
		jacksonMapper.setSerializationInclusion(Include.NON_EMPTY);
		jacksonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		
		return jacksonMapper.writeValueAsString(online);
	}
	
	
	
	
	@RequestMapping(value= "/api/v1/itsm/xml/batch.do", produces = { "application/xml;charset=utf-8" })
	public @ResponseBody ItsmRows batchXml() {
		return batchService.getStatus();
	}
	
	@RequestMapping(value= "/api/v1/itsm/json/batch.do", produces = { "application/json;charset=utf-8" })
	public @ResponseBody String batchJson() throws JsonProcessingException {
		
		ItsmRows batch = batchService.getStatus();
		
		ObjectMapper jacksonMapper = new ObjectMapper();
		jacksonMapper.setSerializationInclusion(Include.NON_NULL);
		jacksonMapper.setSerializationInclusion(Include.NON_EMPTY);
		jacksonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		
		return jacksonMapper.writeValueAsString(batch);
	}
	
	
	
	
	@RequestMapping(value= "/api/v1/itsm/xml/adapterStatus.do", produces = { "application/xml;charset=utf-8" })
	public @ResponseBody AdapterStatus adapterStatusXml() {
		return adapterStatusService.getStatus();
	}
	
	@RequestMapping(value= "/api/v1/itsm/json/adapterStatus.do", produces = { "application/json;charset=utf-8" })
	public @ResponseBody String adapterStatusJson() throws JsonProcessingException {
		
		AdapterStatus status = adapterStatusService.getStatus();
		
		ObjectMapper jacksonMapper = new ObjectMapper();
		jacksonMapper.setSerializationInclusion(Include.NON_NULL);
		jacksonMapper.setSerializationInclusion(Include.NON_EMPTY);
		jacksonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		
		return jacksonMapper.writeValueAsString(status);
	}
	
	
	
	@RequestMapping(value= "/api/v1/itsm/xml/errorEvent.do", produces = { "application/xml;charset=utf-8" })
	public @ResponseBody ErrorEvent errorEventXml() {
		return errorEventService.getErrorEvent();
	}
	
	@RequestMapping(value= "/api/v1/itsm/json/errorEvent.do", produces = { "application/json;charset=utf-8" })
	public @ResponseBody String errorEventJson() throws JsonProcessingException {
		
		ErrorEvent errorEvent = errorEventService.getErrorEvent();
		
		ObjectMapper jacksonMapper = new ObjectMapper();
		jacksonMapper.setSerializationInclusion(Include.NON_NULL);
		jacksonMapper.setSerializationInclusion(Include.NON_EMPTY);
		jacksonMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		
		return jacksonMapper.writeValueAsString(errorEvent);
	}
	
	
}
