package com.jasamedika.medifirst2000.controller;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
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.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.IBaseRestController;
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.RegistrasiSmsService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienRegistrasiVO;
import com.jasamedika.medifirst2000.vo.RegistrasiSmsVO;

/**
 * Controller class for Registrasi Pasien Business
 * 
 * @author adik
 */
@RestController
@RequestMapping("/registrasi-pasien-sms")
@JsonIgnoreProperties(ignoreUnknown = true)
public class RegistrasiPasienSmsController extends LocaleController<AntrianPasienRegistrasiVO>
		implements IBaseRestController<AntrianPasienRegistrasiVO> {
	@Autowired
	private RegistrasiSmsService registrasiSmsService;

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

	@RequestMapping(value = "/save-registrasi-sms/", method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> saveRegistrasiSms(
			@RequestParam(value = "noCm", required = true) String noCm,
			@RequestParam(value = "kodeRuangan", required = true) String kodeRuangan,
			@RequestParam(value = "kodeDokter", required = true) short kodeDokter,
			@RequestParam(value = "kodeRegulerEkslusif", required = true) Integer kodeRegulerEkslusif,
			@RequestParam(value = "tanggal", required = true) String tanggal, HttpServletRequest request) {

		try {
			RegistrasiSmsVO vo = new RegistrasiSmsVO();
			vo.setNoCm(noCm);
			vo.setKodeRuangan(kodeRuangan);
			vo.setKodeDokter(kodeDokter);
			vo.setKodeRegulerEkslusif(kodeRegulerEkslusif);
			vo.setTanggal(DateUtil.toDate(tanggal));
			Map<String, Object> result = registrasiSmsService.saveRegistrasiPasienSms(vo);
			Boolean dataFound = new Boolean((boolean) result.get("dataFound"));
			if (dataFound) {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,
						getMessage(MessageResource.LABEL_ERROR, 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);
		}

	}

	@RequestMapping(value = "/sms_gateway_kemenkes/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRegistrasiSms(
			@RequestParam(value = "msisdn", required = true) String msisdns,
			@RequestParam(value = "trx_id", required = true) String trx_id,
			@RequestParam(value = "sms", required = true) String sms, HttpServletRequest request) {

		String[] rs = sms.split("_");
		// rs[0] --> "WALL+RS"
		// rs[1] --> "DAFTAR"
		// rs[2] --> kode rs
		// rs[3] --> kode poli
		// rs[4] --> kode dokter
		// rs[5] --> kode cara bayar
		// rs[6] --> kode no rm
		// rs[7] --> kode tgl lahir
		// rs[8] --> kode tgl daftar

		try {
			RegistrasiSmsVO vo = new RegistrasiSmsVO();
			vo.setNoCm(rs[6]);
			vo.setKodeRuangan(rs[3]);

			String tgllahir = rs[7];
			String tgldaftar = rs[8];
			String kdDokter = rs[4];
			Map<String, Object> result = registrasiSmsService.saveRegistrasiPasienSmsKemenkes(vo, tgllahir, tgldaftar,
					kdDokter);
			Integer stats = new Integer((Integer) result.get("status"));
			if (stats > 0) {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,
						getMessage(MessageResource.LABEL_ERROR, 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);
		}

	}

	@RequestMapping(value = "/get-ruangan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> zzgetRuangan(HttpServletRequest request) {
		try {
			Map<String, Object> result = registrasiSmsService.findAllRuangan();
			Boolean dataFound = new Boolean((boolean) result.get("dataFound"));
			if (dataFound) {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,
						getMessage(MessageResource.LABEL_ERROR, 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);
		}

	}

	@RequestMapping(value = "/get-dokter/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDokter(
			@RequestParam(value = "kodeRuangan", required = true) Integer kodeRuangan,
			@RequestParam(value = "tanggal", required = true) String tanggal, HttpServletRequest request) {
		try {
			Map<String, Object> result = registrasiSmsService.findAllDokter(kodeRuangan, DateUtil.toDate(tanggal));
			Boolean dataFound = new Boolean((boolean) result.get("dataFound"));
			if (dataFound) {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,
						getMessage(MessageResource.LABEL_ERROR, 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);
		}

	}

	@RequestMapping(value = "/get-all-dokter", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDokterRuangan(HttpServletRequest request) {
		try {
			Map<String, Object> result = registrasiSmsService.findAllDokter();
			Boolean dataFound = new Boolean((boolean) result.get("dataFound"));
			if (dataFound) {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			} else {
				mapHeaderMessage.clear();
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,
						getMessage(MessageResource.LABEL_ERROR, 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);
		}
	}

	@Override
	public ResponseEntity<Collection<AntrianPasienRegistrasiVO>> getAllVOWithQueryString(HttpServletRequest request,
			Integer page, Integer limit, String sort, String dir) {
		return null;
	}

	@Override
	public ResponseEntity<AntrianPasienRegistrasiVO> getVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<String> addVO(AntrianPasienRegistrasiVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> editVO(AntrianPasienRegistrasiVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> deleteVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<List<AntrianPasienRegistrasiVO>> getAllVO() {
		return null;
	}

}
