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.JenisKelaminVO;
import com.jasamedika.medifirst2000.vo.AgamaVO;
import com.jasamedika.medifirst2000.vo.DegreeVO;
import com.jasamedika.medifirst2000.vo.StatusKawinVO;
import com.jasamedika.medifirst2000.vo.PosisiLamaranVO;

import com.jasamedika.medifirst2000.entities.JenisKelamin;
import com.jasamedika.medifirst2000.entities.Agama;
import com.jasamedika.medifirst2000.entities.Degree;
import com.jasamedika.medifirst2000.entities.StatusKawin;
import com.jasamedika.medifirst2000.entities.PosisiLamaran;
import com.jasamedika.medifirst2000.entities.RunningNumber;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.IsianPelamarService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.entities.IsianPelamar;
import com.jasamedika.medifirst2000.vo.IsianPelamarVO;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.IsianPelamarDao;
import com.jasamedika.medifirst2000.dao.RunningNumberDao;

/**
 * Implement class for IsianPelamarService
 * 
 * @author Generator
 * @param <T>
 */
@Service("isianPelamarService")
public class IsianPelamarServiceImpl extends BaseVoServiceImpl implements IsianPelamarService {
	@Autowired
	private BaseConverterImpl<IsianPelamarVO, IsianPelamar> isianPelamarConverter;
	@Autowired
	private BaseConverterImpl<JenisKelaminVO, JenisKelamin> jenisKelaminConverter;

	@Autowired
	private BaseConverterImpl<DegreeVO, Degree> degreeConverter;
	
	@Autowired
	private BaseConverterImpl<AgamaVO, Agama> agamaConverter;

	@Autowired
	private BaseConverterImpl<StatusKawinVO, StatusKawin> statusKawinConverter;

	@Autowired
	private BaseConverterImpl<PosisiLamaranVO, PosisiLamaran> posisiLamaranConverter;

	@Autowired
	private IsianPelamarDao isianPelamarDao;

	@Autowired
	private RunningNumberDao runningNumberDao;

	private String getRunningNumber(String kegunaan) {
		RunningNumber runningNumber = runningNumberDao.findByResetAndKegunaan("0", kegunaan);
		if (runningNumber == null) {
			runningNumber = new RunningNumber();
			runningNumber.setKegunaan(kegunaan);
			runningNumber.setReset("0");
			runningNumber.setNomerTerbaru((Integer) 0);
		}
		Integer number = (runningNumber.getNomerTerbaru() + (Integer) 1);
		runningNumber.setNomerTerbaru(runningNumber.getNomerTerbaru() + (Integer) 1);
		String noOrderIntern = StringUtil.formatNumber(number.toString(), 10);
		return noOrderIntern;
	}

	@Override
	@Transactional(readOnly = false)
	public Map<String, Object> saveIsianPelamar(IsianPelamarVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		IsianPelamar model = new IsianPelamar();
		model = isianPelamarConverter.transferVOToModel(vo, model);

		model.setNoPendaftaran(getRunningNumber("IsianPelamar"));
		if (CommonUtil.isNotNullOrEmpty(vo.getJenisKelamin())) {
			model.setJenisKelamin(jenisKelaminConverter.transferVOToModel(vo.getJenisKelamin(), new JenisKelamin()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getAgama())) {
			model.setAgama(agamaConverter.transferVOToModel(vo.getAgama(), new Agama()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getStatus())) {
			model.setStatus(statusKawinConverter.transferVOToModel(vo.getStatus(), new StatusKawin()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getDegree())) {
			model.setDegree(degreeConverter.transferVOToModel(vo.getDegree(), new Degree()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getPosisiLamaran())) {
			model.setPosisiLamaran(
					posisiLamaranConverter.transferVOToModel(vo.getPosisiLamaran(), new PosisiLamaran()));
		}

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

		return result;
	}

	@Override
	public Map<String, Object> findDataPelamar() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("items", JsonUtil.ToMaps(isianPelamarDao.findAll()));
		return result;
	}
}