package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.service.StokProdukGlobalService;
import com.jasamedika.medifirst2000.vo.StokProdukGlobalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_ERROR;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("/stok-produk-global")
public class StokProdukGlobalController extends LocaleController<StokProdukGlobalVO> {

	@Autowired
	private StokProdukGlobalService stokProdukGlobalService;

	@RequestMapping(value = "/find-produk-obat-stok/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getAllVO(@RequestParam(value = "generikId") Integer generikId,
			HttpServletRequest request) {
		Map<String, Object> result = stokProdukGlobalService.findProdukObatAndStok(generikId);
		boolean dataFound = (boolean) result.get("dataFound");
		mapHeaderMessage.clear();
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK, mapHeaderMessage);

	}

	@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) {
		return stokProdukGlobalService.listStok(page, limit, sort, dir, kelompokProdukId, produkId, asalProdukId,
				ruanganId, jenisProdukId);
	}

	@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) {
		return stokProdukGlobalService.listStokDetail(page, limit, sort, dir, kelompokProdukId, 1, produkId,
				asalProdukId, ruanganId, noTerima);
	}

	@RequestMapping(value = "/find-produk-obat-stok-no-generik/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getProdukObatStok(HttpServletRequest request) {
		Map<String, Object> result = stokProdukGlobalService.findProdukObatAndStok();
		boolean dataFound = (boolean) result.get("dataFound");
		mapHeaderMessage.clear();
		if (dataFound) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		} else {
			mapHeaderMessage.put(LABEL_ERROR, getMessage(MessageResource.LABEL_ERROR, request));
		}
		return getJsonResponse(result, OK, mapHeaderMessage);
	}

}
