package com.jasamedika.medifirst2000.service.impl;

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.FasilitasMDao;
import com.jasamedika.medifirst2000.entities.Departemen;
import com.jasamedika.medifirst2000.entities.FasilitasM;
import com.jasamedika.medifirst2000.entities.Kelas;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.FasilitasMService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DepartemenVO;
import com.jasamedika.medifirst2000.vo.FasilitasMVO;
import com.jasamedika.medifirst2000.vo.FasilitasVO;
import com.jasamedika.medifirst2000.vo.KelasVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("FasilitasMService")
public class FasilitasMServiceImpl implements FasilitasMService {
	
	@Autowired
	private FasilitasMDao fasilitasDao;
	
	@Autowired
	private BaseConverterImpl<FasilitasMVO, FasilitasM> fasilitasConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<KelasVO, Kelas> kelasConverter;
	
	@Autowired
	private BaseConverterImpl<DepartemenVO, Departemen> departemenConventer;

	@Override
	public Map<String, Object> saveFasilitas(FasilitasMVO fv) {
		Map<String, Object> result = new HashMap<>();
		
		FasilitasM fa = new FasilitasM();
		
		if (CommonUtil.isNotNullOrEmpty(fv)) {
			fa = fasilitasConverter.transferVOToModel(fv, fa);
		}
		
		if (CommonUtil.isNotNullOrEmpty(fv)) {
			fa.setNamaFasilitas(fv.getNamaFasilitas());
		}
		
		if (CommonUtil.isNotNullOrEmpty(fv.getKdRuangan()) ) {
			fa.setKdRuangan(ruanganConverter.transferVOToModel(fv.getKdRuangan(), new Ruangan()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(fv.getKdKelas()) ) {
			fa.setKdKelas(kelasConverter.transferVOToModel(fv.getKdKelas(), new Kelas()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(fv.getKdDepartemen()) ) {
			fa.setKdDepartemen(departemenConventer.transferVOToModel(fv.getKdDepartemen(), new Departemen()));
		}
		
		if (CommonUtil.isNotNullOrEmpty(fa)) {
			FasilitasM faResult = this.fasilitasDao.save(fa);
			result.put("id", faResult.getId());
		}
		return result;
				
	}
	

	@Override
	public Map<String, Object> getListFasilitas() {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = this.fasilitasDao.getAllFasilitas();
		
		if (CommonUtil.isNotNullOrEmpty(listData)) {
			result.put("data", listData);
		}
		return result;
	}

	@Override
	public Map<String, Object> getListFasilitasByKelasRuangan(Integer idKelas, Integer idRuangan) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = this.fasilitasDao.getAllFasilitasByKelasRuangan(idKelas, idRuangan);

		if (CommonUtil.isNotNullOrEmpty(listData)) {
			result.put("data", listData);
		}
		return result;
	}
	
	
	@Override
	public Map<String, Object> getListFasilitasByRuangan(Integer idRuangan) {
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = this.fasilitasDao.getAllFasilitasByRuangan(idRuangan);

		if (CommonUtil.isNotNullOrEmpty(listData)) {
			result.put("data", listData);
		}
		return result;
	}
	
	public Map<String, Object> getAllDepartemen() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> list = this.fasilitasDao.getAllDepartemen();
		
		if (CommonUtil.isNotNullOrEmpty(list)) {
			result.put("data", list);
		}
		
		return result;
	}
	

}
