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.PostOperasiAnestesiDao;
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.Ruangan;
import com.jasamedika.medifirst2000.service.PostOperasiAnestesiService;
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.RuanganVO;

@Service("PostOperasiAnestesiService")
public class PostOperasiAnestesiServiceImpl implements PostOperasiAnestesiService {
	@Autowired
	private AsuhanKeperawatanPeriOperasiHeaderDao asuhanKeperawatanPeriOperasiHeaderDao;
	
	@Autowired
	private AsuhanKeperawatanPeriOperasiTransDao asuhanKeperawatanPeriOperasiTransDao;
	
	@Autowired
	private PostOperasiAnestesiDao postOperasiAnestesiDao;
	
	@Autowired
	private BaseConverterImpl<AsuhanKeperawatanPeriOperatifVO, AsuhanKeperawatanPeriOperatif> asuhanKeperawatanPeriOperatifConverter;
	
	@Autowired
	private BaseConverterImpl<AsuhanKeperawatanPeriOperasiTransVO, AsuhanKeperawatanPeriOperasiTrans> asuhanDetaiConverter;
	
	@Autowired
	private BaseConverterImpl<AntrianPasienDiPeriksaVO, AntrianPasienDiPeriksa> antrianPasienDaftarConverter;

	@Override
	public Map<String, Object> savePostOperasiAnestesi(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());
			}
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getPostOperasiAnestesi(String notrans) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = new ArrayList<>();
		
		try {
			Map<String, Object> dataMasalahTrans = new HashMap<>();
			
			List<Map<String, Object>> detailTrans = new ArrayList<>();
			List<Map<String, Object>> dataAsuhanDetail = this.asuhanKeperawatanPeriOperasiHeaderDao
					.getListPostOperasiTransByDataMasalahAnestesi(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);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			Map<String, Object> intervensiMap = new HashMap<>();
			
			List<Map<String, Object>> tempList = new ArrayList<>();
			List<Map<String, Object>> intervensiList = this.asuhanKeperawatanPeriOperasiHeaderDao
					.getListPostOperasiTransByIntervensiAnestesi(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
					.getListPostOperasiTransByEvaluasiAnestesi(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.getListAsuhanTransByNoTrans(notrans);
		if (CommonUtil.isNotNullOrEmpty(dataHeader)) {
			result.put("noTrans", dataHeader.get("noTrans"));
			result.put("noRec", dataHeader.get("noRec"));
			result.put("jamMasukOperasi", dataHeader.get("jamMasukOperasi"));
			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.getPostOperasiAnestesiByNoRecAntrian(noRec);

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

		return result;
	}

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

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