package com.jasamedika.medifirst2000.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
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.RequestBody;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.dao.OrderPemakaianRuangRapatService;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.OrderPemakaianRuangRapatVO;

import net.kaczmarzyk.spring.data.jpa.domain.GreaterThanOrEqual;
import net.kaczmarzyk.spring.data.jpa.domain.LessThanOrEqual;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;

@RestController
@RequestMapping("/order-pemakaian-ruang-rapat")
public class OrderPemakaianRuangRapatController extends LocaleController<OrderPemakaianRuangRapatVO> {

	@Autowired
	private OrderPemakaianRuangRapatService orderPemakaianRuangRapatService;

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

	@RequestMapping(value = "/save-order-pemakaian-ruang-rapat/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveOrderPemakaianRuangRapat(
			@Valid @RequestBody OrderPemakaianRuangRapatVO vo, HttpServletRequest request) {
		try {
			Map<String, Object> result = orderPemakaianRuangRapatService.saveOrderPemakaianRuangRapat(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when save Order Pemakaian Ruang Rapat", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when save Order Pemakaian Ruang Rapat", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/find-by-no-order/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findByNoOrder(
			@RequestParam(value = "noOrder", required = false) String noOrder, HttpServletRequest request) {
		Map<String, Object> result = orderPemakaianRuangRapatService.findByNoOrder(noOrder);
		Boolean dataFound = new Boolean((boolean) result.get("dataFound"));
		if (dataFound) {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,
					getMessage(MessageResource.LABEL_ERROR, request));
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/find-ruang-rapat/", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> findRuangRapat(
			@RequestParam(value = "namaRuangan", required = true) String namaRuangan, HttpServletRequest request) {
		try {
			Map<String, Object> listRuangRapatVO = orderPemakaianRuangRapatService.findRuangRapat(namaRuangan);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(listRuangRapatVO, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when Find Ruang Rapat", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when Find Ruang Rapat", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@RequestMapping(value = "/find-status-ruang-rapat/", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> findStatusRuangRapat(
			@RequestParam(value = "periodeAwal", required = true) Long periodeAwal,
			@RequestParam(value = "periodeAkhir", required = true) Long periodeAkhir,
			@RequestParam(value = "idRuangan", required = true) Integer idRuangan, HttpServletRequest request) {
		try {
			Map<String, Object> listRuangRapatVO = orderPemakaianRuangRapatService.findStatusRuangRapat(periodeAwal,
					periodeAkhir, idRuangan);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(listRuangRapatVO, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when Find Ruang Rapat", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when Find Ruang Rapat", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/find-status-ruangan-rapat/")
	@ResponseBody
	public ResponseEntity<List<Object>> findStatusRuanganRapat(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@Spec(path = "tglPelayananAwal", params = "periodeAwal", config = "yyyy-MM-dd HH:mm:ss", spec = GreaterThanOrEqual.class) Specification<StrukOrder> spec1,
			@Spec(path = "tglPelayananAkhir", params = "periodeAkhir", config = "yyyy-MM-dd HH:mm:ss", spec = LessThanOrEqual.class) Specification<StrukOrder> spec2) {

		Specification<StrukOrder> spec = Specifications.where(spec1).and(spec2);

		Map<String, Object> resultPageMap = orderPemakaianRuangRapatService.ruangRapatPaging(page, limit, sort, dir,
				spec);

		return constructListPageResult(resultPageMap);
	}

}
