package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.vo.PersyaratanPelamarVO;

import com.jasamedika.medifirst2000.entities.PersyaratanPelamar;

import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.DetailBerkasLamaranService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.entities.BerkasLamaran;
import com.jasamedika.medifirst2000.entities.DetailBerkasLamaran;
import com.jasamedika.medifirst2000.vo.BerkasLamaranVO;
import com.jasamedika.medifirst2000.vo.DetailBerkasLamaranVO;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.DetailBerkasLamaranDao;

/**
 * Implement class for DetailBerkasLamaranService
 * 
 * @author Generator
 * @param <T>
 */
@Service("detailBerkasLamaranService")
public class DetailBerkasLamaranServiceImpl extends BaseVoServiceImpl implements DetailBerkasLamaranService {
	@Autowired
	private BaseConverterImpl<DetailBerkasLamaranVO, DetailBerkasLamaran> detailBerkasLamaranConverter;
	
	@Autowired
	private BaseConverterImpl<BerkasLamaranVO, BerkasLamaran> berkasLamaranConverter;
	@Autowired
	private BaseConverterImpl<PersyaratanPelamarVO, PersyaratanPelamar> persyaratanPelamarConverter;

	@Autowired
	private DetailBerkasLamaranDao detailBerkasLamaranDao;

	@Override
	public Map<String, Object> saveDetailBerkasLamaran(DetailBerkasLamaranVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		DetailBerkasLamaran model = new DetailBerkasLamaran();
		detailBerkasLamaranConverter.setUseGson(true);
		model = detailBerkasLamaranConverter.transferVOToModel(vo, model);
		if (CommonUtil.isNotNullOrEmpty(vo.getBerkasLamaran())) {
			model.setBerkasLamaran(berkasLamaranConverter.transferVOToModel(vo.getBerkasLamaran(), new BerkasLamaran()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getSyarat())) {
			model.setSyarat(persyaratanPelamarConverter.transferVOToModel(vo.getSyarat(), new PersyaratanPelamar()));
		}

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

		return result;
	}

	@Override
	public Map<String, Object> findByName(String nama) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("items", JsonUtil.ToMaps(detailBerkasLamaranDao.findByStatusContaining(nama)));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> findByNamaPelamarNoRec(String nama) {
		try {
			return JsonUtil.ToMaps(detailBerkasLamaranDao.findByBerkasLamaranNoRec(nama));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
}