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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.AsuhanKeperawatanPeriOperasiHeaderDao;
import com.jasamedika.medifirst2000.dao.AsuhanKeperawatanPeriOperasiTransDao;
import com.jasamedika.medifirst2000.dao.IntraOperasiPerawatBedahDao;
import com.jasamedika.medifirst2000.dao.PostOperasiPerawatBedahDao;
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.Pasien;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PostOperasiPerawatBedah;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.PostOperasiPerawatBedahService;
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.PasienVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PostOperasiPerawatBedahVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("PostOperasiPerawatBedahService")
public class PostOperasiPerawatBedahServiceImpl implements PostOperasiPerawatBedahService {
	@Autowired
	private AsuhanKeperawatanPeriOperasiHeaderDao asuhanKeperawatanPeriOperasiHeaderDao;
	
	@Autowired
	private AsuhanKeperawatanPeriOperasiTransDao asuhanKeperawatanPeriOperasiTransDao;
	
	@Autowired
	private PostOperasiPerawatBedahDao postOperasiPerawatBedahDao;
	
	@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<PostOperasiPerawatBedahVO, PostOperasiPerawatBedah> postOperasiBedahConverter;
	
	@Autowired
	private BaseConverterImpl<AsuhanKeperawatanPeriOperasiHeaderVO, AsuhanKeperawatanPeriOperasiHeader> asuhanHedaerConverter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDaftarConverter;
	

	@Override
	public Map<String, Object> saveInteraOperasiPerawatBedah(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);
		}
		
		asuhanHeader.setPasienDaftar(antrianPasienDaftarConverter.transferVOToModel(vo.getPasienDaftar(), new AntrianPasienDiPeriksa()));
		asuhanHeader.setJamMasukOperasi(vo.getJamMasukOperasi());
		asuhanHeader.setStatusEnabled(vo.getStatusEnabled());
		asuhanHeader.setNoRec(vo.getNoRec());
		
		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());
				AsuhanKeperawatanPeriOperasiTrans asuhanResult = this.asuhanKeperawatanPeriOperasiTransDao.save(asuhan);
				result.put("noRecDetail", asuhanResult.getNoRec());
			}
			
			//set Post Oparasi tambahaan data
			PostOperasiPerawatBedah postOperasiPerawatBedah = postOperasiBedahConverter.transferVOToModel(vo.getPostOperasi(), new PostOperasiPerawatBedah());
			postOperasiPerawatBedah.setAsuhankeperawatanHeaderId(noTrans);
			postOperasiPerawatBedah.setNoRec(vo.getPostOperasi().getNoRec());
			postOperasiPerawatBedah.setNamaJaringan(vo.getPostOperasi().getNamaJaringan());
			postOperasiPerawatBedah.setJumlah(vo.getPostOperasi().getJumlah());
			postOperasiPerawatBedah.setPa(vo.getPostOperasi().getPa());
			postOperasiPerawatBedah.setKulturJaringan(vo.getPostOperasi().getKulturJaringan());
			postOperasiPerawatBedah.setSitologi(vo.getPostOperasi().getSitologi());
			postOperasiPerawatBedah.setDisiapkanOleh(vo.getPostOperasi().getDisiapkanOleh());
			postOperasiPerawatBedah.setPerawatBedah(pegawaiConverter.transferVOToModel(vo.getPostOperasi().getPerawatBedah(), new Pegawai()));
			PostOperasiPerawatBedah postOperasiResult = this.postOperasiPerawatBedahDao.save(postOperasiPerawatBedah);
			result.put("noRecPostOperasi", postOperasiResult.getNoRec());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getInteraOperasiPerwatanBedah(String notrans) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = new ArrayList<>();
		//start here Data & Masalah Keperawatan
		try {
			/*
			Map<String, Object> dataMasalahTrans = new HashMap<>();
			
			List<Map<String, Object>> detailTrans = new ArrayList<>();
			List<Map<String, Object>> dataAsuhanDetail = this.asuhanKeperawatanPeriOperasiHeaderDao.getListPostOperasiTransByDataMasalah(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", "Data & Masalah Keperawatan");
			dataMasalahTrans.put("detail", detailTrans);
			listData.add(dataMasalahTrans);
			*/
			//Sirkulasi
			Map<String, Object> dataKeperawatanMap = new HashMap<>();
			List<Map<String, Object>> dataKeperawatanList = new ArrayList<>();
			
			try {
				Map<String, Object> sirkulasiMap = new HashMap<>();
				List<Map<String, Object>> sirkulasiList = new ArrayList<>();
				
				List<Map<String, Object>>  sirkulasi = this.asuhanKeperawatanPeriOperasiHeaderDao.getListDataMasalahPostOperasiSirkulasi(notrans);
					for(Map<String, Object> data : sirkulasi) {
						if(CommonUtil.isNotNullOrEmpty(data)) {
							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"));
							sirkulasiList.add(tempMap);
						}
					}
					sirkulasiMap.put("header", "Sirkulasi");
					sirkulasiMap.put("id", 365);
					sirkulasiMap.put("detail", sirkulasiList);
					dataKeperawatanList.add(sirkulasiMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//Kondisi Kulit
			try {
				Map<String, Object> kulitMap = new HashMap<>();
				List<Map<String, Object>> kulitList = new ArrayList<>();
				List<Map<String, Object>> kulit = this.asuhanKeperawatanPeriOperasiHeaderDao.getListDataMasalahPostOperasiKondisiKulit(notrans);
				for(Map<String, Object> data : kulit){
					if(CommonUtil.isNotNullOrEmpty(data)) {
						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"));
						kulitList.add(tempMap);
					}
				}
				kulitMap.put("header", "Kondisi Kulit");
				kulitMap.put("id", 366);
				kulitMap.put("detail", kulitList);
				dataKeperawatanList.add(kulitMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//show data cairan drain
			try {
				Map<String, Object> cairanDrainMap = new HashMap<>();
				List<Map<String, Object>> cairanDrainList = new ArrayList<>();
				List<Map<String, Object>> cairannDrain = this.asuhanKeperawatanPeriOperasiHeaderDao.getListDataMasalahPostOperasiCairanDrain(notrans);
				for(Map<String, Object> data : cairannDrain) {
					if(CommonUtil.isNotNullOrEmpty(data)) {
						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"));
						cairanDrainList.add(tempMap);
					}
				}
				cairanDrainMap.put("header", "Cairan Drain");
				cairanDrainMap.put("id", 368);
				cairanDrainMap.put("detail", cairanDrainList);
				dataKeperawatanList.add(cairanDrainMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//show data ekstremitas
			try {
				Map<String, Object> ekstremitasMap = new HashMap<>();
				List<Map<String, Object>> ekstremitasList = new ArrayList<>();
				List<Map<String, Object>> ekstremitas = this.asuhanKeperawatanPeriOperasiHeaderDao.getListDataMasalahPostOperasiEkstremitas(notrans);
				for(Map<String, Object> data : ekstremitas) {
					if(CommonUtil.isNotNullOrEmpty(data)) {
						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"));
						ekstremitasList.add(tempMap);
					}
				}
				ekstremitasMap.put("header", "Ekstremitas");
				ekstremitasMap.put("id", 367);
				ekstremitasMap.put("detail", ekstremitasList);
				dataKeperawatanList.add(ekstremitasMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//show data diagnosa keperawatan
			try {
				Map<String, Object> diagnosakeperawatanMap = new HashMap<>();
				List<Map<String, Object>> diagnosakeperawatanList = new ArrayList<>();
				List<Map<String, Object>> diagnosakeperawatan = this.asuhanKeperawatanPeriOperasiHeaderDao.
						getListDataMasalahPostOperasiDiagnosaKeperawatan(notrans);
				for(Map<String, Object> data : diagnosakeperawatan) {
					if(CommonUtil.isNotNullOrEmpty(data)) {
						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"));
						diagnosakeperawatanList.add(tempMap);
					}
					
				}
				diagnosakeperawatanMap.put("header", "Diagnosa Keperawatan");
				diagnosakeperawatanMap.put("id", 369);
				diagnosakeperawatanMap.put("detail", diagnosakeperawatanList);
				dataKeperawatanList.add(diagnosakeperawatanMap);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			dataKeperawatanMap.put("header", "Data & Masalah Keperawatan");
			dataKeperawatanMap.put("detail", dataKeperawatanList);
			listData.add(dataKeperawatanMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//finish here Data & Masalah Keperawatan
		try {
			Map<String, Object> intervensiMap = new HashMap<>();
			
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> intervensiList = this.asuhanKeperawatanPeriOperasiHeaderDao.getListPostOperasiTransByIntervensi(notrans);
			
			for(Map<String, Object> data : intervensiList) {
				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"));
				tempList.add(tempMap);
			}
			
			intervensiMap.put("header", "Intervensi");
			intervensiMap.put("detail", tempList);
			listData.add(intervensiMap);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			Map<String, Object> evaluasiMap = new HashMap<>();
			
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> evaluasiList = this.asuhanKeperawatanPeriOperasiHeaderDao.getListPostOperasiTransByEvaluasi(notrans);
			
			for(Map<String, Object> data : evaluasiList) {
				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"));
				tempList.add(tempMap);
			}
			
			evaluasiMap.put("header", "Evaluasi");
			evaluasiMap.put("detail", tempList);
			listData.add(evaluasiMap);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Map<String, Object> dataHeader =  this.asuhanKeperawatanPeriOperasiHeaderDao.getListPostOperasiByNoTrans(notrans);
		if (CommonUtil.isNotNullOrEmpty(dataHeader)) {
			result.put("noTrans", (String)dataHeader.get("noTrans"));
			result.put("noRec", (String)dataHeader.get("noRec"));
			result.put("noRecPost", (String)dataHeader.get("noRecPost"));
			result.put("jamMasukOperasi", (String)dataHeader.get("jamMasukOperasi"));
			result.put("namaJaringan", (String)dataHeader.get("namaJaringan"));
			result.put("jumlah", (Double)dataHeader.get("jumlah"));
			result.put("kulturJaringan", (Boolean)dataHeader.get("kulturJaringan"));
			result.put("sitologi", (Boolean)dataHeader.get("sitologi"));
			result.put("pa", (Boolean)dataHeader.get("pa"));
			result.put("disiapkanOleh", (String)dataHeader.get("disiapkanOleh"));
			result.put("idPegawai", (Integer)dataHeader.get("idPegawai"));
			result.put("namePegawai", (String)dataHeader.get("namePegawai"));
			result.put("loadData", listData);
		}
		
		return result;

	}


	@Override
	public String noTrans() {
		Long count = this.postOperasiPerawatBedahDao.countNoTrans("PO");
		
		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 "PO" + yearNow + monthNow + digit + count;
	}

	@Override
	public Map<String, Object> noDefault() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<String> list = this.postOperasiPerawatBedahDao.noTrans("PO");
		
		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> getPasienDaftarOperasi(String noRec) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = this.asuhanKeperawatanPeriOperasiHeaderDao.getPostOperasiByNoRecAntrian(noRec);

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

		return result;
	}

}
