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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.MappingPegawaiToAtasanDao;
import com.jasamedika.medifirst2000.entities.MappingPegawaiToAtasan;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.service.MappingPegawaiToAtasanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.MapPegawaiJabatanToUnitKerjaVO;
import com.jasamedika.medifirst2000.vo.MappingPegawaiToAtasanVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;

@Service("MappingPegawaiToAtasanService")
public class MappingPegawaiToAtasanServiceImpl implements MappingPegawaiToAtasanService{
	
	@Autowired
	BaseConverterImpl<MappingPegawaiToAtasanVO, MappingPegawaiToAtasan> mappingpegawaiToAtasanConverter;
	
	@Autowired
	BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;
	
	@Autowired
	MappingPegawaiToAtasanDao mappingPegawaiToAtasanDao;

	@Override
	@Transactional(readOnly = false)
	public Map<String, Object> saveMappingPegawaiToAtasan(List<MappingPegawaiToAtasanVO> vos) {
		Map<String, Object> result = new HashMap<String, Object>();
		for(MappingPegawaiToAtasanVO vo : vos)
		{
			if(CommonUtil.isNotNullOrEmpty(vo.getPegawai()))
			{
				MappingPegawaiToAtasan model = mappingpegawaiToAtasanConverter.transferVOToModel(vo, new MappingPegawaiToAtasan());				
				model.setPegawai(pegawaiConverter.transferVOToModel(vo.getPegawai(),new Pegawai()));
				MappingPegawaiToAtasan currentData = mappingPegawaiToAtasanDao.findMappingPegawaiToAtasan(vo.getPegawai().getId());
				if(CommonUtil.isNotNullOrEmpty(currentData))
				{
					model.setId(currentData.getId());
				}
				if(CommonUtil.isNotNullOrEmpty(vo.getAtasanLangsung())) {
					model.setAtasanLangsung(pegawaiConverter.transferVOToModel(vo.getAtasanLangsung(),new Pegawai()));
				} else {
					model.setAtasanLangsung(currentData.getAtasanLangsung());
				}
				
				if(CommonUtil.isNotNullOrEmpty(vo.getAtasanPejabatPenilai())) {
					model.setAtasanPejabatPenilai(pegawaiConverter.transferVOToModel(vo.getAtasanPejabatPenilai(),new Pegawai()));
				} else {
					model.setAtasanPejabatPenilai(currentData.getAtasanPejabatPenilai());
				}
				
				if(CommonUtil.isNotNullOrEmpty(model))
				{
					model.setTglInput(new Date());
					MappingPegawaiToAtasan temp = mappingPegawaiToAtasanDao.save(model);
					result.put(temp.getId()+"", temp.getNoRec());
				}
			}
			
		}
		return result;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Map<String, Object>> getPegawaibawahan(Integer id) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<Pegawai> datas = mappingPegawaiToAtasanDao.findPegawaiYangDinilai(id);
		for(Pegawai data : datas)
		{
			try {
				result.add(data.ToMap());
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Map<String, Object>> getPegawaiAtasan(Integer id) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		result = mappingPegawaiToAtasanDao.findAtasanPegawai(id);
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getPegawaiAtasanByJabatan(Integer id, Integer idJabatan) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		result = mappingPegawaiToAtasanDao.findAtasanPegawaiByJabatan(id, idJabatan);
		return result;
	}
	
	@Override
	public List<Map<String, Object>> getPegawaiAtasan(Integer id, Integer idAtasan) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		result = mappingPegawaiToAtasanDao.findAtasanPegawai(id, idAtasan);
		return result;
	}
	
	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> findPegawaiAtasan(Integer id) {
		Map<String, Object> result = new HashMap<>();
		result = mappingPegawaiToAtasanDao.getAtasanPegawai(id);
		return result;
	}
	
	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> findAtasanLangsungAsPejabatPenilai(Integer idAtasanLangsung) {
		Map<String, Object> result = mappingPegawaiToAtasanDao.getAtasanLgsgAsPejabatPenilai(idAtasanLangsung);
		return result;
	}

	@Override
	@Transactional(readOnly = true)
	public List<Map<String, Object>> getMappingpegawai() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		result = mappingPegawaiToAtasanDao.findMappingPegawai();
		return result;
	}

	@Override
	public void updateFromMapPegawaiJabatanToUnitKerja(MapPegawaiJabatanToUnitKerjaVO mapp) {
		List<MappingPegawaiToAtasanVO> vos = new ArrayList<>();
		saveMappingPegawaiToAtasan(vos);
		
	}

	@Override
	public void deleteMapPegawaiToAtasan(MappingPegawaiToAtasanVO vo) {
		try {
			if(CommonUtil.isNotNullOrEmpty(vo.getPegawai())) {	
				MappingPegawaiToAtasan model = mappingpegawaiToAtasanConverter.transferVOToModel(vo, new MappingPegawaiToAtasan());
				mappingPegawaiToAtasanDao.delete(model);			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	

}
