package com.jasamedika.medifirst2000.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.*;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
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.ResponseBody;
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.entities.DetailSpekAnggaran;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.entities.StrukPelayananDetail;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.security.model.AppPermission;
import com.jasamedika.medifirst2000.service.PemusnahanBarangService;
import com.jasamedika.medifirst2000.service.RekananService;
import com.jasamedika.medifirst2000.service.StrukOrderService;
import com.jasamedika.medifirst2000.service.StrukPelayananDetailService;
import com.jasamedika.medifirst2000.service.StrukPelayananService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.DetailAnggaranVO;
import com.jasamedika.medifirst2000.vo.DistribusiBarangInvestasiVO;
import com.jasamedika.medifirst2000.vo.PenerimaanBarangDetailVO;
import com.jasamedika.medifirst2000.vo.PenerimaanBarangVO;
import com.jasamedika.medifirst2000.vo.RekananVO;
import com.jasamedika.medifirst2000.vo.RequestBarangDariRuanganVO;
import com.jasamedika.medifirst2000.vo.StrukHistoriVO;
import com.jasamedika.medifirst2000.vo.StrukKirimVO;
import com.jasamedika.medifirst2000.vo.StrukOrderVO;

import net.kaczmarzyk.spring.data.jpa.domain.DateBetween;
import net.kaczmarzyk.spring.data.jpa.domain.Equal;
import net.kaczmarzyk.spring.data.jpa.domain.Like;
import net.kaczmarzyk.spring.data.jpa.domain.Null;
import net.kaczmarzyk.spring.data.jpa.web.annotation.And;
import net.kaczmarzyk.spring.data.jpa.web.annotation.Spec;

@RestController
@RequestMapping("/rekanan")
public class RekananController extends LocaleController{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RekananController.class);
	
	@Autowired
	private RekananService rekananService;
	
	@RequestMapping(value = "/save-rekanan/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRekanan(@Valid @RequestBody RekananVO vo,HttpServletRequest request,HttpServletResponse response) {
		
		try {
			Map<String, Object> result = rekananService.saveRekanan(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) {
			LOGGER.error("Got exception {} when save Rekanan", e.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when save Rekanan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-rekanan")
	@ResponseBody
	public Map<String,Object>  listRekanan(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "tglRegisterAset") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "desc") String dir,
			@RequestParam(value = "rekananId", required = false) Integer rekananId,
			@RequestParam(value = "jenisRekananId", required = false) Integer jenisRekananId) {
		
		Map<String, Object> resultPageMap = rekananService.listRekanan(page, limit, sort, dir, rekananId,jenisRekananId);
		
		return resultPageMap;
	}
	
	

}
