package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.StrukOrderService;
import com.jasamedika.medifirst2000.vo.RequestBarangDariRuanganVO;
import com.jasamedika.medifirst2000.vo.StrukKirimVO;
import com.jasamedika.medifirst2000.vo.StrukOrderVO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/request-permintaan-barang")
public class RequestPermintaanBarangController extends LocaleController<RequestBarangDariRuanganVO> {

	private static final Logger LOG = getLogger(RequestPermintaanBarangController.class);

	@Autowired
	private StrukOrderService strukOrderService;

	@RequestMapping(value = "/save-request-permintaan-barang/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRequestPermintaanBarang(
			@Valid @RequestBody RequestBarangDariRuanganVO vo, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Map<String, Object> result = strukOrderService.savePermintanBarangDariRuangan(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOG.error("Got ServiceVOException {} when savePermintanBarangDariRuangan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOG.error("Got JpaSystemException {} when savePermintanBarangDariRuangan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/save-request-kirim-barang/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRequestKirimBarang(@Valid @RequestBody RequestBarangDariRuanganVO vo,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = strukOrderService.saveKirimBarangDariRuangan(vo);
		if (null != result)
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
		return getJsonResponse(result, CREATED, mapHeaderMessage);
	}

	@RequestMapping(value = "/save-verifikasi-pengiriman/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveVerifikasiPengiriman(
			@Valid @RequestBody RequestBarangDariRuanganVO vo, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Map<String, Object> result = strukOrderService.saveVerifikasiPengiriman(vo);
			if (null != result)
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOG.error("Got ServiceVOException {} when saveVerifikasiPengiriman", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOG.error("Got JpaSystemException {} when saveVerifikasiPengiriman", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-order", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getOrder(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noOrder", required = false) String noOrder) {
		return strukOrderService.findAllPermintaanBarang(page, limit, sort, dir, dateStart, dateEnd, noOrder);
	}

	@RequestMapping(value = "/get-order-unit", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getOrderUnit(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "ruanganTujuanId", required = false) Integer ruanganTujuanId,
			@RequestParam(value = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "noOrder", required = false) String noOrder) {
		return strukOrderService.findAllPermintaanBarangUnit(page, limit, sort, dir, dateStart, dateEnd,
				ruanganTujuanId, ruanganId, noOrder);
	}

	@RequestMapping(value = "/get-informasi-pengiriman-barang", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getInformasiPengirimanBarang(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noOrder", required = false) String noOrder,
			@RequestParam(value = "parameter", required = false) String parameter) {
		return strukOrderService.getInformasiPengirimanBarang(page, limit, sort, dir, dateStart, dateEnd, noOrder,
				parameter);
	}

	@RequestMapping(value = "/get-informasi-pengiriman-barang-aset", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getInformasiPengirimanBarangAset(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noOrder", required = false) String noOrder,
			@RequestParam(value = "parameter", required = false) String parameter) {
		return strukOrderService.getInformasiPengirimanBarangAset(page, limit, sort, dir, dateStart, dateEnd, noOrder,
				parameter);
	}

	@RequestMapping(value = "/get-informasi-penerimaan-barang", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getInformasiPenerimaanBarang(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "dateStart", required = false) String dateStart,
			@RequestParam(value = "dateEnd", required = false) String dateEnd,
			@RequestParam(value = "noOrder", required = false) String noOrder) {
		return strukOrderService.getInformasiPenerimaanBarang(page, limit, sort, dir, dateStart, dateEnd, noOrder);
	}

	@RequestMapping(value = "/get-detail-permintaan-barang/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<StrukOrderVO> getDetailPermintaanBarang(@RequestParam(value = "noRec") String noRec) {
		StrukOrderVO strukOrder = strukOrderService.findByNoRec(noRec);
		try {
			return getJsonResponse(strukOrder, OK, mapHeaderMessage);
		} catch (IllegalArgumentException e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-kirim-detail-permintaan-barang/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<StrukKirimVO> getKirimDetailPermintaanBarang(@RequestParam(value = "noRec") String noRec) {
		StrukKirimVO strukOrder = strukOrderService.findByStrukKirimNoRec(noRec);
		try {
			return getJsonResponse(strukOrder, HttpStatus.OK, mapHeaderMessage);
		} catch (IllegalArgumentException e) {
			throw new ServiceVOException(e.getMessage());
		}
	}

	@RequestMapping(value = "/get-no-order", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getNoOrder() {
		Map<String, Object> result = new HashMap<>();
		result.put("noOrder", strukOrderService.preFormatNoUrut("Struk Order", "STO"));
		return result;
	}

	@RequestMapping(value = "/get-no-kirim", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getNoKirim() {
		Map<String, Object> result = new HashMap<>();
		result.put("noKirim", strukOrderService.preFormatNoUrut("KRIM", "KRM"));
		return result;
	}

	@RequestMapping(value = "/get-no-retur", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getNoRetur() {
		Map<String, Object> result = new HashMap<>();
		result.put("noRetur", strukOrderService.preFormatNoUrut("Struk Retur", "RRU"));
		return result;
	}

	@RequestMapping(value = "/get-no-pemusnahan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getNoPemusnahan() {
		Map<String, Object> result = new HashMap<>();
		result.put("noPemusnahan", strukOrderService.preFormatNoUrut("Pemusnahan", "PMA"));
		return result;
	}

}
