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.KedudukanVO;

	import com.jasamedika.medifirst2000.entities.Kedudukan;


import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.MutasiPegawaiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.entities.MutasiPegawai;
import com.jasamedika.medifirst2000.entities.StatusKawin;
import com.jasamedika.medifirst2000.entities.StatusPerkawinanPegawai;
import com.jasamedika.medifirst2000.vo.MutasiPegawaiVO;
import com.jasamedika.medifirst2000.vo.StatusKawinVO;
import com.jasamedika.medifirst2000.vo.StatusPerkawinanPegawaiVO;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.MutasiPegawaiDao;

/**
 * Implement class for MutasiPegawaiService
 * 
 * @author Generator
 * @param <T>
 */
@Service("mutasiPegawaiService")
public class MutasiPegawaiServiceImpl extends BaseVoServiceImpl implements MutasiPegawaiService 
{	
	@Autowired
	private BaseConverterImpl<MutasiPegawaiVO,MutasiPegawai> mutasiPegawaiConverter;
	
	@Autowired
	private BaseConverterImpl<KedudukanVO,Kedudukan> kedudukanConverter;
	
	@Autowired
	private BaseConverterImpl<StatusPerkawinanPegawaiVO, StatusPerkawinanPegawai> statusKawinConverter;


	@Autowired
	private MutasiPegawaiDao mutasiPegawaiDao;

	
	@Override
	public Map<String, Object> saveMutasiPegawai(MutasiPegawaiVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
//		MutasiPegawai model = new MutasiPegawai();
		MutasiPegawai model = mutasiPegawaiConverter.transferVOToModel(vo,new MutasiPegawai());
		if (CommonUtil.isNotNullOrEmpty(vo.getKedudukan()))
		{
			model.setKedudukan(kedudukanConverter.transferVOToModel(vo.getKedudukan(), new Kedudukan()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getStatusKawin()))
		{
			model.setStatusKawin(statusKawinConverter.transferVOToModel(vo.getStatusKawin(), new StatusPerkawinanPegawai()));
		}

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

		return result;
	}


	@Override
	public Map<String, Object> noAgenda() {
		Map<String, Object> result = new HashMap<>();
	 	String monthNow = String.valueOf(DateUtil.getMonthFromDate(new Date()));
		String yearNow = String.valueOf(DateUtil.getYearFromDate(new Date())).substring(2, 4);
		
		 	if (monthNow.length() == 1)
			monthNow = "0" + monthNow;
		
		List<String> list = mutasiPegawaiDao.noAgenda("PMP" + yearNow + monthNow);
		if (CommonUtil.isNotNullOrEmpty(list)) {
			Integer data = Integer.parseInt(list.get(list.size() - 1).substring(6)) + 1;
			result.put("noAgenda", "PMP" + yearNow + monthNow + String.format("%04d",data));
		}  else {
			result.put("noAgenda", "PMP" + yearNow + monthNow + "0001" );
		}
	 
		return result;
	}
}