package com.jasamedika.medifirst2000.controller;

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.ResponseBody;
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.AlatService;
import com.jasamedika.medifirst2000.service.StokProdukGlobalService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.AlatVO;
import com.jasamedika.medifirst2000.vo.StokProdukGlobalVO;

@RestController
@RequestMapping("/stok-produk-global")
public class StokProdukGlobalController extends LocaleController<StokProdukGlobalVO> {
	
	@Autowired
	private StokProdukGlobalService stokProdukGlobalService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(StokProdukGlobalController.class);
		
	@RequestMapping(value = "/find-produk-obat-stok/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getAllVO(
			@RequestParam(value = "generikId", required = true) Integer generikId, HttpServletRequest request) {
		Map<String, Object> result = stokProdukGlobalService.findProdukObatAndStok(generikId);
		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);

	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-stok")
	@ResponseBody
	public Map<String, Object> listStok(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "kelompokProdukId", required = false) Integer kelompokProdukId,
			@RequestParam(value = "produkId", required = false) Integer produkId,
			@RequestParam(value = "asalProdukId", required = false) Integer asalProdukId,
			@RequestParam(value = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "jenisProdukId", required = false) Integer jenisProdukId) {

		Map<String, Object> resultPageMap = stokProdukGlobalService.listStok(page, limit, sort, dir, kelompokProdukId, produkId, asalProdukId, ruanganId,jenisProdukId);

		return resultPageMap;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/cetak-list-stok")
	@ResponseBody
	public Map<String, Object> cetakListStok(
			@RequestParam(value = "kelompokProdukId", required = false) Integer kelompokProdukId,
			@RequestParam(value = "produkId", required = false) Integer produkId,
			@RequestParam(value = "asalProdukId", required = false) Integer asalProdukId,
			@RequestParam(value = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "jenisProdukId", required = false) Integer jenisProdukId) {

		Map<String, Object> resultPageMap = stokProdukGlobalService.cetakListStok(kelompokProdukId, produkId, asalProdukId, ruanganId,jenisProdukId);

		return resultPageMap;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-stok-detail")
	@ResponseBody
	public Map<String, Object> listStokDetail(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "kelompokProdukId", required = false) Integer kelompokProdukId,
			@RequestParam(value = "produkId", required = false) Integer produkId,
			@RequestParam(value = "asalProdukId", required = false) Integer asalProdukId,
			@RequestParam(value = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "noTerima", required = false) String noTerima) {

		Map<String, Object> resultPageMap = stokProdukGlobalService.listStokDetail(page, 
				limit, 
				sort, 
				dir, 
				kelompokProdukId, 
				1, // Sementara 
				produkId,
				asalProdukId, 
				ruanganId,
				noTerima);

		return resultPageMap;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/cetak-list-stok-detail")
	@ResponseBody
	public Map<String, Object> cetakListStokDetail(
			@RequestParam(value = "kelompokProdukId", required = false) Integer kelompokProdukId,
			@RequestParam(value = "produkId", required = false) Integer produkId,
			@RequestParam(value = "asalProdukId", required = false) Integer asalProdukId,
			@RequestParam(value = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "noTerima", required = false) String noTerima,
			@RequestParam(value = "jenisProdukId", required = false) Integer jenisProdukId) {

		Map<String, Object> resultPageMap = stokProdukGlobalService.cetakListStokDetail(kelompokProdukId, produkId, asalProdukId, ruanganId,noTerima,jenisProdukId);

		return resultPageMap;
	}
	
	@RequestMapping(value = "/find-produk-obat-stok-no-generik/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getProdukObatStok(HttpServletRequest request) {
		Map<String, Object> result = stokProdukGlobalService.findProdukObatAndStok();
		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);

	}

}
