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.KontrakService;
import com.jasamedika.medifirst2000.vo.RekananSkKontrakDetailVO;
import com.jasamedika.medifirst2000.vo.RekananSkKontrakVO;
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.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
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("/kontrak")
public class KontrakController extends LocaleController<RekananSkKontrakVO> {

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

	@Autowired
	private KontrakService kontrakService;

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

	}

	@RequestMapping(value = "/save-kontrak-detail/", method = POST, produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveKontrakDetail(@Valid @RequestBody RekananSkKontrakDetailVO vo,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Map<String, Object> result = kontrakService.saveKontrakDetail(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 saveKontrakDetail", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when saveKontrakDetail", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/list-kontrak")
	@ResponseBody
	public Map<String, Object> listkontrak(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "namaKontrak") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "desc") String dir,
			@RequestParam(value = "noKontrak", required = false) String noKontrak,
			@RequestParam(value = "rekananId", required = false) Integer rekananId,
			@RequestParam(value = "namaKontrak", required = false) String namaKontrak) {
        return kontrakService.listKontrak(page, limit, sort, dir, noKontrak, rekananId,
                namaKontrak);
	}

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

}
