package com.jasamedika.medifirst2000.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
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.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.entities.UnitCost;
import com.jasamedika.medifirst2000.entities.UnitCostDetail;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.UnitCostService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.UnitCostDetailVO;
import com.jasamedika.medifirst2000.vo.UnitCostVO;

@RestController
@RequestMapping("/unit-cost")
public class DaftarKegiatanController extends LocaleController<UnitCostDetailVO> {
	
	@Autowired
	private UnitCostService unitCostService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DaftarKegiatanController.class);
	 
	@RequestMapping(value = "/detail-unit-cost", method = RequestMethod.GET)
	public ResponseEntity< Map<String,Object>> findAllDepartemenById5And6(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idInstalasiDepartemen", required = false) Integer idInstalasiDepartemen,
			@RequestParam(value = "idLayanan", required = false) Integer idLayanan,
			@RequestParam(value = "periodeAwal", required = false) String periodeAwal,
			@RequestParam(value = "periodeAkhir", required = false) String periodeAkhir,
			HttpServletRequest request) {
		try {
			 //String idRuangan, String idInstalasiDepartemen, Date periodeAwal, Date periodeAkhir,String idLayanan
			Map<String,Object> listDepartemen = unitCostService.findDetailCostUnit(idRuangan,idInstalasiDepartemen,periodeAwal,periodeAkhir,idLayanan);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			return RestUtil.getJsonResponse(listDepartemen, HttpStatus.OK,mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get all dokter", 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 dokter", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	@RequestMapping(value = "/get-list-pelayanan", method = RequestMethod.GET)
	public ResponseEntity< Map<String,Object>> findListPelayanan(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			HttpServletRequest request) {
		try {
			 
			Map<String,Object> listPelayanan = unitCostService.findListPelayanan(idRuangan);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			return RestUtil.getJsonResponse(listPelayanan, HttpStatus.OK,mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when get all dokter", 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 dokter", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	//Simpan data unit cost
	@RequestMapping(value = "/save-unit-cost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveDaftarKerja( @RequestBody UnitCostVO vo,
			HttpServletRequest request) throws ParseException {
		try {
			 
			UnitCostVO result = (UnitCostVO) unitCostService.add(vo);
			Map<String, Object> mapResult= new HashMap<>();
			mapResult.put("data", result);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(mapResult, 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);
		}

	}
	@RequestMapping(value = "/get-list-unit-cost-jenis", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> findUnitCostJenis(HttpServletRequest request) throws ParseException {
		try {
			Map<String, Object> mapResult= unitCostService.getListJenisUnitCost();
		 		mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(mapResult, 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);
		}

	}
	@RequestMapping(value = "/get-list-unit-cost-jenis-detail", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> findUnitCostJenisDetail(
			@RequestParam(value = "idUnitCostJenis", required = false) Integer idUnitCostJenis,
			HttpServletRequest request) throws ParseException {
		try {
			Map<String, Object> mapResult= unitCostService.getListDetailJenisUnitCost(idUnitCostJenis);
		 		mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(mapResult, 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);
		}
	}
	@RequestMapping(value = "/get-list-unit-cost", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> findUnitCost(
			@RequestParam(value = "idRuangan", required = false) Integer idRuangan,
			@RequestParam(value = "idInstalasi", required = false) Integer idInstalasi,
			@RequestParam(value = "namaKegiatan", required = false) String namaKegiatan,
			HttpServletRequest request) throws ParseException {
		try {
			Map<String, Object> mapResult= unitCostService.getListUnitCost(idRuangan,idInstalasi,namaKegiatan);
		 
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(mapResult, HttpStatus.OK, 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) {
			jse.printStackTrace();
			LOGGER.error("Got exception {} when add Pasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	@RequestMapping(value = "/get-unit-cost-by-id", method = RequestMethod.GET)
	public ResponseEntity<UnitCost> findUnitCost(
			@RequestParam(value = "idUnitCost", required = false) Integer idUnitCost,
			 
			HttpServletRequest request) throws ParseException {
		try {
			UnitCost mapResult= unitCostService.findbyUnitCostId(idUnitCost);
		 
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(mapResult, HttpStatus.OK, 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) {
			jse.printStackTrace();
			LOGGER.error("Got exception {} when add Pasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	@RequestMapping(value = "/get-unit-cost-detail-by-unit-cost-id", method = RequestMethod.GET)
	public ResponseEntity<List<UnitCostDetail>> findUnitCostDetailByUnitCostId(
			@RequestParam(value = "idUnitCost", required = false) Integer idUnitCost,
			 
			HttpServletRequest request) throws ParseException {
		try {
			List<UnitCostDetail> mapResult= unitCostService.findUnitCostDetailByUnitCostId(idUnitCost);
		 
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(mapResult, HttpStatus.OK, 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) {
			jse.printStackTrace();
			LOGGER.error("Got exception {} when add Pasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	 
}
