package com.jasamedika.medifirst2000.controller;

import java.text.ParseException;
import java.util.ArrayList;
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.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.PasienDaftarService;
import com.jasamedika.medifirst2000.service.PelayananPasienService;
import com.jasamedika.medifirst2000.service.ProdukService;
import com.jasamedika.medifirst2000.service.ResepDokterService;
import com.jasamedika.medifirst2000.service.SatuanStandarService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.PelayananPasienVO;

@RestController
@RequestMapping("/pelayanan")
public class PelayananController extends LocaleController<PelayananPasienVO> {

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

	@Autowired
	private PelayananPasienService pelayananPasienService;

	@Autowired
	private PasienDaftarService pasienDaftarService;

	@Autowired
	private ProdukService produkService;

	@Autowired
	private SatuanStandarService satuanStandarService;

	@Autowired
	private ResepDokterService resepDokterService;

	@RequestMapping(value = "/calculate-indikator-pelayanan", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> calculateIndikatorPelayanan(HttpServletRequest request,
			@RequestParam("tahun") String tahun) throws ParseException {
		try {
			List<Map<String, Object>> result = pasienDaftarService.findIndikatorPelayanan(tahun);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when calculate indikator pelayanan", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when calculate indikator pelayanan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/validate-nama-produk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> validateNamaProduk(HttpServletRequest request,
			@RequestParam(value = "idProduk", required = false) Integer idProduk,
			@RequestParam(value = "namaProduk", required = true) String namaProduk) {
		try {
			Map<String, Object> result = produkService.validateNamaProduk(idProduk, namaProduk);

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

	@RequestMapping(value = "/paket-to-produk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getMappingPaketToProduk(HttpServletRequest request,
			@RequestParam(value = "idMapping", required = true) Integer idMapping) {
		try {
			Map<String, Object> result = produkService.getMappingPaketToProduk(idMapping);
			return RestUtil.getJsonResponse(result, HttpStatus.OK);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get mapping paket to produk", 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 mapping paket to produk", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/all-paket-produk", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> getAllPaketToProduk(HttpServletRequest request) {
		try {
			List<Map<String, Object>> result = produkService.getAllPaketToProduk();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when get all data paket to produk", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get all data paket to produk", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/master-satuan-standar", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> getAllMasterSatuanStandar(HttpServletRequest request) {
		List<Map<String, Object>> result = new ArrayList<>();
		try {
			result = satuanStandarService.getAll();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when get all master satuan standar distinct on", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get all master satuan standar distinct on", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/klaim-diskon", method = RequestMethod.GET)
	public ResponseEntity<List<String>> KlaimDiskon(HttpServletRequest request,
			@RequestParam(value = "noRegistrasi", required = true) String noRegistrasi,
			@RequestParam(value = "totalKlaim", required = true) Double totalKlaim,
			@RequestParam(value = "jenisDiskon", required = true) Integer jenisDiskon) {
		try {
			List<String> result = pelayananPasienService.updateKlaimDiskon(noRegistrasi, totalKlaim, jenisDiskon);
			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 klaim diskon karyawan", 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 klaim diskon karyawan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/reset-klaim-diskon", method = RequestMethod.GET)
	public ResponseEntity<String> ResetKlaimDiskon(HttpServletRequest request,
			@RequestParam(value = "noRegistrasi") String noRegistrasi) {
		try {
			String result = pelayananPasienService.resetKlaimDiskon(noRegistrasi);

			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 klaim diskon karyawan", 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 klaim diskon karyawan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/check-existing-harga-produk-kelas", method = RequestMethod.GET)
	public ResponseEntity<List<Map<String, Object>>> checkExistingHargaProdukKelas(HttpServletRequest request,
			@RequestParam(value = "kelasId", required = true) Integer idKelas,
			@RequestParam(value = "produkId", required = true) Integer idProduk,
			@RequestParam(value = "mappingId", required = false) Integer idMapping) {
		try {
			List<Map<String, Object>> result = produkService.findExistingMapHargaKelas(idKelas, idProduk, idMapping);

			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 check existing harga produk kelas", 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 check existing harga produk kelas", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
}
