package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
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.dao.PenilaianKinerjaSatpamDao;
import com.jasamedika.medifirst2000.dto.PenilaianKinerjaSatpamDto;
import com.jasamedika.medifirst2000.service.DaftarPenilaianKinerjaSatpamService;
import com.jasamedika.medifirst2000.util.DateUtil;

@Service("DaftarPenilaianKinerjaSatpamService")
public class DaftarPenilaianKinerjaSatpamServiceImpl extends BaseVoServiceImpl
		implements DaftarPenilaianKinerjaSatpamService {

	@Autowired
	private PenilaianKinerjaSatpamDao penilaianKinerjaSatpamDao;

	@Override
	public Map<String, Object> findPenilaianKinerjaSatpamByNamaSatpam(String namaPegawai) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("=======START FIND DAFTAR PENILAIAN KINERJA SATPAM BY NAMA PEGAWAI=======");
			List<PenilaianKinerjaSatpamDto> dtoList = new ArrayList<PenilaianKinerjaSatpamDto>();
			List<Map<String, Object>> resultMap = penilaianKinerjaSatpamDao
					.findPenilaianKinerjaSatpamByNamaSatpam(namaPegawai);
			for (int i = 0; i < resultMap.size(); i++) {
				PenilaianKinerjaSatpamDto dto = new PenilaianKinerjaSatpamDto();
				dto.setNoRecPenilaian(resultMap.get(i).get("noRecPenilaian").toString());
				dto.setTglPenilaian(DateUtil.toDate(resultMap.get(i).get("tglPenilaian").toString()));
				dto.setTglPeriodeAwal(DateUtil.toDate(resultMap.get(i).get("tglPeriodeAwal").toString()));
				dto.setTglPeriodeAkhir(DateUtil.toDate(resultMap.get(i).get("tglPeriodeAkhir").toString()));
				dto.setNamaSatpam(resultMap.get(i).get("namaPegawai").toString());
				dto.setNamaPemberiNilai(resultMap.get(i).get("namaPemberiNilai").toString());
				dto.setTotalUraianTugas(Integer.parseInt(resultMap.get(i).get("totalNilaiUraianTugas").toString()));
				dto.setTotalKualitas(Integer.parseInt(resultMap.get(i).get("totalNilaiKualitas").toString()));
				dto.setTotalSikapPerilaku(Integer.parseInt(resultMap.get(i).get("totalNilaiSikapPerilaku").toString()));
				dto.setGrandTotal(Integer.parseInt(resultMap.get(i).get("grandTotal").toString()));
				dto.setListMappingUraianTugas(new ArrayList<Map<String, Object>>());
				dto.setListMappingKualitas(new ArrayList<Map<String, Object>>());
				dto.setListMappingSikapPerilaku(new ArrayList<Map<String, Object>>());
				// get mapping uraian tugas
				List<Map<String, Object>> listUraianTugas = penilaianKinerjaSatpamDao
						.findMappingUraianTugas(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listUraianTugas) {
					Map<String, Object> dataUraianTugas = new HashMap<String, Object>();
					dataUraianTugas.put("uraianTugas", list.get("uraianTugas").toString());
					dataUraianTugas.put("nilaiUraianTugas", list.get("nilaiUraianTugas").toString());
					dto.getListMappingUraianTugas().add(dataUraianTugas);
				}
				// get mapping kualitas
				List<Map<String, Object>> listKualitas = penilaianKinerjaSatpamDao
						.findMappingKualitas(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listKualitas) {
					Map<String, Object> dataKualitas = new HashMap<String, Object>();
					dataKualitas.put("kualitas", list.get("kualitas").toString());
					dataKualitas.put("nilaiKualitas", list.get("nilaiKualitas").toString());
					dto.getListMappingKualitas().add(dataKualitas);
				}
				// get mapping kualitas
				List<Map<String, Object>> listSikapPerilaku = penilaianKinerjaSatpamDao
						.findMappingSikapPerilaku(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listSikapPerilaku) {
					Map<String, Object> dataSikapPerilaku = new HashMap<String, Object>();
					dataSikapPerilaku.put("sikapPerilaku", list.get("sikapPerilaku").toString());
					dataSikapPerilaku.put("nilaiSikapPerilaku", list.get("nilaiSikapPerilaku").toString());
					dto.getListMappingSikapPerilaku().add(dataSikapPerilaku);
				}
				dtoList.add(dto);
			}
			result.put("result", dtoList);
			System.out.println("=======END FIND DAFTAR PENILAIAN KINERJA SATPAM BY NAMA PEGAWAI=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> getPenilaianKinerjaSatpam() {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("=======START GET ALL PENILAIAN KINERJA SATPAM=======");
			List<PenilaianKinerjaSatpamDto> dtoList = new ArrayList<PenilaianKinerjaSatpamDto>();
			List<Map<String, Object>> resultMap = penilaianKinerjaSatpamDao.getPenilaianKinerjaSatpam();
			for (int i = 0; i < resultMap.size(); i++) {
				PenilaianKinerjaSatpamDto dto = new PenilaianKinerjaSatpamDto();
				dto.setNoRecPenilaian(resultMap.get(i).get("noRecPenilaian").toString());
				dto.setTglPenilaian(DateUtil.toDate(resultMap.get(i).get("tglPenilaian").toString()));
				dto.setTglPeriodeAwal(DateUtil.toDate(resultMap.get(i).get("tglPeriodeAwal").toString()));
				dto.setTglPeriodeAkhir(DateUtil.toDate(resultMap.get(i).get("tglPeriodeAkhir").toString()));
				dto.setNamaSatpam(resultMap.get(i).get("namaPegawai").toString());
				dto.setNamaPemberiNilai(resultMap.get(i).get("namaPemberiNilai").toString());
				dto.setTotalUraianTugas(Integer.parseInt(resultMap.get(i).get("totalNilaiUraianTugas").toString()));
				dto.setTotalKualitas(Integer.parseInt(resultMap.get(i).get("totalNilaiKualitas").toString()));
				dto.setTotalSikapPerilaku(Integer.parseInt(resultMap.get(i).get("totalNilaiSikapPerilaku").toString()));
				dto.setGrandTotal(Integer.parseInt(resultMap.get(i).get("grandTotal").toString()));
				dto.setListMappingUraianTugas(new ArrayList<Map<String, Object>>());
				dto.setListMappingKualitas(new ArrayList<Map<String, Object>>());
				dto.setListMappingSikapPerilaku(new ArrayList<Map<String, Object>>());
				// get mapping uraian tugas
				List<Map<String, Object>> listUraianTugas = penilaianKinerjaSatpamDao
						.findMappingUraianTugas(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listUraianTugas) {
					Map<String, Object> dataUraianTugas = new HashMap<String, Object>();
					dataUraianTugas.put("uraianTugas", list.get("uraianTugas").toString());
					dataUraianTugas.put("nilaiUraianTugas", list.get("nilaiUraianTugas").toString());
					dto.getListMappingUraianTugas().add(dataUraianTugas);
				}
				// get mapping kualitas
				List<Map<String, Object>> listKualitas = penilaianKinerjaSatpamDao
						.findMappingKualitas(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listKualitas) {
					Map<String, Object> dataKualitas = new HashMap<String, Object>();
					dataKualitas.put("kualitas", list.get("kualitas").toString());
					dataKualitas.put("nilaiKualitas", list.get("nilaiKualitas").toString());
					dto.getListMappingKualitas().add(dataKualitas);
				}
				// get mapping kualitas
				List<Map<String, Object>> listSikapPerilaku = penilaianKinerjaSatpamDao
						.findMappingSikapPerilaku(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listSikapPerilaku) {
					Map<String, Object> dataSikapPerilaku = new HashMap<String, Object>();
					dataSikapPerilaku.put("sikapPerilaku", list.get("sikapPerilaku").toString());
					dataSikapPerilaku.put("nilaiSikapPerilaku", list.get("nilaiSikapPerilaku").toString());
					dto.getListMappingSikapPerilaku().add(dataSikapPerilaku);
				}
				dtoList.add(dto);
			}
			result.put("result", dtoList);
			System.out.println("=======END GET ALL PENILAIAN KINERJA SATPAM=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> findPenilaianKinerjaSatpamByNoRec(String noRec) {

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("=======START FIND PENILAIAN KINERJA SATPAM BY NO REC=======");
			List<PenilaianKinerjaSatpamDto> dtoList = new ArrayList<PenilaianKinerjaSatpamDto>();
			List<Map<String, Object>> resultMap = penilaianKinerjaSatpamDao.findPenilaianKinerjaSatpamByNoRec(noRec);
			for (int i = 0; i < resultMap.size(); i++) {
				PenilaianKinerjaSatpamDto dto = new PenilaianKinerjaSatpamDto();
				dto.setNoRecPenilaian(resultMap.get(i).get("noRecPenilaian").toString());
				dto.setTglPenilaian(DateUtil.toDate(resultMap.get(i).get("tglPenilaian").toString()));
				dto.setTglPeriodeAwal(DateUtil.toDate(resultMap.get(i).get("tglPeriodeAwal").toString()));
				dto.setTglPeriodeAkhir(DateUtil.toDate(resultMap.get(i).get("tglPeriodeAkhir").toString()));
				dto.setNamaSatpam(resultMap.get(i).get("namaPegawai").toString());
				dto.setNamaPemberiNilai(resultMap.get(i).get("namaPemberiNilai").toString());
				dto.setTotalUraianTugas(Integer.parseInt(resultMap.get(i).get("totalNilaiUraianTugas").toString()));
				dto.setTotalKualitas(Integer.parseInt(resultMap.get(i).get("totalNilaiKualitas").toString()));
				dto.setTotalSikapPerilaku(Integer.parseInt(resultMap.get(i).get("totalNilaiSikapPerilaku").toString()));
				dto.setGrandTotal(Integer.parseInt(resultMap.get(i).get("grandTotal").toString()));
				dto.setListMappingUraianTugas(new ArrayList<Map<String, Object>>());
				dto.setListMappingKualitas(new ArrayList<Map<String, Object>>());
				dto.setListMappingSikapPerilaku(new ArrayList<Map<String, Object>>());
				// get mapping uraian tugas
				List<Map<String, Object>> listUraianTugas = penilaianKinerjaSatpamDao
						.findMappingUraianTugas(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listUraianTugas) {
					Map<String, Object> dataUraianTugas = new HashMap<String, Object>();
					dataUraianTugas.put("uraianTugas", list.get("uraianTugas").toString());
					dataUraianTugas.put("nilaiUraianTugas", list.get("nilaiUraianTugas").toString());
					dto.getListMappingUraianTugas().add(dataUraianTugas);
				}
				// get mapping kualitas
				List<Map<String, Object>> listKualitas = penilaianKinerjaSatpamDao
						.findMappingKualitas(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listKualitas) {
					Map<String, Object> dataKualitas = new HashMap<String, Object>();
					dataKualitas.put("kualitas", list.get("kualitas").toString());
					dataKualitas.put("nilaiKualitas", list.get("nilaiKualitas").toString());
					dto.getListMappingKualitas().add(dataKualitas);
				}
				// get mapping kualitas
				List<Map<String, Object>> listSikapPerilaku = penilaianKinerjaSatpamDao
						.findMappingSikapPerilaku(dto.getNoRecPenilaian());
				for (Map<String, Object> list : listSikapPerilaku) {
					Map<String, Object> dataSikapPerilaku = new HashMap<String, Object>();
					dataSikapPerilaku.put("sikapPerilaku", list.get("sikapPerilaku").toString());
					dataSikapPerilaku.put("nilaiSikapPerilaku", list.get("nilaiSikapPerilaku").toString());
					dto.getListMappingSikapPerilaku().add(dataSikapPerilaku);
				}
				dtoList.add(dto);
			}
			result.put("result", dtoList);
			System.out.println("=======END FIND PENILAIAN KINERJA SATPAM BY NO REC=======");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
