package com.jasamedika.medifirst2000.controller;

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

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.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.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ReturObatService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;

@RestController
@RequestMapping("/retur-obat")
public class ReturObatController extends LocaleController{

	@Autowired
	private ReturObatService service;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ReturObatController.class);
	
	@RequestMapping(value = "/find-retur-by-struk")
	@ResponseBody
	public List<Map<String, Object>> findReturStruk(
			@RequestParam(value = "ruanganId", required = true) Integer ruanganId,
			@RequestParam(value = "noResep", required = true) String noResep,
			@RequestParam(value = "noCm", required = true) String noCm,
			@RequestParam(value = "tglAwal", required = true) String tglAwal,
			@RequestParam(value = "tglAhir", required = true) String tglAhir) {
		return service.getReturStrukResep(ruanganId, noResep, noCm, tglAwal, tglAhir);
	}
	
	@RequestMapping(value = "/get-ruangan-instalasi-farmasi")
	@ResponseBody
	public List<Map<String, Object>> getRuanganInstalasiFarmasi() {
		return service.getRuanganFarmasi();
	}
	
	@RequestMapping(value = "/get-detail-retur", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDetailRetur(
			@RequestParam(value = "noRec", required = true) String noRec,
			@RequestParam(value = "noStruk", required = true) String noStruk,
			@RequestParam(value = "noSbmLast", required = true) String noSbmLast,
			HttpServletRequest request) {
		try {
			Map<String,Object> result = service.detailReturResep(noRec,noStruk,noSbmLast);
			if (CommonUtil.isNotNullOrEmpty(result)){
				if(result.get("listData").toString().equalsIgnoreCase("Tidak ada barang yang dapat diretur")){
					Map<String, String> headerMessageCustom = new HashMap<>();
					headerMessageCustom.put("label-success", "Tidak ada barang yang dapat diretur");
					return RestUtil.getJsonResponse(null, HttpStatus.CREATED, headerMessageCustom);
				} else{
					mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request));
					return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
				}
			} else{
				Map<String, String> headerMessageCustom = new HashMap<>();
				headerMessageCustom.put("label-success", "Tidak bisa di retur");
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, headerMessageCustom);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getDetailRetur", 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 getDetailRetur", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
}
