package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.StrukKirimDao;
import com.jasamedika.medifirst2000.entities.LimbahB3Keluar;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.entities.StrukKirim;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.entities.StrukPelayanan;
import com.jasamedika.medifirst2000.service.StrukKirimService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.LoginUserVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;
import com.jasamedika.medifirst2000.vo.StrukKirimVO;
import com.jasamedika.medifirst2000.vo.StrukOrderVO;
import com.jasamedika.medifirst2000.vo.StrukPelayananVO;

@Service("StrukKirimService")
public class StrukKirimServiceImpl implements StrukKirimService{
	
	@Autowired
	private StrukKirimDao strukKirimDao;
	
	@Autowired
	private BaseConverterImpl<StrukKirimVO, StrukKirim> strukKirimConverter;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> loginUserConverter;
	
	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;
	
	@Autowired
	private BaseConverterImpl<StrukOrderVO, StrukOrder> strukOrderConverter;
	
	@Autowired
	private BaseConverterImpl<StrukPelayananVO, StrukPelayanan> strukPelayananConverter;
	
	@Override
	public Map<String, Object> addStrukKirim(StrukKirimVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		StrukKirim strukKirim = new StrukKirim();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			strukKirim = strukKirimConverter.transferVOToModel(vo, new StrukKirim());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdpegawaipenerima())){
			strukKirim.setPegawaiPenerima(loginUserConverter.transferVOToModel(vo.getKdpegawaipenerima(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdpegawaipengirim())){
			strukKirim.setPegawaiPengirim(loginUserConverter.transferVOToModel(vo.getKdpegawaipengirim(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdruangan())){
			strukKirim.setKdruangan(ruanganConverter.transferVOToModel(vo.getKdruangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdruanganasal())){
			strukKirim.setKdruanganasal(ruanganConverter.transferVOToModel(vo.getKdruanganasal(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdruangantujuan())){
			strukKirim.setKdruangantujuan(ruanganConverter.transferVOToModel(vo.getKdruangantujuan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNoorder())){
			strukKirim.setNoorder(strukOrderConverter.transferVOToModel(vo.getNoorder(), new StrukOrder()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNostruk())){
			strukKirim.setNostruk(strukPelayananConverter.transferVOToModel(vo.getNostruk(), new StrukPelayanan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNostruk_t())){
			strukKirim.setNostruk_t(strukPelayananConverter.transferVOToModel(vo.getNostruk_t(), new StrukPelayanan()));
		}
		
		StrukKirim resultStrukKirim = new StrukKirim();
		if(CommonUtil.isNotNullOrEmpty(strukKirim)){
			resultStrukKirim = strukKirimDao.save(strukKirim);
		}
		if(CommonUtil.isNotNullOrEmpty(resultStrukKirim)){
			result.put("strukKirim", resultStrukKirim.getNoRec());
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> addStrukKirim(LimbahB3Keluar limbahB3Keluar, StrukKirimVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		StrukKirim strukKirim = new StrukKirim();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			strukKirim = strukKirimConverter.transferVOToModel(vo, new StrukKirim());
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdpegawaipenerima())){
			strukKirim.setPegawaiPenerima(loginUserConverter.transferVOToModel(vo.getKdpegawaipenerima(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdpegawaipengirim())){
			strukKirim.setPegawaiPengirim(loginUserConverter.transferVOToModel(vo.getKdpegawaipengirim(), new Pegawai()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdruangan())){
			strukKirim.setKdruangan(ruanganConverter.transferVOToModel(vo.getKdruangan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdruanganasal())){
			strukKirim.setKdruanganasal(ruanganConverter.transferVOToModel(vo.getKdruanganasal(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getKdruangantujuan())){
			strukKirim.setKdruangantujuan(ruanganConverter.transferVOToModel(vo.getKdruangantujuan(), new Ruangan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNoorder())){
			strukKirim.setNoorder(strukOrderConverter.transferVOToModel(vo.getNoorder(), new StrukOrder()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNostruk())){
			strukKirim.setNostruk(strukPelayananConverter.transferVOToModel(vo.getNostruk(), new StrukPelayanan()));
		}
		if(CommonUtil.isNotNullOrEmpty(vo.getNostruk_t())){
			strukKirim.setNostruk_t(strukPelayananConverter.transferVOToModel(vo.getNostruk_t(), new StrukPelayanan()));
		}
		System.out.print("=============printid limbah===========");
		System.out.print(limbahB3Keluar.getNoRec());
		
		LimbahB3Keluar limbah = new LimbahB3Keluar();
		limbah.setNoRec(limbahB3Keluar.getNoRec());
		strukKirim.setLimbahB3Keluar(limbah);
		
		
		StrukKirim resultStrukKirim = new StrukKirim();
		if(CommonUtil.isNotNullOrEmpty(strukKirim)){
			resultStrukKirim = strukKirimDao.save(strukKirim);
		}
		if(CommonUtil.isNotNullOrEmpty(resultStrukKirim)){
			result.put("strukKirim", resultStrukKirim.getNoRec());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> findAllStrukKirim() {
		return null;
	}

	@Override
	public Map<String, Object> strukKirimPaging(Integer page, Integer limit, String sort, String dir,
			Specification<StrukKirim> spec) {
		return null;
	}

}
