package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.MessageResource;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dto.JadwalSeleksiDto;
import com.jasamedika.medifirst2000.dto.TerimaBerkasLamaranDto;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.RekrutmenService;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jasamedika.medifirst2000.core.web.WebConstants.HttpHeaderInfo.LABEL_SUCCESS;
import static com.jasamedika.medifirst2000.util.DateUtil.toDate;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/rekrutmen")
public class RekrutmenController extends LocaleController<PegawaiVO> {

	@Autowired
	private RekrutmenService rekrutmenService;

	@RequestMapping(value = "/get-all-daftar-pelamar", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAllPenerimaanLinen(
			@RequestParam(value = "startPeriode", required = false) String startPeriode,
			@RequestParam(value = "endPeriode", required = false) String endPeriode, HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getDaftarPelamar(toDate(startPeriode), toDate(endPeriode));
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-pns-nonpns", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPNSNonPNS(HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getPNSNonPNS();
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/generate-no-planning", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> generateStrukPlanning(HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.getGenerateNoStrukPlanning();
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/save-terima-berkas-lamaran", method = POST, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveTerimaBerkasLamaran(@RequestBody TerimaBerkasLamaranDto dto,
			HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.saveBerkasLamaran(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-jenis-test", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getJenisTest(HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getProdukJenisTest();
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-all-ruangan", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAllRuangan(HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getAllRuangan();
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-petugas-sdm", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPetugasSdm(HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getPetugasSdm();
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/save-jadwal-seleksi", method = POST, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveJadwalSeleksi(@RequestBody JadwalSeleksiDto dto,
			HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.saveJadwalSeleksi(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-jadwal-seleksi", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getJadwalSeleksi(
			@RequestParam(value = "periodeLowongan", required = false) String periodeLowongan,
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.listJadwalSeleksi(periodeLowongan);
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-pelaksana-pelamar-by-jenis-test-periode", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPelaksanaSeleksi(
			@RequestParam(value = "jenisTestId", required = false) Integer jenisTestId,
			@RequestParam(value = "periode", required = false) String periode, HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getDaftarPelamarPelaksanaSeleksi(periode, jenisTestId);
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/save-pelaksana-seleksi", method = POST, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> savePelaksanaanSeleksi(@RequestBody JadwalSeleksiDto dto,
			HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.savePelaksanaanSeleksi(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-pelaksana-seleksi", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPelaksanaSeleksi(
			@RequestParam(value = "periodeLowongan", required = false) String periodeLowongan,
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.listPelaksanaSeleksi(periodeLowongan);
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-pelaksana-seleksi-by-no-rec-struk-planning", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPelaksanaSeleksiByNoRecStrukPlanning(
			@RequestParam(value = "noRecStrukPlanning") String noRecStrukPlanning, HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService
				.listPelaksanaSeleksiByNoRecStrukPlanning(noRecStrukPlanning);
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-inputnilai-pelamar-by-jenis-test-periode", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPelamarInputNilaia(
			@RequestParam(value = "jenisTestId", required = false) Integer jenisTestId,
			@RequestParam(value = "periode", required = false) String periode, HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getDaftarPelamarInputNilai(periode, jenisTestId);
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/save-nilai-seleksi", method = POST, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveInputNilai(@RequestBody JadwalSeleksiDto dto,
			HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.saveNilaiSeleksi(dto);
		if (!result.isEmpty()) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-download-dokumen", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDownloadDokumen(@RequestParam(value = "dokumenId") Integer dokumenId,
			HttpServletResponse response) {
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> dokumen = rekrutmenService.getDokumenById(dokumenId);
		String fileName = (String) dokumen.get("pathFile");
		String fileType = "application/pdf";
		response.setContentType(fileType);
		response.setHeader("Content-disposition", "attachment; filename=" + fileName);
		File my_file = new File(fileName);
		OutputStream out;
		try {
			out = response.getOutputStream();
			FileInputStream in = new FileInputStream(my_file);
			byte[] buffer = new byte[4096];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.flush();
		} catch (IOException e) {
			throw new ServiceVOException(e.getMessage());
		}
		mapHeaderMessage.put(LABEL_SUCCESS, "SUKSES DOWNLOAD");
		return getJsonResponse(result, OK, mapHeaderMessage);
	}

	@RequestMapping(value = "/get-jadwal-pelamar-by-jenis-test-periode", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getJadwalPelamar(
			@RequestParam(value = "jenisTestId", required = false) Integer jenisTestId,
			@RequestParam(value = "periode", required = false) String periode, HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getDaftarPelamarJadwalSeleksi(periode, jenisTestId);
		if (result != null) {
			mapHeaderMessage.put(LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return getJsonResponse(result, OK, mapHeaderMessage);
		}
		return getJsonHttpStatus(INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

}
