package com.jasamedika.medifirst2000.controller;

import java.util.HashMap;
import java.util.List;
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.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.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.MapRuanganToAkomodasiService;
import com.jasamedika.medifirst2000.service.RuanganService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.MapRuanganToAkomodasiVO;

@RestController
@RequestMapping("/akomodasi")
public class AkomodasiController extends LocaleController<MapRuanganToAkomodasiVO> {

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

	@Autowired
	private MapRuanganToAkomodasiService mapRuanganToAkomodasiService;

	@Autowired
	private RuanganService ruanganService;

	@RequestMapping(value = "/save-mapping", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveMappingAkomodasi(HttpServletRequest request,
			@Valid @RequestBody MapRuanganToAkomodasiVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			MapRuanganToAkomodasiVO resultVo = new MapRuanganToAkomodasiVO();
			if (CommonUtil.isNotNullOrEmpty(vo.getId())) {
				resultVo = mapRuanganToAkomodasiService.update(vo);
			} else {
				resultVo = mapRuanganToAkomodasiService.add(vo);
			}
			result.put("data", resultVo);
			if (CommonUtil.isNotNullOrEmpty(result)) {
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
						getMessage(MessageResource.LABEL_SUCCESS, request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
			} else {
				return RestUtil.getJsonHttptatus(HttpStatus.NOT_ACCEPTABLE);
			}
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when add mapping akomodasi", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when add mapping akomodasi", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/delete-mapping", method = RequestMethod.DELETE)
	public ResponseEntity<String> deleteMappingAkomodasi(HttpServletRequest request, @RequestParam("id") Integer id) {
		try {
			Boolean result = mapRuanganToAkomodasiService.delete(id);
			if (result) {
				return RestUtil.getJsonResponse("Mapping is deleted successsfully", HttpStatus.OK);
			} else {
				return RestUtil.getJsonHttptatus(HttpStatus.NOT_ACCEPTABLE);
			}
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when delete mapping akomodasi", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when delete mapping akomodasi", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-mapping", method = RequestMethod.GET)
	public ResponseEntity<List<MapRuanganToAkomodasiVO>> getAllMappingAkomodasi(HttpServletRequest request) {
		try {
			List<MapRuanganToAkomodasiVO> result = mapRuanganToAkomodasiService.getAllActiveAkomodasi();
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when get all mapping akomodasi", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get all mapping akomodasi", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-mapping", method = RequestMethod.GET)
	public ResponseEntity<MapRuanganToAkomodasiVO> getMappingAkomodasi(HttpServletRequest request,
			@RequestParam("id") Integer id) {
		try {
			MapRuanganToAkomodasiVO result = mapRuanganToAkomodasiService.findById(id);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		} catch (ServiceVOException sve) {
			LOGGER.error("Got exception {} when get all mapping akomodasi", sve.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, sve.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		} catch (JpaSystemException jse) {
			LOGGER.error("Got exception {} when get all mapping akomodasi", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-kamar-by-ruangan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getKamarByRuangan(HttpServletRequest request,
			@RequestParam(value = "idRuangan", required = true) Integer idRuangan) {
		List<Map<String, Object>> result = null;
		try {
			result = ruanganService.findKamarByRuangan(idRuangan);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/get-akomodasi-ruangan-by-mapping-produk", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAkomodasiRuanganByMappingProduk(HttpServletRequest request,
			@RequestParam(value = "idRuangan", required = true) Integer idRuangan) {
		List<Map<String, Object>> result = null;
		try {
			result = mapRuanganToAkomodasiService.getAkomodasiRuanganByMapProduk(idRuangan);
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,
					getMessage(MessageResource.LABEL_SUCCESS, request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return RestUtil.getJsonResponse(result, HttpStatus.OK);
	}

}
