package com.jasamedika.medifirst2000.service.impl;

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

import org.hibernate.mapping.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.AsuhanKeperawatanPeriOperasiHeaderDao;
import com.jasamedika.medifirst2000.dao.AsuhanKeperawatanPeriOperasiTransDao;
import com.jasamedika.medifirst2000.dao.InfusDetailDao;
import com.jasamedika.medifirst2000.dao.LapPembedahanDanInstruksiPascaBedahDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.AsuhanKeperawatanPeriOperasiHeader;
import com.jasamedika.medifirst2000.entities.AsuhanKeperawatanPeriOperasiTrans;
import com.jasamedika.medifirst2000.entities.AsuhanKeperawatanPeriOperatif;
import com.jasamedika.medifirst2000.entities.Infus;
import com.jasamedika.medifirst2000.entities.InfusDetail;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.LapPembedahanDanInstruksiPascaBedahService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.AntrianPasienDiPeriksaVO;
import com.jasamedika.medifirst2000.vo.AsuhanKeperawatanPeriOperasiHeaderVO;
import com.jasamedika.medifirst2000.vo.AsuhanKeperawatanPeriOperasiTransVO;
import com.jasamedika.medifirst2000.vo.AsuhanKeperawatanPeriOperatifVO;
import com.jasamedika.medifirst2000.vo.InfusDetailVO;
import com.jasamedika.medifirst2000.vo.InfusVO;
import com.jasamedika.medifirst2000.vo.PasienVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("LapPembedahanDanInstruksiPascaBedahService")
public class LapPembedahanDanInstruksiPascaBedahServiceImpl implements LapPembedahanDanInstruksiPascaBedahService {
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDaftarConverter;
	
	@Autowired
	private LapPembedahanDanInstruksiPascaBedahDao lapPembedahanDanInstruksiPascaBedahDao;
	
	@Autowired
	private InfusDetailDao infuseDetailDao; 
	
	@Autowired
	private AsuhanKeperawatanPeriOperasiTransDao asuhanKeperawatanPeriOperasiTransDao;
	
	@Autowired
	private AsuhanKeperawatanPeriOperasiHeaderDao asuhanKeperawatanPeriOperasiHeaderDao;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<PasienVO, Pasien> pasienConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<AsuhanKeperawatanPeriOperatifVO, AsuhanKeperawatanPeriOperatif> asuhanKeperawatanPeriOperatifConverter;
	
	@Autowired
	private BaseConverterImpl<AsuhanKeperawatanPeriOperasiTransVO, AsuhanKeperawatanPeriOperasiTrans> asuhanDetaiConverter;
	
	@Autowired
	private BaseConverterImpl<InfusDetailVO, InfusDetail> infusDetailConverter;
	
	@Autowired
	private BaseConverterImpl<AsuhanKeperawatanPeriOperasiHeaderVO, AsuhanKeperawatanPeriOperasiHeader> asuhanHedaerConverter;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;
	
	@Autowired
	private BaseConverterImpl<InfusVO, Infus> infusConverter;

	
	/*@Override
	public Map<String, Object> getLapPembedahan() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> listData = new ArrayList<>();
		
		try {
			Map<String, Object> lappembedahanMap = new HashMap<>();
			List<Map<String, Object>> lappembedahanList = new ArrayList<>();
			
			List<Map<String, Object>> lappembedahan = this.lapPembedahanDanInstruksiPascaBedahDao.
					getLapPembedahanHeader();
			for(Map<String, Object> data : lappembedahan) {
				Map<String, Object> tempMap = new HashMap<>();
				
				List<Map<String, Object>> lappembedahandetail = this.lapPembedahanDanInstruksiPascaBedahDao.
						getLapPembedahanDetail((Integer) data.get("id"));
				
				tempMap.put("id", (Integer) data.get("id"));
				tempMap.put("namaExternal", (String) data.get("namaExternal"));
				tempMap.put("detail",  lappembedahandetail);
				lappembedahanList.add(tempMap);
			}
				
			lappembedahanMap.put("header", "Laporan Pembedahaan");
			lappembedahanMap.put("detail", lappembedahanList);
			listData.add(lappembedahanMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		result.put("loadData", listData);
		return result;
	}

	@Override
	public Map<String, Object> getInstruksiPascaBedah() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = new ArrayList<>();
		
		try {
			Map<String, Object> insruksiMap = new HashMap<>();
			List<Map<String, Object>> instruksiList = new ArrayList<>();
			
			List<Map<String, Object>> inturksiPascaBedah = this.lapPembedahanDanInstruksiPascaBedahDao.
					getInstruksiPascaBedahHeader();
			for(Map<String, Object> data : inturksiPascaBedah) {
				Map<String, Object> tempMap = new HashMap<>();
				
				List<Map<String, Object>> intruksidetail = this.lapPembedahanDanInstruksiPascaBedahDao.
						getInstruksiPascaBedahDetail((Integer) data.get("id"));
				
				tempMap.put("id", data.get("id"));
				tempMap.put("namaExternal", data.get("namaExternal"));
				tempMap.put("detail", intruksidetail);
				instruksiList.add(tempMap);
			}
			insruksiMap.put("header", "Instruksi Pasca Bedah");
			insruksiMap.put("detail", instruksiList);
			listData.add(insruksiMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		result.put("loadData", listData);
		
		return result;
	}
	*/

	@Override
	public Map<String, Object> saveLapInstruksiPascaBedahHeader(AsuhanKeperawatanPeriOperasiHeaderVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();

		String noTrans = noTrans();

		AsuhanKeperawatanPeriOperasiHeader asuhanHeader = new AsuhanKeperawatanPeriOperasiHeader();

		if (CommonUtil.isNotNullOrEmpty(vo.getNoTrans())) {
			asuhanHeader.setNoTrans(vo.getNoTrans());
		}else {
			asuhanHeader.setNoTrans(noTrans);
		}
		
		AntrianPasienDiPeriksa norec = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasienDaftar().getNoRec());
		asuhanHeader.setPasienDaftar(norec);
		asuhanHeader.setNoRec(vo.getNoRec());
		asuhanHeader.setStatusEnabled(vo.getStatusEnabled());
		asuhanHeader.setJamMasukOperasi(vo.getJamMasukOperasi());
		asuhanHeader.setOperasiMulai(vo.getOperasiMulai());
		asuhanHeader.setOperasiSelesai(vo.getOperasiSelesai());
		asuhanHeader.setTanggal(vo.getTanggal());
		asuhanHeader.setDiagnosaPreOperasi(vo.getDiagnosaPreOperasi());
		asuhanHeader.setDiagnosaPostOperasi(vo.getDiagnosaPostOperasi());
		asuhanHeader.setTindakan(vo.getTindakan());
		asuhanHeader.setJam(vo.getJam());
		asuhanHeader.setAnestesiologi(vo.getAnestesiologi());
		asuhanHeader.setPenataAnestesi(vo.getPenataAnestesi());
		
		//untuk operator bedah
		if(CommonUtil.isNotNullOrEmpty(vo.getDokterBedah())) {
			asuhanHeader.setDokterBedah(pegawaiConverter.transferVOToModel(vo.getDokterBedah(), new Pegawai()));
		}
		
		//untuk asisten operator
		if(CommonUtil.isNotNullOrEmpty(vo.getAsistenBedah())) {
			asuhanHeader.setAsistenBedah(pegawaiConverter.transferVOToModel(vo.getAsistenBedah(), new Pegawai()));
		}
		
		//untuk perawat instrumen
		if(CommonUtil.isNotNullOrEmpty(vo.getPerawatInstrumen1())) {
			asuhanHeader.setPerawatInstrumen1(pegawaiConverter.transferVOToModel(vo.getPerawatInstrumen1(), new Pegawai()));
		}
		
		//untuk penata anestesi
		if(CommonUtil.isNotNullOrEmpty(vo.getPerawatInstrument2())) {
			asuhanHeader.setPerawatInstrument2(pegawaiConverter.transferVOToModel(vo.getPerawatInstrument2(), new Pegawai()));
		}

		if (CommonUtil.isNotNullOrEmpty(asuhanHeader)) {
			AsuhanKeperawatanPeriOperasiHeader asuhanHeaderResult = this.asuhanKeperawatanPeriOperasiHeaderDao.save(asuhanHeader);
			result.put("noRec", asuhanHeaderResult.getNoRec());

			for (AsuhanKeperawatanPeriOperasiTransVO asuhandetail : vo.getAsuhanTrans()) {
				AsuhanKeperawatanPeriOperasiTrans asuhan = asuhanDetaiConverter.transferVOToModel(asuhandetail,
						new AsuhanKeperawatanPeriOperasiTrans());
				if(CommonUtil.isNotNullOrEmpty(asuhan.getAsuhankeperawatanHeaderId())) {
					asuhan.setAsuhankeperawatanHeaderId(asuhan.getAsuhankeperawatanHeaderId());
				} else {
					asuhan.setAsuhankeperawatanHeaderId(noTrans);
				}
				asuhan.setStatusEnabled(asuhandetail.getStatusEnabled());
				asuhan.setNoRec(asuhandetail.getNoRec());
				asuhan.setObjekDataMasalah(asuhanKeperawatanPeriOperatifConverter
						.transferVOToModel(asuhandetail.getObjekDataMasalah(), new AsuhanKeperawatanPeriOperatif()));
				asuhan.setFlagasuhan(asuhandetail.getFlagasuhan()); 
				asuhan.setFlagHeader(asuhandetail.getFlagHeader());
				asuhan.setKeterangan(asuhandetail.getKeterangan());
				asuhan.setUraian(asuhandetail.getUraian());
				AsuhanKeperawatanPeriOperasiTrans asuhanResult = this.asuhanKeperawatanPeriOperasiTransDao.save(asuhan);
				result.put("noRecDetail", asuhanResult.getNoRec());
			}
			
			for(InfusDetailVO infusDetail : vo.getInfusDetail()) {
				InfusDetail infus =infusDetailConverter.transferVOToModel(infusDetail, new InfusDetail());
				infus.setPembedahanDanInstruksiId(noTrans);
				infus.setNoRec(infusDetail.getNoRec());
				infus.setMacamInfus(infusConverter.transferVOToModel(infusDetail.getMacamInfus(), new Infus()));
				infus.setJumlah(infusDetail.getJumlah());
				infus.setTetesan(infusDetail.getJumlah());
				InfusDetail infusDetailResult =  this.infuseDetailDao.save(infus);
				result.put("noRecInfusDetail", infusDetailResult.getNoRec());
			}
			
		}

		return result;

	}

	@Override
	public String noTrans() {
		Long count = this.asuhanKeperawatanPeriOperasiHeaderDao.countNoTrans("PB");

		String monthNow = String.valueOf(DateUtil.getMonthFromDate(new Date()));
		String yearNow = String.valueOf(DateUtil.getYear(new Date())).substring(2, 4);
		String digit = null;

		count++;

		if (count.toString().length() == 1) {
			digit = "000";
		} else if (count.toString().length() == 2) {
			digit = "00";
		} else if (count.toString().length() == 3) {
			digit = "0";
		}
		if (monthNow.length() == 1)
			monthNow = "0" + monthNow;

		return "PB" + yearNow + monthNow + digit + count;
	}

	@Override
	public Map<String, Object> noDefault() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<String> list = this.asuhanKeperawatanPeriOperasiHeaderDao.noTrans("PB");
		
		if (CommonUtil.isNotNullOrEmpty(list)) {
			Integer data = Integer.parseInt(list.get(list.size() - 1).substring(2)) + 1;
			result.put("noTrans", noTrans());
		}
		else {
			result.put("noTrans", noTrans());
		}
		return result;
	}

	@Override
	public Map<String, Object> getListAsuhanByNoTrans(String notrans) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = new ArrayList<>();

		//laporan bedah
		try {
			Map<String, Object> dataMasalahTrans = new HashMap<>();

			List<Map<String, Object>> detailTrans = new ArrayList<>();
			List<Map<String, Object>> dataAsuhanDetail = this.lapPembedahanDanInstruksiPascaBedahDao.getListLaporanBedahTransByNoTrans(notrans);

			for (Map<String, Object> data : dataAsuhanDetail) {
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", data.get("noRec"));
				tempMap.put("noTrans", data.get("noTrans")); 
				tempMap.put("idParent", data.get("idParent"));
				tempMap.put("id", data.get("id"));
				tempMap.put("namaExternal", data.get("namaExternal"));
				tempMap.put("keterangan", data.get("keterangan"));
				detailTrans.add(tempMap);
			}
			dataMasalahTrans.put("header", "Laporan Pembedahaan");
			dataMasalahTrans.put("detail", detailTrans);
			listData.add(dataMasalahTrans);

		} catch (Exception e) {
			e.printStackTrace();
		}

		//pasca bedah
		try {
			Map<String, Object> dataMasalahTrans = new HashMap<>();

			List<Map<String, Object>> detailTrans = new ArrayList<>();
			List<Map<String, Object>> dataAsuhanDetail = this.lapPembedahanDanInstruksiPascaBedahDao.getListLapPascaBedahTransByNoTrans(notrans);

			for (Map<String, Object> data : dataAsuhanDetail) {
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", data.get("noRec"));
				tempMap.put("noTrans", data.get("noTrans"));
				tempMap.put("idParent", data.get("idParent"));
				tempMap.put("id", data.get("id"));
				tempMap.put("namaExternal", data.get("namaExternal"));
				tempMap.put("keterangan", data.get("keterangan"));
				detailTrans.add(tempMap);
			}
			dataMasalahTrans.put("header", "Instruksi Pasca Bedah");
			dataMasalahTrans.put("detail", detailTrans);
			listData.add(dataMasalahTrans);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//penggunaan infus
		try {
			Map<String, Object> infusTrans = new HashMap<>();

			List<Map<String, Object>> detailTrans = new ArrayList<>();
			List<Map<String, Object>> infusDetail = this.lapPembedahanDanInstruksiPascaBedahDao.getListLapInfusTransByNoTrans(notrans);

			for (Map<String, Object> data : infusDetail) {
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("noRec", (String)data.get("noRec"));
				tempMap.put("noTrans", (String)data.get("noTrans"));
				tempMap.put("id", (Integer)data.get("id"));
				tempMap.put("name", (String)data.get("nameInfus"));
				tempMap.put("jumlah", (String)data.get("jumlah"));
				tempMap.put("tetesan", (String)data.get("tetesan"));
				detailTrans.add(tempMap);
			}
			infusTrans.put("header", "Detail Infus");
			infusTrans.put("detail", detailTrans);
			listData.add(infusTrans);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Map<String, Object> dataHeader = this.asuhanKeperawatanPeriOperasiHeaderDao
				.getListAsuhanTransByNoTrans(notrans);
		if (CommonUtil.isNotNullOrEmpty(dataHeader)) {
			result.put("noTrans", dataHeader.get("noTrans"));
			result.put("tindakan", dataHeader.get("tindakan"));
			result.put("noRec", dataHeader.get("noRec"));
			result.put("diagnosaPostOperasi", dataHeader.get("diagnosaPostOperasi"));
			result.put("operasiSelesai", dataHeader.get("operasiSelesai"));
			result.put("operasiMulai", dataHeader.get("operasiMulai"));
			result.put("jamMasukOperasi", dataHeader.get("jamMasukOperasi"));
			result.put("tanggal", dataHeader.get("tanggal"));
			result.put("jam", dataHeader.get("jam"));
			result.put("idOperator", dataHeader.get("idDokterBedah")); //untuk operator bedah
			result.put("nameOperator", dataHeader.get("nameDokterBedah"));
			result.put("idAsistenOperator", dataHeader.get("idAsistenBedah")); //untuk asiste operator bedah
			result.put("nameAsistenOperator", dataHeader.get("nameAsistenBedah"));
			result.put("idPerawatInstrumen", dataHeader.get("idPerawatInstrumen1")); //untuk perawat instrumen
			result.put("namePerawatInstrumen", dataHeader.get("namePerawatInstrumen1"));
			result.put("idPenataAnestesi", dataHeader.get("idPerawatInstrumen2"));   //untuk penata anestesi
			result.put("namePenataAnestesi", dataHeader.get("namePerawatInstrumen2"));
			result.put("anestesiologi", dataHeader.get("anestesiologi"));
			result.put("penataAnestesi", dataHeader.get("penataAnestesi")); 
			result.put("diagnosaPreOperasi", dataHeader.get("diagnosaPreOperasi"));
			//result.put("nameOperatorAlatCanggih", dataHeader.get("nameOperatorAlatCanggih"));
			result.put("namaKelas", dataHeader.get("namaKelas"));
			result.put("namaRuangan", dataHeader.get("namaRuangan"));
			result.put("jenisKelamin", dataHeader.get("jenisKelamin"));
			result.put("namaPasien", dataHeader.get("namaPasien"));
			result.put("tglLahir", dataHeader.get("tglLahir"));
			result.put("noCm", dataHeader.get("noCm"));
			result.put("loadData", listData);
		}

		return result;
	}

	@Override
	public Map<String, Object> getPasienDaftarOperasi(String noRec) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = this.asuhanKeperawatanPeriOperasiHeaderDao.getPacaBedahByNoRecAntrian(noRec);

		if (CommonUtil.isNotNullOrEmpty(listData)) {
			result.put("listData", listData);
		}

		return result;
	}

	@Override
	public Map<String, Object> getMacamInfus() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData =  this.lapPembedahanDanInstruksiPascaBedahDao.getMacamInfus();
		
		if(CommonUtil.isNotNullOrEmpty(listData)) {
			result.put("listData", listData);
		}
		return result;
	}
	

}
