package com.jasamedika.medifirst2000.controller;

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

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

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.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.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.core.web.WebConstants;
import com.jasamedika.medifirst2000.dto.FileUploadExcelDto;
import com.jasamedika.medifirst2000.dto.OrderPelayananGiziDto;
import com.jasamedika.medifirst2000.dto.PayRollDto;
import com.jasamedika.medifirst2000.dto.PayRollUangMakanDto;
import com.jasamedika.medifirst2000.service.PayRollPegawaiService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.StrukHistoriVO;

@RestController
@RequestMapping(value="/pay-roll")
public class PayRollPegawaiController extends LocaleController<StrukHistoriVO> {
	
	@Autowired
	private PayRollPegawaiService payRollPegawaiService;

	@RequestMapping(value="/get-pegawai-blu-pkwt", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> prosesPegawaiBluPkwt(
			@RequestParam(value="idKategoryPegawai", required = true) Integer idKategoryPegawai,
			@RequestParam(value="idJenisGaji" , required = true) Integer idJenisGaji,
			@RequestParam(value="periode", required = true) String periode,
			@RequestParam(value="tglHistori", required = false) String tglHistori,
			HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.prosesPegawaiBluPkwt(idKategoryPegawai,idJenisGaji,periode,DateUtil.toDate(tglHistori));
		
		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="/save-pegawai-blu-pkwt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PayRollDto>> savePegawaiByStatusPegawai(@RequestBody List<PayRollDto> dtos,
			HttpServletRequest request) {
		List<PayRollDto> result = payRollPegawaiService.save(dtos);
		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="/find-status-pegawai-blu-pkwt", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> findPegawaiBluPkwt(HttpServletRequest request) {
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(9);
		ids.add(10);
		List<Map<String, Object>> result = payRollPegawaiService.findDetailKategoryPegawaiBluPkwt(ids);
		
		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="/find-jenis-gaji", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> findJenisGaji(HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.findAllJenisGaji();
		
		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-uang-makan-pegawai", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> prosesUangMakanPegawai(
			@RequestParam(value="idKategoryPegawai", required = true) Integer idKategoryPegawai,
			@RequestParam(value="idJenisGaji" , required = true) Integer idJenisGaji,
			@RequestParam(value="periode", required = true) String periode,
			@RequestParam(value="tglHistori", required = false) String tglHistori,
			HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.prosesUangMakanPegawai(idKategoryPegawai,idJenisGaji,periode,"uangmakan",DateUtil.toDate(tglHistori));
		
		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="/find-status-pegawai-uang-makan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> findPegawaiUangMakan(HttpServletRequest request) {
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(9);
		ids.add(10);
		ids.add(18);
		ids.add(19);
		List<Map<String, Object>> result = payRollPegawaiService.findDetailKategoryPegawaiBluPkwt(ids);
		
		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="/save-pegawai-uang-makan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PayRollUangMakanDto>> savePegawaiUangMakan(@RequestBody List<PayRollUangMakanDto> dtos,
			HttpServletRequest request) {
		List<PayRollUangMakanDto> result = payRollPegawaiService.saveUangMakan(dtos,"uangmakan");
		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="/import-uang-makan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PayRollUangMakanDto>> importUangMakan(
			@RequestParam(value="idKategoryPegawai", required = false) Integer idKategoryPegawai,
			@RequestParam(value="idJenisGaji" , required = false) Integer idJenisGaji,
			@RequestParam(value="periode", required = false) String periode,
			@RequestParam(value="file",required = false) String file, 
			@RequestParam(value="tglHistori", required = false) String tglHistori,@RequestBody FileUploadExcelDto dto,
			HttpServletRequest request) {
		List<PayRollUangMakanDto> result = payRollPegawaiService.importUangMakan(idKategoryPegawai,idJenisGaji,periode,file,DateUtil.toDate(tglHistori),dto);
		if (result != null) {
			if (!result.isEmpty()) {
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
				return RestUtil.getJsonResponse(result, HttpStatus.CREATED, mapHeaderMessage);
			} else {
				mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_ERROR, "Processing data error returns empty data");
				return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
			}
		} else {
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		}
		
	}

	@RequestMapping(value="/find-status-pegawai-harianLepas", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> findPegawaiHarianLepas(HttpServletRequest request) {
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(13);
		List<Map<String, Object>> result = payRollPegawaiService.findDetailKategoryPegawaiBluPkwt(ids);
		
		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-harian-lepas-pegawai", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> prosesPegawaiHarianLepas(
			@RequestParam(value="idKategoryPegawai", required = true) Integer idKategoryPegawai,
			@RequestParam(value="idJenisGaji" , required = true) Integer idJenisGaji,
			@RequestParam(value="periode", required = true) String periode,
			@RequestParam(value="tglHistori", required = false) String tglHistori,
			HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.prosesUangMakanPegawai(idKategoryPegawai,idJenisGaji,periode,"harianlepas",DateUtil.toDate(tglHistori));
		
		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="/save-pegawai-harian-lepas", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PayRollUangMakanDto>> saveHarianLepas(@RequestBody List<PayRollUangMakanDto> dtos,
			HttpServletRequest request) {
		List<PayRollUangMakanDto> result = payRollPegawaiService.saveUangMakan(dtos,"harianlepas");
		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="/find-status-pegawai-pkwt", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> findPegawaiDokterPsikologiPkwt(HttpServletRequest request) {
		List<Integer> ids=new ArrayList<Integer>();
		ids.add(10);
		List<Map<String, Object>> result = payRollPegawaiService.findDetailKategoryPegawaiBluPkwt(ids);
		
		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-dokter-psikologi-pkwt", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> prosesDokterPsikologiPkwt(
			@RequestParam(value="idKategoryPegawai", required = true) Integer idKategoryPegawai,
			@RequestParam(value="idJenisGaji" , required = true) Integer idJenisGaji,
			@RequestParam(value="periode", required = true) String periode,
			@RequestParam(value="tglHistori", required = false) String tglHistori,
			HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.prosesDokterPsikologiPkwt(idKategoryPegawai,idJenisGaji,periode,DateUtil.toDate(tglHistori));
		
		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="/save-dokter-psikologi-pkwt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PayRollUangMakanDto>> saveDokterPsikologiPkwt(@RequestBody List<PayRollUangMakanDto> dtos,
			HttpServletRequest request) {
		List<PayRollUangMakanDto> result = payRollPegawaiService.saveUangMakan(dtos,"tunjanganlayanan");
		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="/find-rekanan-penjamin-pasien", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> findRekananPenjaminPasien(HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.findRekananPenjaminPasien();
		
		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="/find-umr", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> findUmr(
			@RequestParam(value="periode", required = true) String periode,
			HttpServletRequest request) {
		Map<String, Object> result = payRollPegawaiService.findUmr(periode);
		
		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-premi-asuransi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> prosesPremiAsuransi(
			@RequestParam(value="idJenisGaji" , required = true) Integer idJenisGaji,
			@RequestParam(value="periode", required = true) String periode,
			@RequestParam(value="idRekanan" , required = true) Integer idRekanan,
			@RequestParam(value="tglHistori", required = false) String tglHistori,
			HttpServletRequest request) {
		List<Map<String, Object>> result = payRollPegawaiService.prosesPremiAsuransi(idJenisGaji,periode,idRekanan,DateUtil.toDate(tglHistori));
		
		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="/save-premi-asuransi", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<PayRollUangMakanDto>> savePremiAsuransi(@RequestBody List<PayRollUangMakanDto> dtos,
			HttpServletRequest request) {
		List<PayRollUangMakanDto> result = payRollPegawaiService.savePremiAsuransi(dtos);
		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);
	}

}
