package com.jasamedika.medifirst2000.controller;

import java.util.Date;
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.security.core.userdetails.User;
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.dao.LoginUserDao;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.security.model.UserAuthentication;
import com.jasamedika.medifirst2000.security.service.TokenAuthenticationService;
import com.jasamedika.medifirst2000.service.ActivityPegawaiService;
import com.jasamedika.medifirst2000.service.DiagnosaPasienService;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.DiagnosaPasienVO;
import com.jasamedika.medifirst2000.vo.K3CheckListFacillitySefetyVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPelayananVO;

/**
 * Controller class for Registrasi Pasien Business
 * 
 * @author Adik
 */
@RestController
@RequestMapping("/diagnosa-pasien")
public class DiagnosaPasienController extends LocaleController<RegistrasiPelayananVO>{

	@Autowired
	private DiagnosaPasienService diagnosaPasienService;
	
	
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(DiagnosaPasienController.class);	
	@Autowired
	private LoginUserDao loginUserDao;
	@Autowired
	private ActivityPegawaiService activityPegawaiServiceImpl;
	
//	@RequestMapping(value = "/save-diagnosa-pasien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
//	public ResponseEntity<Map<String, Object>> saveDiagnosaPasien(@Valid @RequestBody DiagnosaPasienVO vo,HttpServletRequest request) {
//		try {
//			Map<String,Object> result=diagnosaPasienService.saveDiagnosaPasien(vo);
//			if (null != result)
//			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
//			String token = request.getHeader(WebConstants.AUTH_HEADER_NAME);
//			final User user = TokenAuthenticationService.tokenHandler.parseUserFromToken(token);
//			if (user != null) { 	
//				{ 
//					UserAuthentication data=	new UserAuthentication(user);
//					PegawaiVO pegawai = new PegawaiVO();
//					pegawai.setId(loginUserDao.findByNamaUser(data.getName()).get(0).getPegawai().getId());
//					activityPegawaiServiceImpl.record(pegawai, new Date(), "Memasukan Diagnosis");
//					BroadcastMessage("IKI", "");
//				}
//			}
//			return RestUtil.getJsonResponse(result, HttpStatus.CREATED,mapHeaderMessage);
//
//		} catch (ServiceVOException e) {
//			LOGGER.error("Got exception {} when saveDiagnosaPasien", 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 saveDiagnosaPasien", jse.getMessage());
//			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
//			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
//		}
//
//	}
	
	@RequestMapping(value = "/save-diagnosa-pasien", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveDiagnosaPasien(@Valid @RequestBody DiagnosaPasienVO vo, HttpServletRequest request) {
		try {
			Map<String,Object> result=diagnosaPasienService.saveDiagnosaPasien(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 saveDiagnosaPasien", 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 saveDiagnosaPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/get-by-no-cm", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> getByNoCM(@RequestParam(value="noCM", required= false) String noCm,HttpServletRequest request) {
		try {
			Map<String,Object> result=diagnosaPasienService.findByNoCm(noCm);
			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 saveDiagnosaPasien", 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 saveDiagnosaPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
		
	}
	
	@RequestMapping(value = "/find-all", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> findAll(HttpServletRequest request) {
		try {
			Map<String,Object> result=diagnosaPasienService.findAll();
			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 saveDiagnosaPasien", 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 saveDiagnosaPasien", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
		
	}
	
	@RequestMapping(value = "/get-diagnosa-by-antrian", method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> getDiagnosaByAntrian(@RequestParam(value="noRec", required= true) String noRec,HttpServletRequest request) {
		try {
			Map<String,Object> result=diagnosaPasienService.findDiagnosaPasienByAntrian(noRec);
			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.OK,mapHeaderMessage);

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