package com.jasamedika.medifirst2000.controller;

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

import javax.servlet.http.HttpServletRequest;
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.data.jpa.domain.Specifications;
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.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.AdvokasiHukumMedicolegal;
import com.jasamedika.medifirst2000.entities.DataPerusahaanYangBekerjaSama;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DataPerusahaanYangBekerjaSamaService;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.DataPerusahaanYangBekerjaSamaVO;

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;

@RestController
@RequestMapping("/data-perusahaan-yang-bekerjasama")
public class DataPerusahaanYangBekerjaSamaController extends LocaleController<DataPerusahaanYangBekerjaSamaVO> {
	
	@Autowired
	private DataPerusahaanYangBekerjaSamaService dataPerusahaanYangBekerjaSamaService;
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(DataPerusahaanYangBekerjaSamaController.class);
	
	@RequestMapping(value = "/save-data-perusahaan-yang-bekerjasama/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> addVO(@Valid @RequestBody DataPerusahaanYangBekerjaSamaVO vo, HttpServletRequest request) {
		
		try {
			Map<String,Object> result=dataPerusahaanYangBekerjaSamaService.addDataPerusahaanYangBekerjaSama(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 DataPerusahaanYangBekerjaSamaService", 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 DataPerusahaanYangBekerjaSamaService", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/update-data-perusahaan-yang-bekerjasama/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> updateVO(@Valid @RequestBody DataPerusahaanYangBekerjaSamaVO vo, HttpServletRequest request) {
		
		try {
			Map<String,Object> result=dataPerusahaanYangBekerjaSamaService.addDataPerusahaanYangBekerjaSama(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 update DataPerusahaanYangBekerjaSamaService", 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 update DataPerusahaanYangBekerjaSamaService", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/find-all-data-perusahaan-yang-bekerjasama/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> getAllVO(
			@RequestParam(value="jenisRekananId",required=false,defaultValue="6") Integer jenisRekananId,
			@RequestParam(value="status",required=false,defaultValue="1") Integer status,
			HttpServletRequest request) {
		
		try {
			Map<String,Object> result=dataPerusahaanYangBekerjaSamaService.findAllDataPerusahaanYangBekerjaSama(jenisRekananId,status);
			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 get DataPerusahaanYangBekerjaSamaService", 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 get DataPerusahaanYangBekerjaSamaService", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
					jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
					mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/find-all-data-perusahaan-yang-bekerjasama-2/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String,Object>> getAllVO2(
			@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 = "desc") String dir,
			@Spec(path = "rekanan.jenisRekanan.id", params = "idJenisRekanan", spec = Equal.class) Specification<DataPerusahaanYangBekerjaSama> jenisRekanan,HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String, Object>();
		
		Specification<DataPerusahaanYangBekerjaSama> spec = jenisRekanan;
		result = dataPerusahaanYangBekerjaSamaService.findAll(page, take, sort, dir, spec);
		return constructListPageResult(result);
//		try {
//			Map<String,Object> result=dataPerusahaanYangBekerjaSamaService.findAllDataPerusahaanYangBekerjaSama();
//			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 get DataPerusahaanYangBekerjaSamaService", 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 get DataPerusahaanYangBekerjaSamaService", jse.getMessage());
//			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE,
//					jse.getMessage());
//			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT,
//					mapHeaderMessage);
//		}
	}
	@RequestMapping(value = "/get-perusanaan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPerusahaan(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getPerusahaan();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-asuransi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getAsuransi(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getAsuransi();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-rumah-sakit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getRumahSakit(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getRumahSakit();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-yayasan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getYayasan(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getYayasan();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-perusahaan-yang-bekerja-sama/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getPerusahaanYangBekerjaSama(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getPerusahaanYangBekerjaSama();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-asuransi-yang-bekerja-sama/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getAsuransiYangBekerjaSama(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getAsuransiYangBekerjaSama();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-rumah-sakit-yang-bekerja-sama/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getRumahSakitYangBekerjaSama(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getRumahSakitYangBekerjaSama();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/get-yayasan-yang-bekerja-sama/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getYayasanYangBekerjaSama(HttpServletRequest request) {
		Map<String, Object> result = null;
		try{
			result = dataPerusahaanYangBekerjaSamaService.getYayasanYangBekerjaSama();
			Boolean dataFound=new Boolean((boolean) result.get("dataFound"));
			
			if(dataFound){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request ));
			}
			else{
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR,getMessage(MessageResource.LABEL_ERROR,request ));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}
	
}
