package com.jasamedika.medifirst2000.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
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.StrukPelayananDetail;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
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.InputNoBatchVO;
import com.jasamedika.medifirst2000.vo.PenerimaanBarangDetailVO;
import com.jasamedika.medifirst2000.vo.PenerimaanBarangVO;
import com.jasamedika.medifirst2000.vo.UploadExcelVO;

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("/penerimaan-barang")
public class PenerimaanBarangController extends LocaleController<PenerimaanBarangVO> {

	@Autowired
	private StrukPelayananService strukPelayananService;
	
	@Autowired
	private StrukPelayananDetailService strukPelayananDetailService;

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

	@RequestMapping(value = "/save-penerimaan-barang/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePenerimaanBarang(@Valid @RequestBody PenerimaanBarangVO vo,HttpServletRequest request,HttpServletResponse response) {
		
		try {
			Map<String, Object> result = strukPelayananService.savePenerimaanBarang(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 Penerimaan Barang", 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 Penerimaan Barang", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/upload-exel/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> upload(@Valid @RequestBody UploadExcelVO vo,HttpServletRequest request) throws IOException {
		try {
			Map<String,Object> result =  strukPelayananService.uploadExel(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 add Agama", 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 add Agama", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
		
	}
	
	@RequestMapping(value = "/save-penerimaan-no-bacth/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveNoBatch(@Valid @RequestBody  InputNoBatchVO vo,HttpServletRequest request,HttpServletResponse response) {
		
		try {
			Map<String, Object> result = strukPelayananService.saveNoBatch(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 No Batch", 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 No Batch", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}

	}
	
	@RequestMapping(value = "/get-detail-penerimaan/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<PenerimaanBarangDetailVO> findByNoRec(
			@RequestParam(value = "noRec", required = true) String noRec) {
		
		PenerimaanBarangDetailVO penerimaanBarangDetailVO = strukPelayananDetailService.findByNoRec(noRec);

		try {
			return RestUtil.getJsonResponse(penerimaanBarangDetailVO, HttpStatus.OK, mapHeaderMessage);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(null, HttpStatus.NOT_FOUND, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/get-detail-penerimaan-batch/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> findByNoRecBatch(
			@RequestParam(value = "noRec", required = true) String noRec) {
		
		Map<String,Object> penerimaanBarangDetailVO = strukPelayananDetailService.findByNoRecBatch(noRec);

		try {
			return RestUtil.getJsonResponse(penerimaanBarangDetailVO, HttpStatus.OK, mapHeaderMessage);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(null, HttpStatus.NOT_FOUND, mapHeaderMessage);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/grid-penerimaan-barang")
	@ResponseBody
	public Map<String,Object> gridPenerimaanBarang(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "noRec") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "tanggalTerimAwal", required = false) String tanggalTerimaAwal,
			@RequestParam(value = "tanggalTerimAhir", required = false) String tanggalTerimaAhir,
			@RequestParam(value = "nofaktur", required = false) String nofaktur,
			@RequestParam(value = "idProduk", required = false) Integer idProduk,
			@RequestParam(value = "supplierId", required = false) Integer supplierId) {
		
		Map<String, Object> resultPageMap = strukPelayananDetailService.gridPenerimaanBarang(page, limit, sort, dir, tanggalTerimaAwal,tanggalTerimaAhir,nofaktur,idProduk,supplierId);
		
		return resultPageMap;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list-penerimaan-barang")
	@ResponseBody
	public Map<String, Object> listPenerimaanBarang(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10000") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "asc") String dir,
			@RequestParam(value = "tanggalAwal", required = false) String tanggalAwal,
			@RequestParam(value = "tanggalAhir", required = false) String tanggalAhir,
			@RequestParam(value = "produkId", required = false) Integer produkId,
			@RequestParam(value = "jenisProdukId", required = false) Integer jenisProdukId,
			@RequestParam(value = "kelompokProdukId", required = false) Integer kelompokProdukId,
			@RequestParam(value = "noTerima", required = false) String noTerima) {

		Map<String, Object> resultPageMap = strukPelayananDetailService.listPenerimaanBarang(page, limit, sort, dir, tanggalAwal,tanggalAhir,produkId,jenisProdukId,kelompokProdukId,noTerima);

		return resultPageMap;
	}
	
	
	@RequestMapping(value = "/detail-penerimaan-barang/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> reportPenerimaanBarang(
			@RequestParam(value = "noRec", required = true) String noRec) {
		Map<String, Object> result = strukPelayananDetailService.detailPenerimaan(noRec);

		return result;
	}
	
	

}
