package com.jasamedika.medifirst2000.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.dao.PegawaiHistoriRekapDao;
import com.jasamedika.medifirst2000.dao.PegawaiHistoriRekapGajiDao;
import com.jasamedika.medifirst2000.dao.custom.PegawaiHistoriRekapGajiDaoCustom;
import com.jasamedika.medifirst2000.service.InformasiDaftarGajiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

@Service("InformasiDaftarGajiService")
public class InformasiDaftarGajiServiceImpl implements InformasiDaftarGajiService{

	@Autowired
	private PegawaiHistoriRekapGajiDao pegawaiHistoriRekapGajiDao;
	
	@Autowired
	private PegawaiHistoriRekapGajiDaoCustom pegawaiHistoriRekapGajiDaoCustom;
	
	@Autowired
	private PegawaiHistoriRekapDao pegawaiHistoriRekapDao;
	
	@Override
	public Map<String, Object> getPeriodeGolongan(){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listPeriode = pegawaiHistoriRekapGajiDao.getPeriodeRekap();
		if(CommonUtil.isNotNullOrEmpty(listPeriode)){
			result.put("listPeriode", listPeriode);
		}
		List<Map<String, Object>> listGolongan = new ArrayList<>();
		for(int i=1; i<=5; i++){
			Map<String, Object> golongan = new HashMap<>();
			if(i==1){
				golongan.put("id", i);
				golongan.put("namaGolongan", "Golongan I");
				listGolongan.add(golongan);
			} else if(i==2){
				golongan.put("id", i);
				golongan.put("namaGolongan", "Golongan II");
				listGolongan.add(golongan);
			} else if(i==3){
				golongan.put("id", i);
				golongan.put("namaGolongan", "Golongan III");
				listGolongan.add(golongan);
			} else if(i==4){
				golongan.put("id", i);
				golongan.put("namaGolongan", "Golongan IV");
				listGolongan.add(golongan);
			} else if(i==5){
				golongan.put("id", i);
				golongan.put("namaGolongan", "Rekapitulasi");
				listGolongan.add(golongan);
			}
		}
		if(CommonUtil.isNotNullOrEmpty(listGolongan)){
			result.put("listGolongan", listGolongan);
		}
		
		return result;
	}
	
	@Override
	public Map<String, Object> getDaftarGajiPns(String periode){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = pegawaiHistoriRekapGajiDaoCustom.getDaftarGajiPns(periode);
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getDtailGajiPns(String noRec, Integer id, String namaPegawai, String periode){
		Map<String, Object> result = new HashMap<>();
		
		List<Map<String, Object>> listData = new ArrayList<>();
		
		List<Map<String, Object>> listGajiPegawai = pegawaiHistoriRekapGajiDaoCustom.getPegawaiPns(noRec, id, namaPegawai, periode);
				
		//Double total = 0.00;
		BigDecimal total=new BigDecimal(0);
		if(CommonUtil.isNotNullOrEmpty(listGajiPegawai)){
			for(Map<String, Object> data : listGajiPegawai){
				//add by askur 07-04-2017
				Integer pegawaiId=(Integer) data.get("pegawaiId");
				Map<String,Object> mapTotalHarga=pegawaiHistoriRekapDao.getTotalHarga(noRec,pegawaiId);
				BigDecimal totalHarga=new BigDecimal(0);
				if(mapTotalHarga!=null){
					BigDecimal totalHargaAdd=(BigDecimal) mapTotalHarga.get("totalGajiAdd");
					BigDecimal totalHargaMin=(BigDecimal) mapTotalHarga.get("totalGajiMin");
					totalHarga=totalHarga.add(totalHargaAdd);
					totalHarga=totalHarga.add(totalHargaMin);
				}
				//end
				//edit by askur 10042017	
				//total=total+((BigDecimal)data.get("totalHarga")).doubleValue();
				total=total.add(totalHarga);
				//
				Map<String, Object> map = new HashMap<>();
				map.put("nip", data.get("nip"));
				map.put("namaPegawai", data.get("namaPegawai"));
				map.put("golongan", data.get("golongan"));
				//edit by askur 07-04-2017
				//map.put("totalHarga", data.get("totalHarga"));
				//map.put("detail", pegawaiHistoriRekapGajiDaoCustom.getDtailGajiPns((Integer)data.get("pegawaiId"), periode));
				map.put("totalHarga", totalHarga);
				map.put("detail", pegawaiHistoriRekapGajiDaoCustom.getDtailGajiPnsAddNoRecStrukHistori(noRec,(Integer)data.get("pegawaiId"), periode));
				//end				
				listData.add(map);
			}
		}
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
			result.put("totalKeseluruhan", total);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getDaftarGajiPkwt(String periode){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = pegawaiHistoriRekapGajiDaoCustom.getDaftarGajiPkwt(periode);
		if(CommonUtil.isNotNullOrEmpty(listData)){
			result.put("listData", listData);
		}
		return result;
	}
	
	@Override
	public Map<String, Object> getDtailGajiPkwt(String noRec, Integer id){
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = new ArrayList<>();
		if(CommonUtil.isNotNullOrEmpty(id)){
			listData = pegawaiHistoriRekapGajiDaoCustom.getDtailGajiPkwt(noRec, id);
		}
		
		List<Map<String, Object>> listResult = new ArrayList<>();
		if(CommonUtil.isNotNullOrEmpty(listData)){
			Double total = 0.00;
			for(Map<String, Object> data : listData){
				total=total+((BigDecimal)data.get("harga")).doubleValue();
				
				Map<String, Object> dataMap = new HashMap<>();
				dataMap.put("nip", data.get("nip"));
				//-------------------------------------------------
				dataMap.put("namaPegawai", data.get("namaPegawai"));
				//-------------------------------------------------
				dataMap.put("pendidikan", data.get("pendidikan"));
				//-------------------------------------------------
				dataMap.put("golongan", data.get("golongan"));
				//-------------------------------------------------
				Date tglMasuk = (Date) dataMap.get("tglMasuk");
				Date tglKini = DateUtil.now();
				Long jumlahHari = Math.abs(tglKini.getTime() - tglMasuk.getTime());
				Long toDay = TimeUnit.MILLISECONDS.toDays(jumlahHari);
				Double masaKerjaTahun = (toDay.doubleValue()+1)/365;
				dataMap.put("masaKerja", Math.ceil(masaKerjaTahun));
				//-------------------------------------------------
				listResult.add(dataMap);
			}
			result.put("listDetail", listResult);
			result.put("total", total);
		}
		return result;
	}
}
