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.KamarDao;
import com.jasamedika.medifirst2000.dao.TempatTidurDao;
import com.jasamedika.medifirst2000.entities.Kamar;
import com.jasamedika.medifirst2000.entities.Kelas;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.TempatTidur;
import com.jasamedika.medifirst2000.service.KetersediaanTempatTidurService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.InformasiKetersediaanKamarVO;
import com.jasamedika.medifirst2000.vo.KetersediaanTempatTidurVO;

@Service("KetersediaanTempatTidurService")
public class KetersediaanTempatTidurServiceImpl implements KetersediaanTempatTidurService {
	
	@Autowired
	private TempatTidurDao tempatTidurDao;
	
	@Autowired
	private KamarDao kamarDao;
	
	@Override
	public Map<String, Object> countDataKetersediaanTempatTidur() {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("Terpakai", tempatTidurDao.countTempatTidurTerpakai());
		result.put("Kosong", tempatTidurDao.countTempatTidurKosong());
		result.put("Proses", tempatTidurDao.countTempatTidurProsesAdministrasi());
		result.put("Total", tempatTidurDao.countAllTempatTidur());
		result.put("dataFound", true);
		return result;
	}

	@Override
	public Map<String, Object> getDataKetersediaanTempatTidur(KetersediaanTempatTidurVO vo) {
		Map<String, Object> result = new HashMap<String,Object>();
		return result;
	}

	@Override
	public Map<String, Object> getTempatTidurByKamarAndKelas(Integer idKamar, Integer idKelas) {
		Map<String, Object> result = new HashMap<String,Object>();
		
		List<TempatTidur> list = (List<TempatTidur>) tempatTidurDao.findByKamarAndKelas(idKamar, idKelas);
		
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("Data", list);
			result.put("dataFound", true);
		}
		
		return result;
	}

	@Override
	public Map<String, Object> countTempatTidurByKamarAndKelas(Integer idKamar, Integer idKelas) {
		Map<String, Object> result = new HashMap<String,Object>();
		result.put("Terpakai", tempatTidurDao.countTempatTidurTerpakaiByKamarAndKelas(idKamar, idKelas));
		result.put("Kosong", tempatTidurDao.countTempatTidurKosongByKamarAndKelas(idKamar, idKelas));
		result.put("Proses", tempatTidurDao.countTempatTidurProsesAdministrasiByKamarAndKelas(idKamar, idKelas));
		if(CommonUtil.isNotNullOrEmpty(result)){
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getRuanganRawatInap() {
		Map<String, Object> result = new HashMap<String,Object>();
		List<Ruangan> ruangan = new ArrayList<Ruangan>();
		
		ruangan = kamarDao.getRuanganRawatInapFromKamar();
		
		if(CommonUtil.isNotNullOrEmpty(ruangan)){
			result.put("data", ruangan);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getKamarByRuanganAndKelas(Integer idRuangan, Integer idKelas) {
		List<Kamar> listKamar = new ArrayList<Kamar>();
		System.out.println("================================================");
		System.out.println("IDRUANGAN : " + idRuangan.toString() + "IDKELAS : "+idKelas.toString());
		listKamar = kamarDao.getKamarByRuanganAndKelas(idRuangan, idKelas);
		
		List<InformasiKetersediaanKamarVO> list = new ArrayList<InformasiKetersediaanKamarVO>();
		for(Kamar kamar : listKamar){
			InformasiKetersediaanKamarVO informasiKetersediaanKamarVO = new InformasiKetersediaanKamarVO();
			informasiKetersediaanKamarVO.setNamaKamar(kamar.getNamaKamar());
			informasiKetersediaanKamarVO.setNamaKelas(kamar.getKelas().getNamaKelas());
			informasiKetersediaanKamarVO.setTempatTidurTerpakai(tempatTidurDao.countTempatTidurTerpakaiByKamarAndKelas(kamar.getId(), idKelas));
			informasiKetersediaanKamarVO.setTempatTidurProsesAdministrasi(tempatTidurDao.countTempatTidurProsesAdministrasiByKamarAndKelas(kamar.getId(), idKelas));
			informasiKetersediaanKamarVO.setTempatTidurKosong(tempatTidurDao.countTempatTidurKosongByKamarAndKelas(kamar.getId(), idKelas));
			System.out.println(informasiKetersediaanKamarVO.getTempatTidurKosong());
			list.add(informasiKetersediaanKamarVO);
		}
		
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getAllInformasiKetersediaanKamar() {
		List<Kamar> listKamar = new ArrayList<Kamar>();
		listKamar = (List<Kamar>) kamarDao.findAll();
		
		List<InformasiKetersediaanKamarVO> list = new ArrayList<InformasiKetersediaanKamarVO>();
		for(Kamar kamar : listKamar){
			InformasiKetersediaanKamarVO informasiKetersediaanKamarVO = new InformasiKetersediaanKamarVO();
			informasiKetersediaanKamarVO.setNamaKamar(kamar.getNamaKamar());
			informasiKetersediaanKamarVO.setNamaKelas(kamar.getKelas().getNamaKelas());
			informasiKetersediaanKamarVO.setTempatTidurTerpakai(tempatTidurDao.countTempatTidurKosongByKamarAndKelas(kamar.getId(), kamar.getKelas().getId()));
			informasiKetersediaanKamarVO.setTempatTidurProsesAdministrasi(tempatTidurDao.countTempatTidurProsesAdministrasiByKamarAndKelas(kamar.getId(), kamar.getKelas().getId()));
			informasiKetersediaanKamarVO.setTempatTidurKosong(tempatTidurDao.countTempatTidurKosongByKamarAndKelas(kamar.getId(), kamar.getKelas().getId()));
			list.add(informasiKetersediaanKamarVO);
		}
		
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getKelasByRuangan(Integer idRuangan) {
		List<Kelas> listKelas = new ArrayList<Kelas>();
		listKelas = kamarDao.getKelasByRuangan(idRuangan);
		Map<String, Object> result = new HashMap<String,Object>();
		if(CommonUtil.isNotNullOrEmpty(listKelas)){
			result.put("data", listKelas);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		
		return result;
	}

	@Override
	public List<Map<String, Object>> findAllActiveBed() {
		return tempatTidurDao.findAllActiveBed();
	}
	
	

}
