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.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.RincianKegiatanDao;
import com.jasamedika.medifirst2000.dao.UraianTugasDao;
import com.jasamedika.medifirst2000.dao.UraianTugasMDao;
import com.jasamedika.medifirst2000.entities.Dokumen;
import com.jasamedika.medifirst2000.entities.Jabatan;
import com.jasamedika.medifirst2000.entities.KategoryTugas;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.PelaksanaanTugas;
import com.jasamedika.medifirst2000.entities.RincianKegiatan;
import com.jasamedika.medifirst2000.entities.UraianTugas;
import com.jasamedika.medifirst2000.entities.UraianTugasM;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.service.UraianTugasService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DokumenVO;
import com.jasamedika.medifirst2000.vo.JabatanVO;
import com.jasamedika.medifirst2000.vo.KategoryTugasVO;
import com.jasamedika.medifirst2000.vo.PelaksanaanTugasVO;
import com.jasamedika.medifirst2000.vo.RincianKegiatanVO;
import com.jasamedika.medifirst2000.vo.UraianTugasMVO;
import com.jasamedika.medifirst2000.vo.UraianTugasVO;

/**
 * Implement class for UraianTugasService
 * 
 * @author Generator
 * @param <T>
 */
@Service("uraianTugasService")
public class UraianTugasServiceImpl extends BaseVoServiceImpl implements UraianTugasService {

	@Autowired
	private LoginUserService loginUserService;

	@Autowired
	private BaseConverterImpl<UraianTugasVO,UraianTugas>uraianTugasConverter;
	
	@Autowired
	private BaseConverterImpl<UraianTugasMVO,UraianTugasM> uraianTugasMConverter;
	
	@Autowired
	private BaseConverterImpl<KategoryTugasVO,KategoryTugas>kategoryTugasConverter;
	
	@Autowired
	private BaseConverterImpl<RincianKegiatanVO, RincianKegiatan> rincianKegiatanConverter;
	
	@Autowired
	private BaseConverterImpl<PelaksanaanTugasVO, PelaksanaanTugas> pelaksanaTugasConverter;
	
	@Autowired
	private BaseConverterImpl<DokumenVO, Dokumen> dokumenConverter;

	@Autowired
	private BaseConverterImpl<JabatanVO, Jabatan> jabatanConverter;

	@Autowired
	private UraianTugasDao uraianTugasDao;
	
	@Autowired
	private UraianTugasMDao uraianTugasMDao;

	@Autowired
	private RincianKegiatanDao rincianKegiatanDao;
	
	@Override
	public Map<String, Object> saveUraianTugas(UraianTugasVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		UraianTugas model = new UraianTugas();
		model = uraianTugasConverter.transferVOToModel(vo,model);
		
		if (CommonUtil.isNotNullOrEmpty(vo.getKategoryTugas()))
		{
				model.setKategoryTugas(kategoryTugasConverter.transferVOToModel(vo.getKategoryTugas(), new KategoryTugas()));
		}

		UraianTugas resultDomain = uraianTugasDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noRec", resultDomain.getNoRec());
		}

		return result;
	}



	@Override
	public Map<String, Object> saveRincianKegiatan(RincianKegiatanVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(vo))
		{
			RincianKegiatan rincianKegiatan = rincianKegiatanConverter.transferVOToModel(vo, new RincianKegiatan());
			if(CommonUtil.isNotNullOrEmpty(rincianKegiatan))
			{
				rincianKegiatanDao.save(rincianKegiatan);
				result.put("id", rincianKegiatan.getId());
			}
		}
		return result;
	}



	@Override
	public Map<String, Object> saveUraianTugasNew(UraianTugasMVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(vo))
		{
			UraianTugasM uraianTugas = uraianTugasMConverter.transferVOToModel(vo, new UraianTugasM());
			if(CommonUtil.isNotNullOrEmpty(vo.getPelaksanaanTugas()))
			{
				uraianTugas.setPelaksanaanTugas(pelaksanaTugasConverter.transferVOToModel(vo.getPelaksanaanTugas(), new PelaksanaanTugas()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getJabatan()))
			{
				uraianTugas.setJabatan(jabatanConverter.transferVOToModel(vo.getJabatan(), new Jabatan()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getRincianKegiatan()))
			{
				uraianTugas.setRincianKegiatan(rincianKegiatanConverter.transferVOToModel(vo.getRincianKegiatan(), new RincianKegiatan()));
			}
			if(CommonUtil.isNotNullOrEmpty(vo.getDokumen()))
			{
				uraianTugas.setDokumen(dokumenConverter.transferVOToModel(vo.getDokumen(), new Dokumen()));
			}
			if(CommonUtil.isNotNullOrEmpty(uraianTugas))
			{
				uraianTugasMDao.save(uraianTugas);
				result.put("id", uraianTugas.getId());
			}
		}
		return result;
	}



	@Override
	public Map<String, Object> getRincianKegiatanPegawai(Integer idJabatan, Integer idPelaksanaanTugas) {
		Map<String, Object> result = new HashMap<>();
		
		LoginUser loginUser = loginUserService.getLoginUser();
		List<Map<String, Object>> data = uraianTugasMDao.getRincianKegiatanPegawai(loginUser.getPegawai().getId(), idJabatan, idPelaksanaanTugas);
		
		List<Map<String, Object>> listRincian = new ArrayList<>();
		for (Map<String, Object> map : data) {
			result.put("idPegawai", map.get("idPegawai"));
			result.put("namaPegawai", map.get("namaPegawai"));
			result.put("idJabatan", map.get("idJabatan"));
			result.put("namaJabatan", map.get("namaJabatan"));
			result.put("idPelaksanaanTugas", map.get("idPelaksanaanTugas"));
			result.put("pelaksanaanTugas", map.get("pelaksanaanTugas"));
			
			Map<String, Object> mapRincian = new HashMap<>();
			mapRincian.put("idUraianTugas", map.get("idUraianTugas"));
			mapRincian.put("idRincianKegiatan", map.get("idRincian"));
			mapRincian.put("namaKegiatan", map.get("rincianKegiatan"));
			mapRincian.put("statusVerifikasi", map.get("statusVerifikasi"));
			listRincian.add(mapRincian);
			
			result.put("listRincian", listRincian);
		}
		
		return result;
	}
}