package com.jasamedika.medifirst2000.controller;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.JadwalSeleksiDto;
import com.jasamedika.medifirst2000.dto.RegistrasiPelamarDto;
import com.jasamedika.medifirst2000.dto.TerimaBerkasLamaranDto;
import com.jasamedika.medifirst2000.service.RekrutmenService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.PegawaiVO;

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

	@Autowired
	private RekrutmenService rekrutmenService;
	
	@RequestMapping(value="/get-jenis-kelamin", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getJenisKelamin(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getAllJenisKelamin();
		
		if (result!=null) {
			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-agama", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAgama(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getAllAgama();
		
		if (result!=null) {
			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-status-perkawinan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getStatusPerkawinan(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getAllStatusPerkawinan();
		
		if (result!=null) {
			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-posisi-lamar", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPosisiLamar(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getPosisiLamar();
		
		if (result!=null) {
			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-degree", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPendidikan(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getAllPendidikan();
		
		if (result!=null) {
			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-registrasi-pelamar", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> saveRegistrasiPelamar(
			@RequestBody RegistrasiPelamarDto dto,
			HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.saveRegistrasiPelamar(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-all-daftar-pelamar", method = RequestMethod.GET, produces = MediaType.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(DateUtil.toDate(startPeriode),
				DateUtil.toDate(endPeriode));
		
		if (result!=null) {
			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-pns-nonpns", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPNSNonPNS(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getPNSNonPNS();
		
		if (result!=null) {
			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="/generate-no-planning", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> generateStrukPlanning(
			HttpServletRequest request) {
		Map<String, Object> result = rekrutmenService.getGenerateNoStrukPlanning();
		
		if (result!=null) {
			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-berkas-lamaran", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAmbilBerkasLamaran(
			@RequestParam(value="kategoryPegawaiId",required=true) Integer kategoryPegawaiId,
			@RequestParam(value="jabatanId",required=true) Integer jabatanId,
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getBerkasLamaran(kategoryPegawaiId,jabatanId);
		
		if (result!=null) {
			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-terima-berkas-lamaran", method = RequestMethod.POST, produces = MediaType.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(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-jenis-test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getJenisTest(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getProdukJenisTest();
		
		if (result!=null) {
			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-all-ruangan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAllRuangan(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getAllRuangan();
		
		if (result!=null) {
			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-petugas-sdm", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPetugasSdm(
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getPetugasSdm();
		
		if (result!=null) {
			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-pelamars-lolos-administrasi", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getDaftarPelamarLolosAdministrasi(
			@RequestParam(value="startPeriode",required=false) String startPeriode,
			@RequestParam(value="endPeriode",required=false) String endPeriode,
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.getDaftarPelamar(DateUtil.toDate(startPeriode),
				DateUtil.toDate(endPeriode));
		
		if (result!=null) {
			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-jadwal-seleksi", method = RequestMethod.POST, produces = MediaType.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(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-seleksi", method = RequestMethod.GET, produces = MediaType.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(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-pelaksana-pelamar-by-jenis-test-periode", method = RequestMethod.GET, produces = MediaType.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(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-pelaksana-seleksi", method = RequestMethod.POST, produces = MediaType.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(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-pelaksana-seleksi", method = RequestMethod.GET, produces = MediaType.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(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-pelaksana-seleksi-by-no-rec-struk-planning", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getPelaksanaSeleksiByNoRecStrukPlanning(
			@RequestParam(value="noRecStrukPlanning",required=true) String noRecStrukPlanning,
			HttpServletRequest request) {
		List<Map<String, Object>> result = rekrutmenService.listPelaksanaSeleksiByNoRecStrukPlanning(noRecStrukPlanning);
		
		if (result!=null) {
			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-inputnilai-pelamar-by-jenis-test-periode", method = RequestMethod.GET, produces = MediaType.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(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-nilai-seleksi", method = RequestMethod.POST, produces = MediaType.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(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-daftar-hadir", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDaftarHadir(
			@RequestParam(value="noRecStrukPlanning",required=true) String noRecStrukPlanning,
			@RequestParam(value="jenisTestId",required=true) Integer jenisTestId,
			HttpServletRequest request) {
		JadwalSeleksiDto dto=new JadwalSeleksiDto();
		Map<String, Object> result = rekrutmenService.getDaftarHadirRekrutmen(noRecStrukPlanning,jenisTestId);
		
		if (result!=null) {
			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-download-dokumen", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, Object>> getDownloadDokumen(
			@RequestParam(value="dokumenId",required=true) Integer dokumenId,
			HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String,Object>();
		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) {
			e.printStackTrace();
		}

		if (result!=null) {
			mapHeaderMessage.put(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, "SUKSES DOWNLOAD");
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		}
		return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}
	
	@RequestMapping(value="/get-jadwal-pelamar-by-jenis-test-periode", method = RequestMethod.GET, produces = MediaType.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(WebConstants.HttpHeaderInfo.LABEL_SUCCESS, getMessage(MessageResource.LABEL_SUCCESS, request));
			return RestUtil.getJsonResponse(result, HttpStatus.OK, mapHeaderMessage);
		}
		return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
	}

}
