package com.jasamedika.medifirst2000.controller;

import java.util.Collection;
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.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.CheckOutOperationService;
import com.jasamedika.medifirst2000.service.SignInOperationService;
import com.jasamedika.medifirst2000.service.SignOutOperationService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.CheckOutOperationVO;
import com.jasamedika.medifirst2000.vo.RegistrasiPelayananVO;
import com.jasamedika.medifirst2000.vo.SignInOperationVO;
import com.jasamedika.medifirst2000.vo.SignOutOperationVO;


/******************
 * @author Shakato
 ******************/
@RestController
@RequestMapping("/operation")
@JsonIgnoreProperties(ignoreUnknown = true)
public class OperationController extends LocaleController<SignOutOperationVO>
{
	
	@Autowired
	private SignOutOperationService signOutOperationService;
	
	@Autowired
	private CheckOutOperationService checkOutOperationService;
	
	private static final Logger LOGGER 	= LoggerFactory.getLogger(OperationController.class);
	
	
	@RequestMapping(value = "/save-signout-operation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> saveSignInOperation(@Valid @RequestBody SignOutOperationVO vo, HttpServletRequest request) {
		try {
			Map<String,Object> result = signOutOperationService.saveSignOutOperationService(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
				RestUtil.getJsonHttptatus(HttpStatus.CREATED);
			return RestUtil.getJsonResponse("Status Sukses", HttpStatus.CREATED,mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when save SignOutOperation1", 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 update SignOutOperation2", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/save-checkout-operation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> saveCheckOutOperation(@Valid @RequestBody CheckOutOperationVO vo, HttpServletRequest request) {
		try {
			Map<String,Object> result = checkOutOperationService.saveCheckOutOperationService(vo);
			if (null != result)
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
				RestUtil.getJsonHttptatus(HttpStatus.CREATED);
			return RestUtil.getJsonResponse("Status Sukses", HttpStatus.CREATED,mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when save CheckOutOperation1", 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 update CheckOutOperation2", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(
			value = "/get-list-kesadaran-check-out", 
			  method = RequestMethod.GET, 
			    produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getKesadaranCheckOut(HttpServletRequest request){
		
		try {
			Map<String,Object> result = this.checkOutOperationService.getKesadaranCheckOut();
			
			if (null != result){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
				
			} else{
				return RestUtil.getJsonResponse(result, HttpStatus.NOT_FOUND,mapHeaderMessage);
				
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getKesadaranCheckOut", 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 getKesadaranCheckOut", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
			
		}
	}
	
	
	@RequestMapping(
			value = "/get-list-kelengkapan-check-out", 
			  method = RequestMethod.GET, 
			    produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getKelengkapanCheckOut(HttpServletRequest request){
		
		try {
			Map<String,Object> result = this.checkOutOperationService.getKelengkapanCheckOut();
			
			if (null != result){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
				
			} else{
				return RestUtil.getJsonResponse(result, HttpStatus.NOT_FOUND,mapHeaderMessage);
				
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getKelengkapanCheckOut", 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 getKelengkapanCheckOut", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
			
		}
	}
	
	@RequestMapping(
			value = "/get-check-out-by-norec", 
			  method = RequestMethod.GET, 
			    produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getCheckOutByNoRec(
			@RequestParam(value = "noRec", required = true) String noRec, HttpServletRequest request){
		
		try {
			Map<String,Object> result = this.checkOutOperationService.getCheckOutByNoRec(noRec);
			
			if (null != result){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
				
			} else{
				return RestUtil.getJsonResponse(result, HttpStatus.NOT_FOUND,mapHeaderMessage);
				
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getCheckOutByNoRec", 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 getCheckOutByNoRec", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
			
		}
	}
	
	@RequestMapping(
			value = "/get-check-out-by-norec-antrian", 
			  method = RequestMethod.GET, 
			    produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getCheckOutByNoRecAntrian(
			@RequestParam(value = "noRec", required = true) String noRec, HttpServletRequest request){
		
		try {
			Map<String,Object> result = this.checkOutOperationService.getCheckOutByNoRecAntrian(noRec);
			
			if (null != result){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse(result, HttpStatus.OK,mapHeaderMessage);
				
			} else{
				return RestUtil.getJsonResponse(result, HttpStatus.NOT_FOUND,mapHeaderMessage);
				
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when getCheckOutByNoRecAntrian", 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 getCheckOutByNoRecAntrian", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
			
		}
	}


}
