package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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.dao.StokProdukGlobalDao;
import com.jasamedika.medifirst2000.dao.StrukResepDao;
import com.jasamedika.medifirst2000.dao.custom.StrukResepDaoCustom;
import com.jasamedika.medifirst2000.service.ReturObatService;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Service("returObatService")
public class ReturObatServiceImpl implements ReturObatService{

	@Autowired
	private StrukResepDao strukResepDao;
	
	@Autowired
	private StrukResepDaoCustom strukResepDaoCustom;
	
	@Autowired
	private StokProdukGlobalDao stokProdukGlobalDao;
	
	@Override
	public List<Map<String, Object>> getReturStrukResep(Integer ruanganId, String noResep, String noCm, String tglAwal, String tglAhir){
		List<Map<String, Object>> result = new ArrayList<>();
		
		for(Map<String, Object> data : strukResepDaoCustom.getReturStrukResep
				(Integer.parseInt(strukResepDao.findIdRuanganFarmasi("IdDepartemenInstalasiFarmasi")), ruanganId, noResep, noCm, tglAwal, tglAhir)){
			
			Map<String, Object> map = new HashMap<>();	
			map.put("noCm", data.get("noCm"));
			map.put("noResep", data.get("noResep"));
			map.put("tglResep", data.get("tglResep"));
			map.put("noStruk", data.get("noStruk"));
			map.put("tglStruk", data.get("tglStruk"));
			map.put("noRecOrder", data.get("noRecOrder"));
			map.put("namaPasien", data.get("namaPasien"));
			map.put("noRegistrasi", data.get("noRegistrasi"));
			
			List<Map<String, Object>> pelayananPasien = strukResepDao.getPelayananPasien((String)data.get("noRecOrder"));
			if(CommonUtil.isNotNullOrEmpty(pelayananPasien)){
				Map<String, Object> pl = pelayananPasien.get(0);
				map.put("noStruk", pl.get("noStruk"));
				map.put("noSbmLast", pl.get("noSbmLast"));
			} else{
				map.put("noStruk", null);
				map.put("noSbmLast", null);
			}
			
			result.add(map);
		}
		
		return result;
	}
	
	public List<Map<String, Object>> getRuanganFarmasi(){
		return strukResepDao.getRuanganFarmasi(
				Integer.parseInt(strukResepDao.findIdRuanganFarmasi("IdDepartemenInstalasiFarmasi")));
	}
	
	@Override
	public Map<String, Object> detailReturResep(String noRec, String noStruk, String noSbmLast){
		Map<String, Object> result = new HashMap<>();

		if(CommonUtil.isNullOrEmpty(noStruk)){
			List<Map<String, Object>> listData = strukResepDao.detailReturResep(noRec);
			if(CommonUtil.isNotNullOrEmpty(listData)){
				result.put("listData", listData);
			} else{
				result.put("listData", "Tidak ada barang yang dapat diretur");
			}
		} else{
			if(CommonUtil.isNotNullOrEmpty(noSbmLast)){
				List<Map<String, Object>> listData = strukResepDao.detailReturResep(noRec);
				if(CommonUtil.isNotNullOrEmpty(listData)){
					result.put("listData", listData);
				} else{
					result.put("listData", "Tidak ada barang yang dapat diretur");
				}
			} else{
				return null;
			}
		}
		return result;
	}
}
