package com.jasamedika.medifirst2000.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.OrderGiziService;
import com.jasamedika.medifirst2000.service.StrukOrderService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.GiziProduksiOrderVO;
import com.jasamedika.medifirst2000.vo.MapProdukMenuToBahanVO;
import com.jasamedika.medifirst2000.vo.RequestBarangDariRuanganVO;

@RestController
@RequestMapping(value="/order-gizi")
public class OrderGiziController extends LocaleController<MapProdukMenuToBahanVO>{

	@Autowired
	private OrderGiziService orderService;
	
	@Autowired
	private StrukOrderService strukOrderService;
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(OrderGiziController.class);
	
	@RequestMapping(value="/get-bahan-order/", method= RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> getBahanOrderGizi(
			@RequestParam(value="noOrder",required=true) String noOrder,
			@RequestParam(value="jenisWaktu",required=false,defaultValue="-1") Integer jenisWaktu,
			HttpServletRequest request)
	{
			Map<String, Object> result = new HashMap<String, Object>();
			result = orderService.getBahanOrder(noOrder,jenisWaktu);
			 if (null != result)
				   mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
	}
	
	//Untuk Pesan Bahan ke Logistik. namun belum berdasarkan siklus Gizi  dan auto generate belum dibuat
	@RequestMapping(value = "/save-request-permintaan-barang/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRequestPermintaanBarang(@Valid @RequestBody RequestBarangDariRuanganVO vo,HttpServletRequest request,HttpServletResponse response) {
		
		try {
			Map<String, Object> result = orderService.savePermintanBarangDariRuangan(vo);
//			Map<String, Object> result = strukOrderService.savePermintanBarangDariRuangan(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 save Penerimaan Barang", 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 save Penerimaan Barang", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value="/get-order-siklus/", method= RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> generateOrderSiklus(
			HttpServletRequest request)
	{
			Map<String, Object> result = new HashMap<String, Object>();
			result = orderService.generateMenuSiklus();
			 if (null != result)
				   mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
	}
	
	@RequestMapping(value="/get-order-gizi/", method= RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> getOrderGizi(
			@RequestParam(value="startDate") String startDate,
			@RequestParam(value="endDate") String endDate,
			HttpServletRequest request)
	{
			Map<String, Object> result = new HashMap<String, Object>();
			result = orderService.generateMenuSiklus();
			 if (null != result)
				   mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
	}
	@RequestMapping(value="/produksi/",method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> produksiOrder(@Valid @RequestBody List<GiziProduksiOrderVO> vos,HttpServletRequest request){
		try {
			Map<String, Object> result = orderService.produksiOrder(vos);
			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 Produksi Order", 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 Produksi Order", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
}
