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.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PlanningDiklatHumasMarketPesertaDao;
import com.jasamedika.medifirst2000.entities.AsalPeserta;
import com.jasamedika.medifirst2000.entities.JenisPetugasPelaksana;
import com.jasamedika.medifirst2000.entities.JenisTempat;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PlanningDiklatHumasMarketBiaya;
import com.jasamedika.medifirst2000.entities.PlanningDiklatHumasMarketPeserta;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Rekanan;
import com.jasamedika.medifirst2000.service.PlanningDiklatHumasMarketPesertaService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.AsalPesertaVO;
import com.jasamedika.medifirst2000.vo.JenisPetugasPelaksanaVO;
import com.jasamedika.medifirst2000.vo.JenisTempatVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PlanningDiklatHumasMarketPesertaVO;
import com.jasamedika.medifirst2000.vo.RekananVO;

@Service("PlanningDiklatHumasMarketPesertaService")
public class PlanningDiklatHumasMarketPesertaServiceImpl implements PlanningDiklatHumasMarketPesertaService {
	@Autowired
	private PlanningDiklatHumasMarketPesertaDao planningDiklatHumasMarketPesertaDao;
	
	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<PlanningDiklatHumasMarketPesertaVO, PlanningDiklatHumasMarketPeserta> pesertaConverter;
	
	@Autowired
	private BaseConverterImpl<JenisPetugasPelaksanaVO, JenisPetugasPelaksana> jenisPetugasConverter;
	
	@Autowired
	private BaseConverterImpl<JenisTempatVO, JenisTempat> jenisTempatConverter;
	
	@Autowired
	private BaseConverterImpl<AsalPesertaVO, AsalPeserta> asalPesertaConverter;
	
	@Autowired
	private BaseConverterImpl<RekananVO, Rekanan> rekananConverter;
	
	public Map<String, Object> savePlanningDHMPeserta(PlanningDiklatHumasMarketPesertaVO vo) {
		Map<String, Object> result = new HashMap<>();
		
        
		PlanningDiklatHumasMarketPeserta peserta = new PlanningDiklatHumasMarketPeserta();
		peserta = pesertaConverter.transferVOToModel(vo, peserta);
	
		if(CommonUtil.isNotNullOrEmpty(vo.getKdPegawai()))  {
			peserta.setKdPegawai(pegawaiConverter.transferVOToModel(vo.getKdPegawai(), new Pegawai()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getKdPegawaiExec()))  {
			peserta.setKdPegawaiExec(pegawaiConverter.transferVOToModel(vo.getKdPegawaiExec(), new Pegawai()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getKdJenisPetugasPe()))  {
			peserta.setKdJenisPetugasPe(jenisPetugasConverter.transferVOToModel(vo.getKdJenisPetugasPe(), new JenisPetugasPelaksana()));
		}
		
		if(CommonUtil.isNotNullOrEmpty(vo.getKdAsalPeserta()))  {
			peserta.setKdAsalPeserta(asalPesertaConverter.transferVOToModel(vo.getKdAsalPeserta(), new AsalPeserta()));
		}
	
		if(CommonUtil.isNotNullOrEmpty(vo.getKdRekananPeserta()))  {
			peserta.setKdRekananPeserta(rekananConverter.transferVOToModel(vo.getKdRekananPeserta(), new Rekanan()));
		}
		
		//save pdhms
		if(CommonUtil.isNotNullOrEmpty(peserta)) {
			PlanningDiklatHumasMarketPeserta resultPeserta = planningDiklatHumasMarketPesertaDao.save(peserta);
			result.put("NoRec", resultPeserta.getNoRec());
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getListPDHMPeserta(String noPlanning) {

			Map<String, Object> result = new HashMap<>();

			List<Map<String, Object>> listdata = this.planningDiklatHumasMarketPesertaDao.getListPlanningDHMPesertaByNoPlanning(noPlanning);
			
			if (CommonUtil.isNotNullOrEmpty(listdata)) {
				result.put("data", listdata);
			}
			return result;
	}
	
	@Override
	public Map<String, Object> getListJenisPetugasPe() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		Pageable topTen = new PageRequest(0, 10);
		
		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketPesertaDao.getListJenisPetugasPe(topTen);
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListAsalPeserta() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		Pageable topTen = new PageRequest(0, 10);
		
		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketPesertaDao.getListAsalPeserta(topTen);
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListRekananPeserta() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		Pageable topTen = new PageRequest(0, 10);
		
		List<Map<String, Object>> listdata =  this.planningDiklatHumasMarketPesertaDao.getListRekananPeserta(topTen);
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListAllPlanningDHM() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		List<Map<String, Object>> listdata = this.planningDiklatHumasMarketPesertaDao.getListAllPlanningDHM();
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListPegawai() {
		Map<String, Object> result = new HashMap<String, Object>();
		
		Pageable topTen = new PageRequest(0, 10);
		
		List<Map<String, Object>> listdata =  this.planningDiklatHumasMarketPesertaDao.getListPegawai(topTen);
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getListProdukDHMbyNoPlanning(String noPlanning) {
		Map <String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listdata =  this.planningDiklatHumasMarketPesertaDao.getListProdukDHMbyNoPlanning(noPlanning);
		
		if (CommonUtil.isNotNullOrEmpty(listdata)) {
			result.put("data", listdata);
		}
		return result;
	}
	


}
