package com.jasamedika.medifirst2000.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.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.RestController;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
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.DaftarStrukPenjualanService;
import com.jasamedika.medifirst2000.service.InputKantongDarahService;
import com.jasamedika.medifirst2000.service.LimbahB3MasukService;
import com.jasamedika.medifirst2000.service.PelayananObatService;
import com.jasamedika.medifirst2000.service.StrukPelayananService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.InputKantongDarahVO;
import com.jasamedika.medifirst2000.vo.KirimProdukRuanganVO;
import com.jasamedika.medifirst2000.vo.LimbahB3MasukVO;
import com.jasamedika.medifirst2000.vo.PelayananObatVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananVO;


@RestController
@RequestMapping("/struk-pelayanan")
@JsonIgnoreProperties(ignoreUnknown = true) 
public class StrukPelayananController extends LocaleController<StrukPelayananVO> {

	@Autowired
	private InputKantongDarahService inputKantongDarahService;
	
	@Autowired
	private LimbahB3MasukService limbahB3MasukService;
	
	@Autowired
	private StrukPelayananService strukPelayananService;
	
	@Autowired
	private PelayananObatService pelayananObatService;
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(StrukPelayananController.class);	
	
	@RequestMapping(value = "/kirim-produk-ke-ruangan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> kirimProdukKeRuangan(@Valid @RequestBody KirimProdukRuanganVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result=inputKantongDarahService.kirimProdukKeRuangan(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 blood pack data", 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 blood pack data", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	@RequestMapping(value = "/simpan-kantong-darah", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveInputKantongDarah(@Valid @RequestBody InputKantongDarahVO vo,HttpServletRequest request) {
		try {
			Map<String,Object> result=inputKantongDarahService.saveInputKantongDarah(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 blood pack data", 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 blood pack data", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/simpan-limbah-b3-masuk", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveLimbahB3Masuk(@Valid @RequestBody LimbahB3MasukVO vo,HttpServletRequest request) throws IllegalAccessException {
		try {
			Map<String,Object> result=limbahB3MasukService.saveLimbahB3Masuk(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 limbah b3 masuk data", 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 limbah b3 masuk data", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/update-limbah-b3-masuk", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> updateLimbahB3Masuk(@Valid @RequestBody LimbahB3MasukVO vo,HttpServletRequest request) throws IllegalAccessException {
		try {
			Map<String,Object> result=limbahB3MasukService.updateLimbahB3Masuk(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 limbah b3 masuk data", 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 limbah b3 masuk data", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/delete-limbah-b3-masuk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Boolean> deleteLimbahB3Masuk(
			@RequestParam(value = "noRec", required = true) String noRec, HttpServletRequest request) {
		try {
			Boolean result = limbahB3MasukService.deleteLimbahB3Masuk(noRec);
			if (result)
				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 deleteLimbahB3Masuk", 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 deleteLimbahB3Masuk", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		} catch (IllegalAccessException e) {
			LOGGER.error("Got exception {} when deleteLimbahB3Masuk", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/find-all-limbah-b3-masuk/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	 public ResponseEntity<Map<String,Object>> getAllVO(HttpServletRequest request) {
	  
		  try {
			   Map<String,Object> result=strukPelayananService.findAllStrukPelayananLimbahMasuk();
			   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 get all LimbahB3Masuk", 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 get all LimbahB3Masuk", jse.getMessage());
			   addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
			     jse.getMessage());
			   return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
			     mapHeaderMessage);
		  }
	 }
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-limbah-b3-masuk")
	public Map<String,Object>  lisLimbahB3Masuk(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "jenisLimbah", required = false) Integer jenisLimbah) {

		Map<String, Object> resultPageMap = limbahB3MasukService.lisLimbahB3Masuk(page, limit, sort, dir, dateStart, dateEnd, jenisLimbah);
		
		return resultPageMap;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-limbah-b3-masuk-bynorec")
	public Map<String,Object>  lisLimbahB3MasukByNoRec(
			@RequestParam(value = "noRec", required = false) String noRec) {

		Map<String, Object> resultPageMap = limbahB3MasukService.lisLimbahB3MasukByNoRec(noRec);
		
		return resultPageMap;
	}

	
	@RequestMapping(value = "/save-pelayanan-obat-dari-luar/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePelayananObatDariLuar(HttpServletRequest request) throws ParseException {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			PelayananObatVO vo = (PelayananObatVO) getItem(request, new PelayananObatVO());
			if (vo.getTanggalPendaftaran().indexOf("-") > -1)
				vo.setTglRegistrasi(format.parse(vo.getTanggalPendaftaran()));
			else
				vo.setTglRegistrasi(new Date(Long.parseLong(vo.getTanggalPendaftaran())));
			Map<String, Object> result = pelayananObatService.savePelayananObat2(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			SaveLog("Order Farmasi", "Permintaan", request);
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);

		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when add Pasien", 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 add Pasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@Autowired
	private DaftarStrukPenjualanService service;
	
	@RequestMapping(value = "/save-retur-struk-pelayanan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> returStrukPelayanan(@Valid @RequestBody StrukPelayananVO vo, HttpServletRequest request) {
		try {
			Map<String,Object> result = service.returPelayananPasien(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 saveK3CheckListFacillitySefety", 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 saveK3CheckListFacillitySefety", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
}
