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.AntrianPasienDiPeriksaDao;
import com.jasamedika.medifirst2000.dao.PenghitunganKassaDanAlatOperasiDao;
import com.jasamedika.medifirst2000.entities.AntrianPasienDiPeriksa;
import com.jasamedika.medifirst2000.entities.DetailAlatYangDigunakan;
import com.jasamedika.medifirst2000.entities.DetailDokterOperasi;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PenghitunganKassaDanAlatOperasi;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.service.PenghitunganKassaDanAlatOperasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DetailAlatYangDigunakanVO;
import com.jasamedika.medifirst2000.vo.DetailDokterOperasiVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PenghitunganKassaDanAlatOperasiVO;
import com.jasamedika.medifirst2000.vo.ProdukVO;


/**
 * Implement class for PasienService
 * 
 * @author Adik
 * @param <T>
 */

@Service("penghitunganKassaDanAlatOperasiService")
public class PenghitunganKassaDanAlatOperasiServiceImpl extends BaseVoServiceImpl implements PenghitunganKassaDanAlatOperasiService {
	
	@Autowired
	private BaseConverterImpl<DetailAlatYangDigunakanVO,DetailAlatYangDigunakan> detailAlatYangDigunakanConverter;
	
	@Autowired
	private BaseConverterImpl<DetailDokterOperasiVO,DetailDokterOperasi> detailDokterOperasiConverter;
	
	@Autowired
	private BaseConverterImpl<ProdukVO,Produk> produkConverter;
	
	@Autowired
	private PenghitunganKassaDanAlatOperasiDao penghitunganKassaDanAlatOperasiDao;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private AntrianPasienDiPeriksaDao antrianPasienDiPeriksaDao;

	@Override
	public Map<String, Object> save(PenghitunganKassaDanAlatOperasiVO vo) {
		PenghitunganKassaDanAlatOperasi model=new PenghitunganKassaDanAlatOperasi();
		
		AntrianPasienDiPeriksa norec = antrianPasienDiPeriksaDao.findByNoRec(vo.getPasienDaftar().getNoRec());
		model.setPasienDaftar(norec);
	
		model.setStatusEnabled(vo.getStatusEnabled());
		model.setNoRec(vo.getNoRec());
		
		//Set Detail Alat Yang DIgunakan
		model.getDetailAlatYangDigunakan().clear();
		for(DetailAlatYangDigunakanVO detailAlatYangDigunakanVO:vo.getDetailAlatYangDigunakan()){
			DetailAlatYangDigunakan detailAlatYangDigunakan = new DetailAlatYangDigunakan();
			detailAlatYangDigunakan=detailAlatYangDigunakanConverter.transferVOToModel(detailAlatYangDigunakanVO, new DetailAlatYangDigunakan());
			if(CommonUtil.isNotNullOrEmpty(detailAlatYangDigunakanVO.getProduk())){
				detailAlatYangDigunakan.setProduk(produkConverter.transferVOToModel(detailAlatYangDigunakanVO.getProduk(), new Produk()));
			}
			detailAlatYangDigunakan.setNoRec(detailAlatYangDigunakanVO.getNoRec());
			detailAlatYangDigunakan.setPenghitunganKassaDanAlatOperasi(model);
			model.getDetailAlatYangDigunakan().add(detailAlatYangDigunakan);
		}
		
		//Set Detail Dokter Operasi
		model.getDetailDokterOperasi().clear();
		for(DetailDokterOperasiVO detailDokterOperasiVO:vo.getDetailDokterOperasi()){
			DetailDokterOperasi detailDokterOperasi = new DetailDokterOperasi();
			detailDokterOperasi=detailDokterOperasiConverter.transferVOToModel(detailDokterOperasiVO, new DetailDokterOperasi());
			//if(CommonUtil.isNotNullOrEmpty(detailDokterOperasiVO.getProduk())){
			//	detailDokterOperasi.setProduk(produkConverter.transferVOToModel(detailDokterOperasiVO.getProduk(), new Produk()));
			//}
			
			if(CommonUtil.isNotNullOrEmpty(detailDokterOperasiVO.getAhliBedah())) {
				detailDokterOperasi.setAhliBedah(pegawaiConverter.transferVOToModel(detailDokterOperasiVO.getAhliBedah(), new Pegawai()));
			}
			
			if(CommonUtil.isNotNullOrEmpty(detailDokterOperasiVO.getAsisten())) {
				detailDokterOperasi.setAsisten(pegawaiConverter.transferVOToModel(detailDokterOperasiVO.getAsisten(), new Pegawai()));
			}
			
			if(CommonUtil.isNotNullOrEmpty(detailDokterOperasiVO.getPerawatInstrumen())) {
				detailDokterOperasi.setPerawatInstrumen(pegawaiConverter.transferVOToModel(detailDokterOperasiVO.getPerawatInstrumen(), new Pegawai()));
			}
			
			if(CommonUtil.isNotNullOrEmpty(detailDokterOperasiVO.getPerawatSekuler())) {
				detailDokterOperasi.setPerawatSekuler(pegawaiConverter.transferVOToModel(detailDokterOperasiVO.getPerawatSekuler(), new Pegawai()));
			}
			
			if(CommonUtil.isNotNullOrEmpty(detailDokterOperasiVO.getPerawatSirkuler())) {
				detailDokterOperasi.setPerawatSirkuler(pegawaiConverter.transferVOToModel(detailDokterOperasiVO.getPerawatSirkuler(), new Pegawai()));
			}
			
			detailDokterOperasi.setNoRec(detailDokterOperasiVO.getNoRec());
			detailDokterOperasi.setPenghitunganKassaDanAlatOperasi(model);
			model.getDetailDokterOperasi().add(detailDokterOperasi);
		}
		penghitunganKassaDanAlatOperasiDao.save(model);
		
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("noRec", model.getNoRec());
		return result;
	}

	@Override
	public Map<String, Object> listHitKassa(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
				
		List<Map<String, Object>> resultData = this.penghitunganKassaDanAlatOperasiDao.getListHitKassa(noRec);
				
		if(CommonUtil.isNotNullOrEmpty(resultData)) {
			result.put("showData", resultData);
		}
		return result;
	}

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

				Map<String, Object> kassaMap = new HashMap<>();
				List<Map<String, Object>> hitKassa = this.penghitunganKassaDanAlatOperasiDao.getHitKassaByNoRec(noRec);
				
				//get data Detail Alat Yang Digunakan
				try {
					
					Map<String, Object> alatMap = new HashMap<>();
					List<Map<String, Object>> tempList = new ArrayList<>();
					List<Map<String, Object>> alatList = this.penghitunganKassaDanAlatOperasiDao
							.getDetailAlatYangDigunakanByNoRec(noRec);
					
					for(Map<String, Object> data : alatList){
						Map<String, Object> tempMap = new HashMap<>();
						tempMap.put("noRec", (String)data.get("noRec"));
						tempMap.put("noRecHitKassa", (String)data.get("noRecHitKassa"));
						tempMap.put("idProduk", (Integer)data.get("idProduk"));
						tempMap.put("namaProduk", (String)data.get("namaProduk"));
						tempMap.put("keterngan", (String)data.get("keterngan")); 
						tempMap.put("kuantitas", (String)data.get("kuantitas")); 
						tempMap.put("tambah1", (String)data.get("tambah1"));
						tempMap.put("tambah2", (String)data.get("tambah2"));
						tempMap.put("tambah3", (String)data.get("tambah3"));
						tempMap.put("tambah4", (String)data.get("tambah4"));
						tempMap.put("tambah5", (String)data.get("tambah5"));
						tempMap.put("tambah6", (String)data.get("tambah6"));
						tempMap.put("tambah7", (String)data.get("tambah7"));
						tempMap.put("statusEnabled", (Boolean) data.get("statusEnabled"));
						tempList.add(tempMap);
					}
					
					alatMap.put("DetailAlatDigunakan", tempList);
					listData.add(alatMap);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				//get data Detail Dokter
				try {
							
					Map<String, Object> dokterMap = new HashMap<>();
					List<Map<String, Object>> tempList = new ArrayList<>();
					List<Map<String, Object>> dokterList = this.penghitunganKassaDanAlatOperasiDao
							.getDetailDokterOperasiByNoRec(noRec);
					
					for(Map<String, Object> data : dokterList){
						Map<String, Object> tempMap = new HashMap<>();
						tempMap.put("noRec", (String)data.get("noRec"));
						tempMap.put("idAhliBedah", (Integer)data.get("idAhliBedah"));
						tempMap.put("nameAhliBedah", (String)data.get("nameAhliBedah"));
						tempMap.put("idAsisten", (Integer)data.get("idAsisten"));
						tempMap.put("nameAsisten", (String)data.get("nameAsisten"));
						tempMap.put("idPerawatInstrumen", (Integer)data.get("idPerawatInstrumen"));
						tempMap.put("namePerawatInstrumen", (String)data.get("namePerawatInstrumen"));
						tempMap.put("idPerawatSekuler", (Integer)data.get("idPerawatSekuler"));
						tempMap.put("namePerawatSekuler", (String)data.get("namePerawatSekuler"));
						tempMap.put("idPerawatSirkuler", (Integer)data.get("idPerawatSirkuler"));
						tempMap.put("namePerawatSirkuler", (String)data.get("namePerawatSirkuler"));
						tempMap.put("kondisiSterilisasi", (String)data.get("kondisiSterilisasi"));
						tempMap.put("load", (String)data.get("load"));
						tempMap.put("alat", (String)data.get("alat"));
						tempMap.put("tanggalSteril", (Date) data.get("tanggalSteril"));
						tempMap.put("statusEnabled", (Boolean) data.get("statusEnabled"));
						tempList.add(tempMap);
					}
							
					dokterMap.put("DetailDokter", tempList);
					listData.add(dokterMap);
							
					} catch (Exception e) {
							e.printStackTrace();
					}
				
				kassaMap.put("Kassa", hitKassa);
				listData.add(kassaMap);
				result.put("showData", listData);
				
				return result;

	}
	
	


}
