package com.jasamedika.medifirst2000.controller;

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

import javax.servlet.http.HttpServletRequest;

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.dto.ItJadwalPerawatanDto;
import com.jasamedika.medifirst2000.dto.PenerimaanLinenDto;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ItJadwalPerawatanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;

@RestController
@RequestMapping("/it-jadwal-perawatan")
public class ItJadwalPerawatanController extends LocaleController{
	@Autowired
	private ItJadwalPerawatanService itJadwalPerawatanService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ItJadwalPerawatanController.class);
	
	@RequestMapping(value="/save-jadwal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveJadwalPerawatan(
			@RequestBody ItJadwalPerawatanDto dto, HttpServletRequest request) {
		Map<String, Object> result = this.itJadwalPerawatanService.saveJadwalPerawatan(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		}
		return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/get-jadwal-perawatan", 
			method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getJadwalPerawatanIt(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "take", required = false, defaultValue = "10") Integer limit,
			@RequestParam(value = "sort", required = false, defaultValue = "tglJadwal") String sort,
			@RequestParam(value = "dir", required = false, defaultValue = "desc") String dir,
			@RequestParam(value = "statusPemeliharaan", required = false) Integer statusPemeliharaan,
			@RequestParam(value = "startDate", required = false) String startDate,
			@RequestParam(value = "endDate", required = false) String endDate
			) {
		String stsPemeliharaan = null;
		if(CommonUtil.isNotNullOrEmpty(statusPemeliharaan))
		switch (statusPemeliharaan) {
			case 0:
				stsPemeliharaan = "Belum Dikerjakan";
				break;
			case 1:
				stsPemeliharaan = "Sudah Dikerjakan";
				break;
			default :
				stsPemeliharaan = null;
				break;
				
		}
		Map<String, Object> result = this.itJadwalPerawatanService.getJadwalPerawatanAll(page,limit,sort,dir,startDate, endDate,stsPemeliharaan);
		return result;
	}
	
	@RequestMapping(value = "/get-jadwal-perawatan-by-norec", 
			method = RequestMethod.GET, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> getJadwalPerawatanByNoRec(
			@RequestParam(value = "noRec") String noRec) {
		Map<String, Object> result = this.itJadwalPerawatanService.getJadwalPerawatanbyNoRec(noRec);
		return result;
	}
	
	@RequestMapping(value="/save-realisasi-jadwal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRealisasiJadwalPerawatan(
			@RequestBody ItJadwalPerawatanDto dto, HttpServletRequest request) {
		Map<String, Object> result = this.itJadwalPerawatanService.saveRealisasiJadwalPerawatan(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		}
		return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}
	
	@RequestMapping(value = "/delete-jadwal-perawatan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> deleteJadwalPerawatanIT(
			@RequestParam(value = "noRec", required = true) String noRec, 
			HttpServletRequest request) {
		try {
			Boolean status = itJadwalPerawatanService.deleteJadwalPerawatan(noRec);
			if (status){
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS,getMessage(MessageResource.LABEL_SUCCESS,request));
				return RestUtil.getJsonResponse("noRec : "+noRec, HttpStatus.OK);
			} else{
				return RestUtil.getJsonResponse("noRec : null", HttpStatus.BAD_REQUEST);
			} 
		} catch (ServiceVOException e) {
			LOGGER.error("Got exception {} when delete-jadwal-perawatan", 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 delete-jadwal-perawatan", jse.getMessage());
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, jse.getMessage());
			return RestUtil.getJsonHttptatus(HttpStatus.CONFLICT, mapHeaderMessage);
		}
	}

}
