package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
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 com.jasamedika.medifirst2000.constants.Master;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.NilaiKelompokJabatanDao;
import com.jasamedika.medifirst2000.entities.KelompokJabatan;
import com.jasamedika.medifirst2000.entities.NilaiKelompokJabatan;
import com.jasamedika.medifirst2000.service.NilaiKelompokJabatanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KelompokJabatanVO;
import com.jasamedika.medifirst2000.vo.NilaiKelompokJabatanVO;
import com.jasamedika.medifirst2000.vo.PlafonRemunerasiKemenkeuVO;

/**
 * Implement class for KelompokJabatanService
 * 
 * @author Generator
 * @param <T>
 */
@Service("nilaiKelompokJabatanService")
public class NilaiKelompokJabatanServiceImpl extends BaseVoServiceImpl implements NilaiKelompokJabatanService {

	@Autowired
	private BaseConverterImpl<NilaiKelompokJabatanVO, NilaiKelompokJabatan> kelompokJabatanConverter;

	@Autowired
	private BaseConverterImpl<KelompokJabatanVO, KelompokJabatan> kelJabatanConverter;

	@Autowired
	private NilaiKelompokJabatanDao nilaiKelompokJabatanDao;

	@Override
	public Map<String, Object> saveNilaiKelompokJabatan(NilaiKelompokJabatanVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		NilaiKelompokJabatan model = new NilaiKelompokJabatan();
		model = kelompokJabatanConverter.transferVOToModel(vo, model);
		if (CommonUtil.isNotNullOrEmpty(vo.getKelompokJabatan())) {
			model.setKelompokJabatan(
					kelJabatanConverter.transferVOToModel(vo.getKelompokJabatan(), new KelompokJabatan()));
		}
		NilaiKelompokJabatan resultDomain = nilaiKelompokJabatanDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("id", resultDomain.getId());
		}

		return result;
	}

	@Override
	public Map<String, Object> saveNilaiKelompokJabatan(PlafonRemunerasiKemenkeuVO vo) {
		Map<String, Object> result = new HashMap<>();
		List<Integer> listId = nilaiKelompokJabatanDao.findIdsByGrade(vo.getGrade());

		List<NilaiKelompokJabatan> models = new ArrayList<NilaiKelompokJabatan>();
		for (Integer id : listId) {
			NilaiKelompokJabatan model = nilaiKelompokJabatanDao.findOne(id);
			model.setNilaiTerendah(vo.getNilaiTerendah());
			model.setNilaiTertinggi(vo.getNilaiTertinggi());
			model.setGajiHonorarium(vo.getGajiHonorarium());
			model.setMinInsentif(vo.getMinInsentif());
			model.setMaxInsentif(vo.getMaxInsentif());
			model.setMaxTotalRemunerasi(vo.getMaxTotalRemunerasi());
			models.add(model);
		}

		Iterable<NilaiKelompokJabatan> rs = nilaiKelompokJabatanDao.save(models);
		if (CommonUtil.isNotNullOrEmpty(rs)) {
			result.put("ids", listId);
		}

		return result;
	}

	@Override
	public Map<String, Object> getNilaiKelompokJabatan(Integer id) {
		Map<String, Object> result = new HashMap<String, Object>();
		NilaiKelompokJabatan model = nilaiKelompokJabatanDao.getNilaiKelompokJabatan(id);
		try {
			result = model.ToMap();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public List<Map<String, Object>> getAllByStatusEnabled() {
		List<Map<String, Object>> result = nilaiKelompokJabatanDao.getAllByStatusEnabled();

		return result;
	}

	@Override
	public List<Map<String, Object>> getPlafonRemunerasi() {
		List<Map<String, Object>> result = new ArrayList<>();
		List<Integer> listGrade = new ArrayList<>();

		List<Map<String, Object>> data = nilaiKelompokJabatanDao.findPlafonRemunerasi();
		if (CommonUtil.isNotNullOrEmpty(data)) {
			for (Map<String, Object> map : data) {
				if (!listGrade.contains(map.get("grade"))) {
					listGrade.add(Integer.valueOf(map.get("grade").toString()));
					result.add(map);
				}
			}
		}

		return result;
	}

	@Override
	public List<Map<String, Object>> getPenempatanEvaluasiJabatan() {
		List<Map<String, Object>> result = new ArrayList<>();
		List<Map<String, Object>> filter = new ArrayList<>();

		List<Map<String, Object>> data = nilaiKelompokJabatanDao
				.findPenempatanEvaluasiJabatan(Arrays.asList(Master.KategoryPegawai.REMUN));
		for (Map<String, Object> map : data) {
			boolean isContained = false;
			for (Map<String, Object> mapFilter : filter) {
				if (map.get("idPegawai").equals(mapFilter.get("idPegawai"))
						&& map.get("idJabatan").equals(mapFilter.get("idJabatan"))) {
					isContained = true;
					break;
				}
			}
			if (!isContained) {
				Map<String, Object> mapFilter = new HashMap<>();
				mapFilter.put("idPegawai", map.get("idPegawai"));
				mapFilter.put("idJabatan", map.get("idJabatan"));
				filter.add(mapFilter);
			}
		}

		for (Map<String, Object> mapFilter : filter) {
			for (Map<String, Object> map : data) {
				if (map.get("idPegawai").equals(mapFilter.get("idPegawai"))
						&& map.get("idJabatan").equals(mapFilter.get("idJabatan"))) {
					result.add(map);
					break;
				}
			}
		}

		return result;
	}

}