package com.jasamedika.medifirst2000.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.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.dto.MapPaketToProdukDto;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.CssdBmhpService;
import com.jasamedika.medifirst2000.service.CssdSterilisasiService;
import com.jasamedika.medifirst2000.service.StokProdukGlobalService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.CssdBmhpHeaderVO;
import com.jasamedika.medifirst2000.vo.CssdProduksiBmhpHeaderVO;
import com.jasamedika.medifirst2000.vo.CssdSterilisasiVO;
import com.jasamedika.medifirst2000.vo.PermintaanPerbaikanVO;

@RestController
@RequestMapping("/cssd-sterilisasi")
public class CssdSterilisasiController extends LocaleController {
	
	@Autowired
	private CssdSterilisasiService cssdSterilisasiService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CssdSterilisasiController.class);

	@RequestMapping(value = "/save-cssd-sterilisasi/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveCssdSterilisasi(@Valid @RequestBody CssdSterilisasiVO vo,HttpServletRequest request,HttpServletResponse response) {
		
		try {
			Map<String, Object> result = cssdSterilisasiService.saveSterilisasi(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 Sterilisasi Alat", 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 Sterilisasi Alat", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/get-paket", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getPaket() {
		Map<String, Object> data = cssdSterilisasiService.getPaket();
		return data;
	}
	
	@RequestMapping(value = "/get-no-urut", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getNoUrut() {
		Map<String, Object> data = cssdSterilisasiService.getNoUrut();
		return data;
	}
	
	@RequestMapping(value = "/get-produk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getByIdProduk(@RequestParam(value = "paketId", required = true) Integer paketId, @RequestParam(value = "produkId", required = true) Integer produkId) {
		Map<String, Object> data = cssdSterilisasiService.getByIdProduk(paketId, produkId);
		return data;
	}
	
	@RequestMapping(value = "/get-produk-by-paket", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getProdukByPaket(@RequestParam(value = "paketId", required = true) Integer paketId) {
		Map<String, Object> data = cssdSterilisasiService.getProdukByPaket(paketId);
		return data;
	}
	
	@RequestMapping(value = "/get-produk-non-paket", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getProdukNonPaket() {
		Map<String, Object> data = cssdSterilisasiService.getProdukNonPaket();
		return data;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-sterilisasi")
	@ResponseBody
	public Map<String, Object> listSterilisasi(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "tanggalAwal", required = false) String tanggalAwal,
			@RequestParam(value = "tanggalAhir", required = false) String tanggalAhir,
			@RequestParam(value = "noOrder", required = false) String noOrder,
			@RequestParam(value = "jenisCustomer", required = false) String jenisCustomer) {

		Map<String, Object> resultPageMap = cssdSterilisasiService.listSterilisasi(page,limit,sort,dir,tanggalAwal,tanggalAhir,noOrder,jenisCustomer);

		return resultPageMap;
	}
	
	@RequestMapping(value = "/get-no-order", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoOrder() {

		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noOrderPerbaikan", cssdSterilisasiService.preFormatNoUrut("Penerimaan Supplier", "PNB"));
		return result;
	}
	
	@RequestMapping(value = "/cetak-sterilisasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> cetakSterilisasi(
			@RequestParam(value = "strukPelayananId", required = true) String strukPelayananId) {
		Map<String, Object> result = cssdSterilisasiService.cetakSterilisasi(strukPelayananId);

		return result;
	}
	
	@RequestMapping(value = "/cetak-bukti-sterilisasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> cetakBuktiSterilisasi(
			@RequestParam(value = "strukPelayananId", required = true) String strukPelayananId) {
		Map<String, Object> result = cssdSterilisasiService.cetakBuktiSterilisasi(strukPelayananId);

		String data=(String)result.get("status");
		if(data.equalsIgnoreCase("error")){
			return result;
		}else{
			return result;
		}
		
	}
	
	@RequestMapping(value = "/get-jenis-paket", 
			method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getjenisPaket() {

		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noOrderPerbaikan", cssdSterilisasiService.getJenisPaket());
		return result;
	}
	
	@RequestMapping(value = "/get-paket-by-jenis-paket", 
			method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getPaketByJenisPaket(
			@RequestParam(value = "idJenisPaket", required = true) Integer idJenisPaket) {
		Map<String, Object> result = cssdSterilisasiService.getPaketByJenisPaket(idJenisPaket);

		return result;
	}
	
	@RequestMapping(value = "/get-map-paket-to-produk", 
			method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getMapProdukByPaket(
			@RequestParam(value = "idPaket", required = true) Integer idPaket) {
		Map<String, Object> result = cssdSterilisasiService.getMapPaketToProduk(idPaket);

		return result;
	}
	
	
	
	@RequestMapping(value="/save-all-map-paket-to-produk", 
			method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> saveAllMapPaketToProduk(
			@RequestBody List<MapPaketToProdukDto> dto,
			HttpServletRequest request) {
		List<Map<String,Object>> result = cssdSterilisasiService.saveMapPaketToProduk(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		}
		return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);

	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-map-paket-to-produk")
	@ResponseBody
	public Map<String, Object> getListMapPaketToProduk(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "namaPaket", required = false, defaultValue = "") String namaPaket) {

		Map<String, Object> resultPageMap = 
				cssdSterilisasiService.getListMapPaketToProduk(page,limit,sort,dir,namaPaket);

		return resultPageMap;
	}
	
	
	@RequestMapping(value="/unmapping-paket-to-produk", 
			method = RequestMethod.POST, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> unmappingPaketToProduk(
			@RequestBody MapPaketToProdukDto dto, HttpServletRequest request){
		Map<String, Object> result = cssdSterilisasiService.unmappingPaketToProduk(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		}
		return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/get-produk-cssd", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getProdukCssd() {
		Map<String, Object> data = cssdSterilisasiService.getProdukCssd();
		return data;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-permintaan-sterilisasi-dari-ruangan")
	@ResponseBody
	public Map<String, Object> listPermintaanSterilisasiByRuangan(
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate) {

		Map<String, Object> resultPageMap = cssdSterilisasiService.getPermintaanSteriliasi(startDate, endDate);

		return resultPageMap;
	}
	
	@RequestMapping(value = "/save-cssd-permintaan-sterilisasi-dari-ruangan/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveCssdPermintaanSterilisasiDariRuangan(
			@Valid @RequestBody CssdSterilisasiVO vo,HttpServletRequest request,HttpServletResponse response) {
		
		try {
			Map<String, Object> result = cssdSterilisasiService.savePermintaanSterilisasiInternal(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 Sterilisasi Alat", 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 Sterilisasi Alat", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	
}
