package com.jasamedika.medifirst2000.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jasamedika.medifirst2000.controller.base.IBaseRestController;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dao.KelasDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.dto.BukuRegisterMasukDto;
import com.jasamedika.medifirst2000.service.ReportingPelayananRekamMedisService;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.RegistrasiPelayananVO;

/**
 * Controller class for Registrasi Pasien Business
 * 
 */
@RestController
@RequestMapping("/cetakan")
public class CetakanController extends LocaleController<RegistrasiPelayananVO>
		implements IBaseRestController<RegistrasiPelayananVO> {
	@Autowired
	private ReportingPelayananRekamMedisService reportingPelayananRekamMedisService;
	
	@Autowired
	private RuanganDao ruanganDao;
	
	@Autowired
	private KelasDao kelasDao;

	private String formatBulan(Integer bulan){
		String bulanString="";
		switch(bulan){
		case 1 :
			bulanString="januari";
			break;
		case 2 :
			bulanString="februari";
			break;
		case 3 :
			bulanString="maret";
			break;
		case 4 :
			bulanString="april";
			break;
		case 5 :
			bulanString="mei";
			break;
		case 6 :
			bulanString="juni";
			break;
		case 7 :
			bulanString="juli";
			break;
		case 8 :
			bulanString="agustus";
			break;
		case 9 :
			bulanString="september";
			break;
		case 10 :
			bulanString="oktober";
			break;
		case 11 :
			bulanString="november";
			break;
		case 12 :
			bulanString="desember";
			break;			
		}
		return bulanString;
	}
	
	@RequestMapping("/lapBukuRegisterMasukRi")
	public ModelAndView generateLapBukuRegisterMasukRi(ModelAndView m,
			@RequestParam(value = "format", required = false) String format,
			@RequestParam(value = "startDate", required = true) String startDate,
			@RequestParam(value = "endDate", required = true) String endDate,
			@RequestParam(value = "ruangan", required = true) Integer ruangan) {
		Map<String, Object> map = new HashMap<String, Object>();
		String month=(formatBulan(Integer.valueOf(new SimpleDateFormat("MM").format(new Date()))));
		String monthFirstCaplock=Character.toString(month.charAt(0)).toUpperCase()+month.substring(1);
		String monthAllCaplock=month.toUpperCase();
		List<BukuRegisterMasukDto> bukuRegisterMasukList = new ArrayList<BukuRegisterMasukDto>();
		
		List<Object[]> list = reportingPelayananRekamMedisService.findAntrianPasienDiPeriksaByRuanganPeriode(ruangan,startDate,endDate);
		for (Object[] data : list) {
			String noPendaftaran=(String) data[0];
			Date tglPendaftaran=(Date) data[1];
			String noCm=(String) data[2];
			String namaPasien=(String) data[3];
			String jenisKelamin=(String) data[4];
			String jenisPasien=(String) data[5];
			String statusPasienRs=(String) data[6];
			String statusPasien=(String) data[7];
			String caraMasuk=(String) data[8];
			String ruanganPerujuk=(String) data[9];
			String statusKeluar=(String) data[10];
			String statusPulang=(String) data[11];
			String kondisiPulang=(String) data[12];
			Date tglKeluar=(Date) data[13];
			String noKamar=(String) data[14];
			String noBed=(String) data[15];
			String kelasPelayanan=(String) data[16];
			String kasusPenyakit=(String) data[17];			
			BukuRegisterMasukDto dto = new BukuRegisterMasukDto();
			dto.setCaraMasuk(caraMasuk);
			dto.setJenisKelamin(jenisKelamin);
			dto.setJenisPasien(jenisPasien);
			dto.setKasusPenyakit(kasusPenyakit);
			dto.setKelasPelayanan(kelasPelayanan);
			dto.setKondisiPulang(kondisiPulang);
			dto.setNoKamar(noKamar);
			dto.setNamaPasien(namaPasien);
			dto.setNoBed(noBed);
			dto.setNoCm(noCm);
			dto.setNoPendaftaran(noPendaftaran);
			dto.setRuanganPerujuk(ruanganPerujuk);
			dto.setStatusKeluar(statusKeluar);
			dto.setStatusPasien(statusPasien);
			dto.setStatusPasienRs(statusPasienRs);
			dto.setStatusPulang(statusPulang);
			dto.setTglKeluar(DateUtil.formatDate(tglKeluar));
			dto.setTglPendaftaran(DateUtil.formatDate(tglPendaftaran));
			bukuRegisterMasukList.add(dto);
		}
		
		m.addObject("dataSource", bukuRegisterMasukList);
		m.addObject("startDate", startDate);
		m.addObject("endDate", endDate);
		m.addObject("tanggalReport", (new SimpleDateFormat("dd").format(new Date()))+" "+
				monthFirstCaplock+" " + (new SimpleDateFormat("YYYY").format(new Date())));
		m.addObject("ruangan", ruanganDao.findOne(ruangan).getNamaRuangan());
		m.addObject("format", "pdf");

		if (format != null && !format.isEmpty()) {
			m.addObject("format", format);
		}

		return m;
	}

	@Override
	public ResponseEntity<Collection<RegistrasiPelayananVO>> getAllVOWithQueryString(HttpServletRequest request,
			Integer page, Integer limit, String sort, String dir) {
		return null;
	}

	@Override
	public ResponseEntity<RegistrasiPelayananVO> getVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<String> addVO(RegistrasiPelayananVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> editVO(RegistrasiPelayananVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> deleteVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<List<RegistrasiPelayananVO>> getAllVO() {
		return null;
	}

}
