package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PsrsPermintaanPerbaikanService;
import com.jasamedika.medifirst2000.service.StokProdukGlobalService;
import com.jasamedika.medifirst2000.service.StrukOrderService;
import com.jasamedika.medifirst2000.vo.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
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("/psrsPermintaanPerbaikan")
public class PsrsPermintaanPerbaikanController extends LocaleController<PermintaanPerbaikanVO> {

	private static final Logger LOGGER = getLogger(PsrsPermintaanPerbaikanController.class);

	@Autowired
	private PsrsPermintaanPerbaikanService psrsPermintaanPerbaikanService;

	@Autowired
	private StrukOrderService strukOrderService;

	@Autowired
	private StokProdukGlobalService stokProdukGlobalService;

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

	@RequestMapping(value = "/get-ruangan-by-user-login", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getRuanganByUserLogin() {
		Ruangan r = psrsPermintaanPerbaikanService.getRuanganByUserLogin();
		Map<String, Object> data = new HashMap<>();
		data.put("id", r.getId());
		data.put("namaRuangan", r.getNamaRuangan());
		return data;
	}

	@RequestMapping(value = "/get-ruangan-tujuan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getRuanganTujuan() {
		Ruangan r = psrsPermintaanPerbaikanService.getRuanganTujuan();
		Map<String, Object> data = new HashMap<>();
		data.put("id", r.getId());
		data.put("namaRuangan", r.getNamaRuangan());
		return data;
	}

	@RequestMapping(value = "/get-user-login", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getUserLogin() {
		Pegawai p = psrsPermintaanPerbaikanService.getPegawaiByUserLogin();
		Map<String, Object> data = new HashMap<>();
		data.put("id", p.getId());
		data.put("namaPegawai", p.getNamaLengkap());
		return data;
	}

	@RequestMapping(value = "/get-kontrak", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getKotrak() {
		return psrsPermintaanPerbaikanService.getKotrak();
	}

	@RequestMapping(value = "/get-kontrak-detail", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getKontrakDetail(@RequestParam(value = "noRec") String noRec) {
		return psrsPermintaanPerbaikanService.getKontrakDetail(noRec);
	}

	@RequestMapping(value = "/get-detail-permintaan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getDetailPermintaan(@RequestParam(value = "noRec") String noRec) {
		return psrsPermintaanPerbaikanService.getDetailPermintaan(noRec);
	}

	@RequestMapping(value = "/pre-add-perbaikan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> preAddPerbaikan(@RequestParam(value = "noRec") String noRec) {
		return psrsPermintaanPerbaikanService.preAddPerbaikan(noRec);
	}

	@RequestMapping(value = "/pre-add-pemeliharaan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> preAddPemeliharaan(@RequestParam(value = "noRec") String noRec) {
		return psrsPermintaanPerbaikanService.preAddPemeliharaan(noRec);
	}

	@RequestMapping(value = "/get-no-order-perbaikan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getNoPemusnahan() {
		Map<String, Object> result = new HashMap<>();
		result.put("noOrderPerbaikan", strukOrderService.preFormatNoUrut("Order Perbaikan", "PBK"));
		return result;
	}

	@RequestMapping(value = "/get-informasi-perbaikan-masuk", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> informasiPerbaikanMasuk(
			@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 = "jenisOrder", required = false) String jenisOrder) {
		return strukOrderService.getInformasiPerbaikanMasuk(page, limit, sort, dir, dateStart, dateEnd, noOrder,
				jenisOrder);
	}

	@RequestMapping(value = "/get-informasi-perbaikan-keluar", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> informasiPerbaikanKeluar(

			@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 = "jenisOrder", required = false) String jenisOrder) {
		return strukOrderService.getInformasiPerbaikanKeluar(page, limit, sort, dir, dateStart, dateEnd, noOrder,
				jenisOrder);
	}

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

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

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

	@RequestMapping(value = "/find-ruangan-asset", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getRuanganRegistrasiAset(HttpServletRequest request) {
		try {
			Map<String, Object> result = psrsPermintaanPerbaikanService.getRuanganPerbaikan();
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getRuanganPerbaikan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getRuanganPerbaikan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/find-produk-by-ruangan", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getAsetPerbaikan(@RequestParam(value = "id") Integer id,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = psrsPermintaanPerbaikanService.getProdukPerbaikan(id);
			if (null != result) {
				mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return getJsonResponse(result, OK, mapHeaderMessage);
			} else {
				return getJsonResponse(null, NOT_FOUND, mapHeaderMessage);
			}
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when getProdukPerbaikan", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when getProdukPerbaikan", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-aset-by-produk-and-ruangan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getAsetByProduk(@RequestParam(value = "produkId") Integer produkId,
			@RequestParam(value = "ruanganId") Integer ruanganId) {
		return psrsPermintaanPerbaikanService.getAsetByProdukAndRuangan(produkId, ruanganId);
	}

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

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

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

	@RequestMapping(value = "/get-stok-ruangan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getStokRuangan() {
		return stokProdukGlobalService.getStokRuangan();
	}

	@RequestMapping(value = "/verifikasi-ruangan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> verifikasiRuangan(
			@RequestParam(value = "noRecKirimProduk", required = false) String noRec) {
		return strukOrderService.verifikasiRuangan(noRec);
	}

	@RequestMapping(value = "/monitoring", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> monitoring(
			@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 = "ruanganId", required = false) Integer ruanganId,
			@RequestParam(value = "noAset", required = false) String noAset,
			@RequestParam(value = "produkId", required = false) Integer produkId) {
		return strukOrderService.monitoringPerbaikan(page, limit, sort, dir, dateStart, dateEnd, ruanganId, noAset,
				produkId);
	}

	@RequestMapping(value = "/detail-monitoring", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> detailMonitoring(
			@RequestParam(value = "strukOrderId", required = false) String strukOrderId,
			@RequestParam(value = "noRegistrasiAset", required = false) String noRegistrasiAset) {
		return strukOrderService.detailMonitoring(strukOrderId, noRegistrasiAset);
	}

	@RequestMapping(value = "/get-lap-permintaan-perbaikan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getLapPermintaanPerbaikan() {
		return psrsPermintaanPerbaikanService.getLapPermintaanPerbaikan();
	}

	@RequestMapping(value = "/get-lap-permintaan-perbaikan-by-date", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getLapPermintaanPerbaikanByDate(
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate) {
		return psrsPermintaanPerbaikanService.getLapPermintaanPerbaikanByDate(startDate, endDate);
	}

	@RequestMapping(value = "/get-lap-permintaan-perbaikan-by-noorder", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getLapPermintaanPerbaikanByNoTrans(
			@RequestParam(value = "noOrder", required = false) String noOrder) {
		return psrsPermintaanPerbaikanService.getLapPermintaanPerbaikanByNoOrder(noOrder);
	}

	@RequestMapping(value = "/get-lap-permintaan-perbaikan-by-jenis-order", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getLapPermintaanPerbaikanByJenisOrder(
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate,
			@RequestParam(value = "jenisOrder", required = false) String jenisOrder) {
		return psrsPermintaanPerbaikanService.getLapPermintaanPerbaikanByJenisOrder(jenisOrder, startDate, endDate);
	}

	@RequestMapping(value = "/get-suku-cadang-by-strukorder", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getOrderSukuCadangbyStrukOrder(String strukOrder) {
		return this.psrsPermintaanPerbaikanService.getOrderSukuCadangbyStrukOrder(strukOrder);
	}

	@RequestMapping(value = "/get-monitoring-perbaikan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getMonitoringPerbaikan(
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate) {
		return this.psrsPermintaanPerbaikanService.getMonitoringPerbaikanByDate(startDate, endDate);
	}

	@RequestMapping(value = "/get-asset-gedung-by-jenis-asset", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getAssetGedungByJenisAsset(Integer idJenis) {
		return this.psrsPermintaanPerbaikanService.getAssetGedungByJenisAsset(idJenis);
	}

	@RequestMapping(value = "/get-asset-non-gedung-by-jenis-asset", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getAssetNonGedungByJenisAsset(@RequestParam(value = "id") Integer id,
			@RequestParam(value = "idJenis") Integer idJenis, HttpServletRequest request) {
		return this.psrsPermintaanPerbaikanService.getAssetNonGedungByJenisAsset(id, idJenis);
	}

	@RequestMapping(value = "/get-asset-gedung-by-order-pelayanan", method = GET, produces = APPLICATION_JSON_VALUE)
	public Map<String, Object> getAssetGedungByOrderPelayanan(
			@RequestParam(value = "noOrderPelayanan", required = false) String noOrderPelayanan,
			HttpServletRequest request) {
		return this.psrsPermintaanPerbaikanService.getAssetGedungByOrderPelayanan(noOrderPelayanan);
	}

}
