package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.JadwalDokterService;
import com.jasamedika.medifirst2000.vo.JadwalDokter_VO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_ERROR;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.DateUtil.toDate;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/jadwalDokter")
public class JadwalDokterController extends LocaleController<JadwalDokter_VO> {

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

	@Autowired
	private JadwalDokterService jadwalDokterService;

	@RequestMapping(value = "/save-jadwal-dokter/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> addVO(@Valid @RequestBody JadwalDokter_VO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = jadwalDokterService.saveJadwalDokter(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when saveJadwalDokter", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveJadwalDokter", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-all-jadwal-dokter/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> addAllVO(@Valid @RequestBody List<JadwalDokter_VO> vos,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = jadwalDokterService.saveListJadwalDokter(vos);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when saveListJadwalDokter", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveListJadwalDokter", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/update-all-jadwal-dokter/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> updateAllVo(@Valid @RequestBody List<JadwalDokter_VO> vos,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = jadwalDokterService.updateListJadwalDokter(vos);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when updateListJadwalDokter", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updateListJadwalDokter", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/find-by-ruangan/{id}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findByNoCm(@PathVariable("id") Integer id, HttpServletRequest request) {
		Map<String, Object> result = jadwalDokterService.findByRuangan(id);
		boolean dataFound = (boolean) result.get("dataFound");
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK);
	}

	@RequestMapping(value = "/find-by-ruangan-tanggal/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findByNoCmtglJadwal(
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "tglJadwal", required = false) String tanggalJadwal, HttpServletRequest request) {
		Map<String, Object> result = jadwalDokterService.findJadwalByTanggalRuangan(toDate(tanggalJadwal), id);
		boolean dataFound = (boolean) result.get("dataFound");
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK);
	}

	@RequestMapping(value = "/find-by-antrian-tanggal/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findByNoCmtglJadwal(
			@RequestParam(value = "noRec", required = false) String noRec,
			@RequestParam(value = "tglJadwal", required = false) String tanggalJadwal, HttpServletRequest request) {
		Map<String, Object> result = jadwalDokterService.findJadwalByTanggalRuangan(toDate(tanggalJadwal), noRec);
		boolean dataFound = (boolean) result.get("dataFound");
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK);
	}

	/*
	 * create by iwan date : 21.04.2017
	 */
	@RequestMapping(value = "/find-by-periode-ruangan-dokter", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findJadwalDokterByPeriodRuangan(
			@RequestParam(value = "iddok", required = false) final int iddok,
			@RequestParam(value = "id", required = false) final int id,
			@RequestParam(value = "from", required = false) final String from,
			@RequestParam(value = "to", required = false) final String to, final HttpServletRequest request) {
		Map<String, Object> result = this.jadwalDokterService.findJadwalDokterByKisaranTglRuangan(iddok, id,
				toDate(from), toDate(to));
		boolean dataFound = (boolean) result.get("dataFound");
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK);
	}

	@RequestMapping(value = "/get-jadwal-dokter-by-id", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getJadwalDokterByIdDokter(
			@RequestParam(value = "id", required = false) Integer id, HttpServletRequest request) {
		Map<String, Object> result = jadwalDokterService.getJadwalDokterByIdDokter(id);
		boolean dataFound = (boolean) result.get("dataFound");
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK);
	}

	@RequestMapping(value = "/get-jadwal-supir-ambulance/{bulan}/{tahun}", method = GET, produces = APPLICATION_JSON_VALUE)
	public List<Map<String, Object>> getAllInformasi(@PathVariable("bulan") Integer bulan,
			@PathVariable("tahun") Integer tahun, HttpServletRequest request) {
		return jadwalDokterService.getJadwalSupirAmbulanceByPeriode(bulan, tahun);
	}

	@RequestMapping(value = "/get-jadwal-supir-ambulance/{id}/{bulan}/{tahun}", method = GET, produces = APPLICATION_JSON_VALUE)
	public List<Map<String, Object>> getJadwalByIdAndPeriode(@PathVariable("id") Integer id,
			@PathVariable("bulan") Integer bulan, @PathVariable("tahun") Integer tahun, HttpServletRequest request) {
		return jadwalDokterService.getJadwalSupirAmbulanByIdAndPeriode(id, bulan, tahun);
	}

}
