package com.jasamedika.medifirst2000.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.entities.PgStatActivity;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PgStatActivityService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.LoginUserVO;

@RestController
@RequestMapping("/integrasi")
public class IntegrationController extends LocaleController<LoginUserVO> {

	private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationController.class);

	@Autowired
	private PgStatActivityService pgStatActivityService;

	@RequestMapping(value = "/pg-stat-activity/in-state-for-minutes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PgStatActivity>> getPgStatActivityInStateForMinutes(HttpServletRequest request) {
		try {
			List<PgStatActivity> result = pgStatActivityService.findByStateAndInterval();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get pg_stat_activity in state for minutes", e.getMessage());
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, e.getMessage());
			return RestUtil.getJsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get pg_stat_activity in state for minutes", jse.getMessage());
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, jse.getMessage());
			return RestUtil.getJsonResponse(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/pg-stat-activity/cancel-backend", method = RequestMethod.POST)
	public ResponseEntity<Boolean> callPgCancelBackend(HttpServletRequest request,
			@RequestParam(value = "pid", required = true) Integer pid) {
		try {
			Boolean result = pgStatActivityService.callCancelBackend(pid);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when call pg_cancel_backend pid : {}", sve.getMessage(), pid);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, sve.getMessage());
			return RestUtil.getJsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when call pg_cancel_backend pid : {}", jse.getMessage(), pid);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, jse.getMessage());
			return RestUtil.getJsonResponse(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/pg-stat-activity/terminate-backend", method = RequestMethod.POST)
	public ResponseEntity<Boolean> callPgTerminateBackend(HttpServletRequest request,
			@RequestParam(value = "pid", required = true) Integer pid) {
		try {
			Boolean result = pgStatActivityService.callTerminateBackend(pid);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when call pg_terminate_backend pid : {}", sve.getMessage(), pid);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, sve.getMessage());
			return RestUtil.getJsonResponse(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when call pg_terminate_backend pid : {}", jse.getMessage(), pid);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, jse.getMessage());
			return RestUtil.getJsonResponse(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

}
