package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.SwabAlatPermukaanDao;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.SwabAlatPermukaan;
import com.jasamedika.medifirst2000.service.SwabAlatPermukaanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.SwabAlatPermukaanVO;

@Service("swabAlatPermukaanService")
public class SwabAlatPermukaanServiceImpl implements SwabAlatPermukaanService{

	@Autowired
	private SwabAlatPermukaanDao swabAlatPermukaanDao;
	
	@Autowired
	private BaseConverterImpl<SwabAlatPermukaanVO, SwabAlatPermukaan> swabAlatPermukaanConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Override
	public Map<String, Object> getUnitRuangan(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = swabAlatPermukaanDao.getUnitRuangan();
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("unitRuangan", listData);
		}
		return result;
		
	}
	
	@Override
	public Map<String, Object> saveSwabAlatPermukaan(SwabAlatPermukaanVO vo){
		Map<String, Object> result = new HashMap<>();
		
		SwabAlatPermukaan swabAlatPermukaan = swabAlatPermukaanConverter.transferVOToModel(vo, new SwabAlatPermukaan());
		
		if(CommonUtil.isNotNullOrEmpty(vo.getNamaRuangan())){
			swabAlatPermukaan.setNamaRuangan(ruanganConverter.transferVOToModel(vo.getNamaRuangan(), new Ruangan()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getUploadDokumen())){
			try{
				byte[] decodedBytes = Base64.decodeBase64(vo.getUploadDokumen());
				swabAlatPermukaan.setFile(decodedBytes);
			}catch (Exception ex){
				ex.printStackTrace();
			}
		}
		
		if(CommonUtil.isNotNullOrEmpty(swabAlatPermukaan)){
			SwabAlatPermukaan pemeriksaanAngkaKumanUdaraResult = swabAlatPermukaanDao.save(swabAlatPermukaan);
			result.put("id", pemeriksaanAngkaKumanUdaraResult.getId());
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getSwabAlatPermukaan(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = swabAlatPermukaanDao.getSwabAlatPermukaan();
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("swabAlatPermukaan", listData);
		}
		return result;
	}
	
	@Override
	public Boolean updateSwabAlatPermukaan(SwabAlatPermukaanVO vo){
		if(CommonUtil.isNotNullOrEmpty(vo)){
			SwabAlatPermukaan swabAlatPermukaan = swabAlatPermukaanConverter.transferVOToModel(vo, new SwabAlatPermukaan());
			if(CommonUtil.isNotNullOrEmpty(vo.getNamaRuangan())){
				swabAlatPermukaan.setNamaRuangan(ruanganConverter.transferVOToModel(vo.getNamaRuangan(), new Ruangan()));
				swabAlatPermukaanDao.save(swabAlatPermukaan);
			}
			return true;
		} else{
			return false;
		}
	}
	
	@Override
	public Boolean deleteSwabAlatPermukaan(Integer id){
		if(CommonUtil.isNotNullOrEmpty(id)){
			SwabAlatPermukaan swabAlatPermukaan = swabAlatPermukaanDao.findOne(id);
			swabAlatPermukaanDao.delete(swabAlatPermukaan);
			return true;
		} else{
			return false;
		}
	}
	
	@Override
	public Map<String, Object> downloadFileHasilPemantauan(Integer id){
		if(CommonUtil.isNotNullOrEmpty(id)){
			return swabAlatPermukaanDao.downloadFileHasilPemantauan(id);
		} else {
			return null;
		}
	}
	
}
