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.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PenilaianKinerjaSatpamDao;
import com.jasamedika.medifirst2000.entities.MappingKualitas;
import com.jasamedika.medifirst2000.entities.MappingSikapPerilaku;
import com.jasamedika.medifirst2000.entities.MappingUraianTugas;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PenilaianKinerjaSatpam;
import com.jasamedika.medifirst2000.service.PenilaianKinerjaSatpamService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.MappingKualitasVO;
import com.jasamedika.medifirst2000.vo.MappingSikapPerilakuVO;
import com.jasamedika.medifirst2000.vo.MappingUraianTugasVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PenilaianKinerjaSatpamVO;

@Service("PenilaianKinerjaSatpamService")
public class PenilaianKinerjaSatpamServiceImpl implements PenilaianKinerjaSatpamService {

	@Autowired
	private PenilaianKinerjaSatpamDao penilaianKinerjaSatpamDao;

	@Autowired
	private BaseConverterImpl<PenilaianKinerjaSatpamVO, PenilaianKinerjaSatpam> penilaianKinerjaSatpamConverter;

	@Autowired
	private BaseConverterImpl<MappingUraianTugasVO, MappingUraianTugas> mappingUraianTugasConverter;

	@Autowired
	private BaseConverterImpl<MappingKualitasVO, MappingKualitas> mappingKualitasConverter;

	@Autowired
	private BaseConverterImpl<MappingSikapPerilakuVO, MappingSikapPerilaku> mappingSikapPerilakuConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Override
	public Map<String, Object> savePenilaianKinerjaSatpam(PenilaianKinerjaSatpamVO vo) {

		Map<String, Object> result = new HashMap<String, Object>();

		try {
			System.out.println("==================START SAVE PENILAIAN KINERJA SATPAM======================");

			PenilaianKinerjaSatpam kinerjaSatpam = penilaianKinerjaSatpamConverter.transferVOToModel(vo,
					new PenilaianKinerjaSatpam());

			// set tgl penilaian
			kinerjaSatpam.setTglPenilaian(DateUtil.getShortDate());

			// set nama pemberi nilai
			if (CommonUtil.isNotNullOrEmpty(vo.getPemberiNilai())) {
				kinerjaSatpam.setPemberiNilai(pegawaiConverter.transferVOToModel(vo.getPemberiNilai(), new Pegawai()));
			}

			// set mapping penilaian kinerja satpam (uraian tugas)
			if (CommonUtil.isNotNullOrEmpty(vo.getMapUraianTugas())) {

				Integer totalUraian = 0, nilaiUraian = 0, nilaiKualitas = 0, nilaiSikapPerilaku = 0, totalKualitas = 0;
				Integer totalSikapPerilaku = 0, grandTotal = 0;

				List<MappingUraianTugas> listMapUraianTugas = new ArrayList<MappingUraianTugas>();
				for (MappingUraianTugasVO modelMapUraianTugas : vo.getMapUraianTugas()) {
					MappingUraianTugas mappingUraianTugas = mappingUraianTugasConverter
							.transferVOToModel(modelMapUraianTugas, new MappingUraianTugas());
					mappingUraianTugas.setPenilaianKinerjaSatpam(kinerjaSatpam);
					listMapUraianTugas.add(mappingUraianTugas);
				}
				for (int i = 0; i < listMapUraianTugas.size(); i++) {
					nilaiUraian = listMapUraianTugas.get(i).getNilaiUraianTugas();
					listMapUraianTugas.get(i).setNilaiUraianTugas(nilaiUraian);
					totalUraian = totalUraian + listMapUraianTugas.get(i).getNilaiUraianTugas();
				}
				// set total nilai uraian tugas
				kinerjaSatpam.setTotalNilaiUraianTugas(totalUraian);
				kinerjaSatpam.setMapUraianTugas(listMapUraianTugas);

				// set mapping penilaian kinerja satpam (kualitas)
				if (CommonUtil.isNotNullOrEmpty(vo.getMapKualitas())) {
					List<MappingKualitas> listMapKualitas = new ArrayList<MappingKualitas>();
					for (MappingKualitasVO modelMapKualitas : vo.getMapKualitas()) {
						MappingKualitas mappingKualitas = mappingKualitasConverter.transferVOToModel(modelMapKualitas,
								new MappingKualitas());
						mappingKualitas.setPenilaianKinerjaSatpam(kinerjaSatpam);
						listMapKualitas.add(mappingKualitas);
					}
					for (int i = 0; i < listMapKualitas.size(); i++) {
						nilaiKualitas = listMapKualitas.get(i).getNilaiKualitas();
						listMapKualitas.get(i).setNilaiKualitas(nilaiKualitas);
						totalKualitas = totalKualitas + listMapKualitas.get(i).getNilaiKualitas();
					}
					// set total nilai kualitas
					kinerjaSatpam.setTotalNilaiKualitas(totalKualitas);
					kinerjaSatpam.setMapKualitas(listMapKualitas);
				}

				// set mapping penilaian kinerja satpam (sikap perilaku)
				if (CommonUtil.isNotNullOrEmpty(vo.getMapSikapPerilaku())) {
					List<MappingSikapPerilaku> listMapSikapPerilaku = new ArrayList<MappingSikapPerilaku>();
					for (MappingSikapPerilakuVO modelMapSikapPerilaku : vo.getMapSikapPerilaku()) {
						MappingSikapPerilaku mappingSikapPerilaku = mappingSikapPerilakuConverter
								.transferVOToModel(modelMapSikapPerilaku, new MappingSikapPerilaku());
						mappingSikapPerilaku.setPenilaianKinerjaSatpam(kinerjaSatpam);
						listMapSikapPerilaku.add(mappingSikapPerilaku);
					}
					for (int i = 0; i < listMapSikapPerilaku.size(); i++) {
						nilaiSikapPerilaku = listMapSikapPerilaku.get(i).getNilaiSikapPerilaku();
						listMapSikapPerilaku.get(i).setNilaiSikapPerilaku(nilaiSikapPerilaku);
						totalSikapPerilaku = totalSikapPerilaku + listMapSikapPerilaku.get(i).getNilaiSikapPerilaku();
					}
					// set total nilai sikap perilaku
					kinerjaSatpam.setTotalNilaiSikapPerilaku(totalSikapPerilaku);
					kinerjaSatpam.setMapSikapPerilaku(listMapSikapPerilaku);
				}

				// set grand total
				grandTotal = totalUraian + totalKualitas + totalSikapPerilaku;
				kinerjaSatpam.setGrandTotal(grandTotal);
			}

			PenilaianKinerjaSatpam resultModel = penilaianKinerjaSatpamDao.save(kinerjaSatpam);
			result.put("noRecPenilaianKinerjaSatpam", resultModel.noRec);
			System.out.println("==================END SAVE PENILAIAN KINERJA SATPAM======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

}
