package com.jasamedika.medifirst2000.controller;

import java.util.HashMap;
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.StrukOrderService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.PenerimaanBarangVO;
import com.jasamedika.medifirst2000.vo.RequestBarangDariRuanganVO;
import com.jasamedika.medifirst2000.vo.StrukKirimVO;
import com.jasamedika.medifirst2000.vo.StrukOrderVO;

@RestController
@RequestMapping("/request-permintaan-barang")
public class RequestPermintaanBarangController extends LocaleController<PenerimaanBarangVO> {
	
	private static final Logger LOG = LoggerFactory.getLogger(RequestPermintaanBarangController.class);

	@Autowired
	private StrukOrderService strukOrderService;

	@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 = 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) {
			LOG.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) {
			LOG.error("Got exception {} when save Penerimaan Barang", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/save-request-kirim-barang/", method = RequestMethod.POST, 
			produces = MediaType.APPLICATION_JSON_VALUE, 
			consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/save-verifikasi-pengiriman/", method = RequestMethod.POST, 
			produces = MediaType.APPLICATION_JSON_VALUE, 
			consumes = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
		} catch (ServiceVOException e) {
			LOG.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) {
			LOG.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", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = strukOrderService.findAllPermintaanBarang(page, limit, sort, dir, dateStart,dateEnd,noOrder);
		return resultPageMap;
	}
	
	@RequestMapping(value = "/get-order-unit", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = strukOrderService.findAllPermintaanBarangUnit(page, limit, sort, dir, dateStart,dateEnd,ruanganTujuanId,ruanganId,noOrder);
		return resultPageMap;
	}
	
	@RequestMapping(value = "/get-informasi-pengiriman-barang", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = strukOrderService.getInformasiPengirimanBarang(page, limit, sort, dir, dateStart,dateEnd,noOrder,parameter);
		return resultPageMap;
	}
	
	@RequestMapping(value = "/get-informasi-pengiriman-barang-aset", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = strukOrderService.getInformasiPengirimanBarangAset(page, limit, sort, dir, dateStart,dateEnd,noOrder,parameter);
		return resultPageMap;
	}
	
	@RequestMapping(value = "/get-informasi-penerimaan-barang", method = RequestMethod.GET, produces = MediaType.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) {
		Map<String, Object> resultPageMap = strukOrderService.getInformasiPenerimaanBarang(page, limit, sort, dir, dateStart,dateEnd,noOrder);
		return resultPageMap;
	}
	
	
	@RequestMapping(value = "/get-detail-permintaan-barang/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<StrukOrderVO> getDetailPermintaanBarang(
			@RequestParam(value = "noRec", required = true) String noRec) {
		StrukOrderVO strukOrder = strukOrderService.findByNoRec(noRec);
		try {
			return RestUtil.getJsonResponse(strukOrder, HttpStatus.OK, mapHeaderMessage);
		} catch (IllegalArgumentException e) {
			LOG.debug(e.getMessage());
		}
		return RestUtil.getJsonResponse(null, HttpStatus.NOT_FOUND, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/get-kirim-detail-permintaan-barang/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<StrukKirimVO> getKirimDetailPermintaanBarang(
			@RequestParam(value = "noRec", required = true) String noRec) {
		StrukKirimVO strukOrder = strukOrderService.findByStrukKirimNoRec(noRec);

		try {
			return RestUtil.getJsonResponse(strukOrder, HttpStatus.OK, mapHeaderMessage);
		} catch (IllegalArgumentException e) {
			LOG.debug(e.getMessage());
		}
		return RestUtil.getJsonResponse(null, HttpStatus.NOT_FOUND, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/get-no-order", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoOrder() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noOrder", strukOrderService.preFormatNoUrut("Struk Order", "STO"));
		return result;
	}
	
	@RequestMapping(value = "/get-no-kirim", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoKirim() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noKirim", strukOrderService.preFormatNoUrut("KRIM", "KRM"));
		return result;
	}
	
	@RequestMapping(value = "/get-no-konfirmasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoKonfirmasi() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noKonfirmasi", strukOrderService.preFormatNoUrut("KONFIRMASI", "KFS"));
		return result;
	}
	
	@RequestMapping(value = "/get-no-retur", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoRetur() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noRetur", strukOrderService.preFormatNoUrut("Struk Retur", "RRU"));
		return result;
	}
	
	@RequestMapping(value = "/get-no-pemusnahan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoPemusnahan() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noPemusnahan", strukOrderService.preFormatNoUrut("Pemusnahan", "PMA"));
		return result;
	}
	
	@RequestMapping(value = "/get-no-penerimaan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object>  getNoPenerimaan() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("noPenerimaan", strukOrderService.formatNoUrut("Penerimaan Supplier", "PNB"));
		return result;
	}

}
