package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.DetailKajianEvaluasi;
import com.jasamedika.medifirst2000.entities.KajianEvaluasi;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DetailKajianEvaluasiService;
import com.jasamedika.medifirst2000.service.KajianEvaluasiService;
import com.jasamedika.medifirst2000.vo.DetailKajianEvaluasiVO;
import com.jasamedika.medifirst2000.vo.KajianEvaluasiVO;
import net.kaczmarzyk.spring.data.jpa.domain.DateBetween;
import net.kaczmarzyk.spring.data.jpa.domain.Equal;
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.data.jpa.domain.Specifications;
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.validation.Valid;
import java.util.Collection;
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.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/kajian-evaluasi")
public class KajianEvaluasiController extends LocaleController<KajianEvaluasiVO> {

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

	@Autowired
	private KajianEvaluasiService evaluasiService;

	@Autowired
	private DetailKajianEvaluasiService detailService;

	@RequestMapping(value = "/find-all/", method = GET)
	public ResponseEntity<Collection<KajianEvaluasiVO>> findAll(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "20") Integer take,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@Spec(path = "tglUsulan", params = { "dateStart",
					"dateEnd" }, config = "yyyy-MM-dd", spec = DateBetween.class) Specification<KajianEvaluasi> tglUsulan,
			@Spec(path = "noUsulan", params = "noUsulan", spec = Equal.class) Specification<KajianEvaluasi> noUsulan,
			@Spec(path = "id", params = "id", spec = Equal.class) Specification<KajianEvaluasi> id) {
		Specification<KajianEvaluasi> spec = Specifications.where(tglUsulan).and(noUsulan).and(id);
		Map<String, Object> result = evaluasiService.findAll(page, take, sort, dir, spec);
		return constructListPageResult(result);
	}

	@RequestMapping(value = "/find-all-by-jabatan/", method = GET)
	public ResponseEntity<Collection<KajianEvaluasiVO>> findAllByJabatan(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "20") Integer take,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@Spec(path = "kajianEvaluasi.tglUsulan", params = { "dateStart",
					"dateEnd" }, config = "yyyy-MM-dd", spec = DateBetween.class) Specification<DetailKajianEvaluasi> tglUsulan,
			@Spec(path = "kajianEvaluasi.noUsulan", params = "noUsulan", spec = Equal.class) Specification<DetailKajianEvaluasi> noUsulan,
			@Spec(path = "tujuan.id", params = "jabatanId", spec = Equal.class) Specification<DetailKajianEvaluasi> tujuanId,
			@Spec(path = "id", params = "id", spec = Equal.class) Specification<DetailKajianEvaluasi> id) {
		Specification<DetailKajianEvaluasi> spec = Specifications.where(tglUsulan).and(tujuanId).and(noUsulan).and(id);
		Map<String, Object> result = detailService.findAll(page, take, sort, dir, spec);
		return constructListPageResult(result);
	}

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

	@RequestMapping(value = "/update-kajian/", method = POST)
	public ResponseEntity<Map<String, Object>> updateKajian(@Valid @RequestBody List<DetailKajianEvaluasiVO> vos,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = detailService.updateKajian(vos);
			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 updateKajian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when updateKajian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/delete/", method = POST)
	public ResponseEntity<Map<String, Object>> deleteKajian(@Valid @RequestBody List<KajianEvaluasiVO> vos,
			HttpServletRequest request) {
		try {
			Map<String, Object> result = evaluasiService.deleteKajian(vos);
			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 deleteKajian", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, e.getMessage());
			return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got JpaSystemException {} when deleteKajian", jse.getMessage());
			addHeaderMessage(ERROR_MESSAGE, jse.getMessage());
			return getJsonHttpStatus(CONFLICT, mapHeaderMessage);
		}
	}

}
