package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.LisOrderSysmex;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PelayananLaboratoriumService;
import com.jasamedika.medifirst2000.vo.AntrianPasienRegistrasiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Controller
 * 
 */
@RestController
@RequestMapping("/lab")
public class LaboratoriumController extends LocaleController<AntrianPasienRegistrasiVO> {

	@Autowired
	private PelayananLaboratoriumService pelayananLaboratoriumService;

	@RequestMapping(value = "/get-result-detail-laboratorium/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getResultLaboratorium(
			@RequestParam(value = "noOrder", required = false) String noOrder, HttpServletRequest request) {
		try {
			List<Map<String, Object>> result = pelayananLaboratoriumService.getResultOnly(noOrder);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-result-detail-laboratorium-history/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getResultLaboratoriumHistory(
			@RequestParam(value = "noOrder", required = false) String noOrder, HttpServletRequest request) {
		try {
			Map<String, Object> result = pelayananLaboratoriumService.getResultWithHistory(noOrder);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/order-sysmex", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<LisOrderSysmex> OrderSysmex(@RequestParam(value = "noOrder", required = false) String noOrder,
			HttpServletRequest request) {
		try {
			LisOrderSysmex result = pelayananLaboratoriumService.SaveOrderToLisOrder(noOrder);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-hd-lab", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getHd(@RequestParam(value = "noOrder", required = false) String noOrder,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pelayananLaboratoriumService.cekDataResDt(noOrder);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (Exception e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

}
