package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.PemakaianAirBersih;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.PemakaianAirBersihService;
import com.jasamedika.medifirst2000.vo.PemakaianAirBersihVO;
import net.kaczmarzyk.spring.data.jpa.domain.DateBetween;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collection;
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("/pemakaian-air-bersih")
public class PemakaianAirBersihController extends LocaleController<PemakaianAirBersihVO> {

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

	@Autowired
	private PemakaianAirBersihService<PemakaianAirBersihVO> pemakaianAirBersihService;

	@RequestMapping(value = "/save-pemakaian-air-bersih/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> addVO(@Valid @RequestBody PemakaianAirBersihVO vo,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = pemakaianAirBersihService.addPemakaianAirBersih(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 addPemakaianAirBersih", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when addPemakaianAirBersih", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/find-all-pemakaian-air-bersih/", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getAllVO(HttpServletRequest request) {
		try {
			Map<String, Object> result = pemakaianAirBersihService.findAllPemakaianAirBersih();
			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 findAllPemakaianAirBersih", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when findAllPemakaianAirBersih", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/find-by-periode/")
	@ResponseBody
	public ResponseEntity<Collection<PemakaianAirBersihVO>> findByPeriode(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@Spec(path = "bulan", params = { "dateStart",
					"dateEnd" }, config = "yyyy-MM-dd", spec = DateBetween.class) Specification<PemakaianAirBersih> spec) {
		Map<String, Object> resultPageMap = pemakaianAirBersihService.pemakaianAirBersihPaging(page, limit, sort, dir,
				spec);
		return constructListPageResult(resultPageMap);
	}

	@RequestMapping(value = "/delete-pemakaian-air-bersih", method = GET)
	public ResponseEntity<String> deleteNamaBahan(@RequestParam(value = "noRec") String noRec) {
		try {
			if (pemakaianAirBersihService.deletePemakaianAirBersih(noRec))
				return getJsonResponse(noRec + " Dihapus", CREATED);
		} catch (ServiceVOException e) {
			LOGGER.error("Got ServiceVOException {} when deletePemakaianAirBersih", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when deletePemakaianAirBersih", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
		return getJsonHttpStatus(NOT_ACCEPTABLE);
	}

}
