package com.jasamedika.medifirst2000.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
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 com.jasamedika.medifirst2000.constants.Master;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.HabsenDao;
import com.jasamedika.medifirst2000.dao.IndikatorKualitasDao;
import com.jasamedika.medifirst2000.dao.KategoriPenilaianKualitasDao;
import com.jasamedika.medifirst2000.dao.PegawaiDao;
import com.jasamedika.medifirst2000.dao.PegawaiJadwalKerjaDao;
import com.jasamedika.medifirst2000.dao.ProgramKegiatanDao;
import com.jasamedika.medifirst2000.dao.UraianKegiatanDao;
import com.jasamedika.medifirst2000.entities.IndikatorKualitas;
import com.jasamedika.medifirst2000.entities.Jabatan;
import com.jasamedika.medifirst2000.entities.KategoriPenilaianKualitas;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.ProgramKegiatan;
import com.jasamedika.medifirst2000.entities.UraianKegiatan;
import com.jasamedika.medifirst2000.service.RemunerasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.IndikatorKualitasVO;
import com.jasamedika.medifirst2000.vo.JabatanVO;
import com.jasamedika.medifirst2000.vo.KategoriPenilaianKualitasVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.ProgramKegiatanVO;
import com.jasamedika.medifirst2000.vo.UraianKegiatanVO;

@Service("RemunerasiService")
public class RemunerasiServiceImpl extends BaseVoServiceImpl implements RemunerasiService {

	@Autowired
	private BaseConverterImpl<ProgramKegiatanVO, ProgramKegiatan> programKegiatanConverter;

	@Autowired
	private BaseConverterImpl<UraianKegiatanVO, UraianKegiatan> uraianKegiatanConverter;

	@Autowired
	private BaseConverterImpl<IndikatorKualitasVO, IndikatorKualitas> indikatorKualitasConverter;

	@Autowired
	private BaseConverterImpl<KategoriPenilaianKualitasVO, KategoriPenilaianKualitas> kategoriPenilaianKualitasConverter;

	@Autowired
	private BaseConverterImpl<JabatanVO, Jabatan> jabatanConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private ProgramKegiatanDao programKegiatanDao;

	@Autowired
	private UraianKegiatanDao uraianKegiatanDao;

	@Autowired
	private IndikatorKualitasDao indikatorKualitasDao;

	@Autowired
	private KategoriPenilaianKualitasDao kategoriPenilaianKualitasDao;

	@Autowired
	private PegawaiJadwalKerjaDao pegawaiJadwalKerjaDao;

	@Autowired
	private PegawaiDao pegawaiDao;

	@Autowired
	private HabsenDao habsenDao;

	@Override
	public Map<String, Object> saveProgramKegiatan(ProgramKegiatanVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		ProgramKegiatan model = new ProgramKegiatan();
		model = programKegiatanConverter.transferVOToModel(vo, model);
		if (CommonUtil.isNotNullOrEmpty(vo.getJabatanPelaksanaKegiatan())) {
			model.setJabatanPelaksanaKegiatan(
					jabatanConverter.transferVOToModel(vo.getJabatanPelaksanaKegiatan(), new Jabatan()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getJabatanAtasanLangsung())) {
			model.setJabatanAtasanLangsung(
					jabatanConverter.transferVOToModel(vo.getJabatanAtasanLangsung(), new Jabatan()));
		}
		if (CommonUtil.isNotNullOrEmpty(vo.getPegawaiAtasanLangsung())) {
			model.setPegawaiAtasanLangsung(
					pegawaiConverter.transferVOToModel(vo.getPegawaiAtasanLangsung(), new Pegawai()));
		}
		ProgramKegiatan resultDomain = programKegiatanDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noRec", resultDomain.getNoRec());
		}
		return result;
	}

	@Override
	public Map<String, Object> saveUraianKegiatan(UraianKegiatanVO vo) {
		Map<String, Object> result = new HashMap<>();
		UraianKegiatan model = new UraianKegiatan();
		model = uraianKegiatanConverter.transferVOToModel(vo, model);
		if (CommonUtil.isNotNullOrEmpty(vo.getProgramKegiatan())) {
			model.setProgramKegiatan(
					programKegiatanConverter.transferVOToModel(vo.getProgramKegiatan(), new ProgramKegiatan()));
		}
		UraianKegiatan resultDomain = uraianKegiatanDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noRec", resultDomain.getNoRec());
		}
		return result;
	}

	@Override
	public Map<String, Object> saveIndikatorKualitas(IndikatorKualitasVO vo) {
		Map<String, Object> result = new HashMap<>();
		IndikatorKualitas model = new IndikatorKualitas();
		model = indikatorKualitasConverter.transferVOToModel(vo, model);
		if (CommonUtil.isNotNullOrEmpty(vo.getUraianKegiatan())) {
			model.setUraianKegiatan(
					uraianKegiatanConverter.transferVOToModel(vo.getUraianKegiatan(), new UraianKegiatan()));
		}
		// Set<KategoriPenilaianKualitas> listKategori = new HashSet<>();
		// for (KategoriPenilaianKualitasVO map : vo.getListKategori()) {
		// KategoriPenilaianKualitas kategori =
		// kategoriPenilaianKualitasConverter.transferVOToModel(map, new
		// KategoriPenilaianKualitas());
		// kategori.setIndikatorKualitas(model);
		// kategori.setStatusEnabled(true);
		// listKategori.add(kategori);
		// }
		// model.setListKategori(listKategori);
		IndikatorKualitas resultDomain = indikatorKualitasDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noRec", resultDomain.getNoRec());
		}
		return result;
	}

	@Override
	public Map<String, Object> saveKategoriPenilaianKualitas(KategoriPenilaianKualitasVO vo) {
		Map<String, Object> result = new HashMap<>();
		KategoriPenilaianKualitas model = new KategoriPenilaianKualitas();
		model = kategoriPenilaianKualitasConverter.transferVOToModel(vo, model);
		KategoriPenilaianKualitas resultDomain = kategoriPenilaianKualitasDao.save(model);
		if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
			result.put("noRec", resultDomain.getNoRec());
		}
		return result;
	}

	@Override
	public Map<String, Object> getKuantitasCapaianHariKerja(Integer idPegawai, String bulan) {
		Map<String, Object> result = new HashMap<>();

		try {
			List<Map<String, Object>> data = new ArrayList<>();
			List<Map<String, Object>> detailReguler = new ArrayList<>();
			List<Map<String, Object>> detailOnSite = new ArrayList<>();

			SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");

			Date convertedDate = df.parse(bulan);

			Calendar c = Calendar.getInstance();
			c.setTime(convertedDate);
			c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));

			Date tglAwal = ddf.parse(bulan + "-01 00:00:00");
			Date tglAkhir = ddf.parse(sdf.format(c.getTime()) + " 23:59:59");

			result = pegawaiDao.getPegawaiCapaianKuantitas(idPegawai);
			List<Map<String, Object>> jadwal = pegawaiJadwalKerjaDao.getKuantitasHariKerja(idPegawai, tglAwal,
					tglAkhir);
			List<Map<String, Object>> presensi = habsenDao.getKuantitasCapaianHariKerja(idPegawai, tglAwal, tglAkhir);

			if (CommonUtil.isNotNullOrEmpty(jadwal)) {
				for (Map<String, Object> j : jadwal) {
					Map<String, Object> mapReguler = new HashMap<>();
					Map<String, Object> mapOnSite = new HashMap<>();
					for (Map<String, Object> p : presensi) {
						if (Arrays.asList(Master.KelompokShift.ALL_NONSHIFT).contains(result.get("shiftKerjaId"))
								&& result.get("jenisPegawaiId").equals(Master.JenisPegawai.DOKTER)
								&& Arrays.asList(Master.ShiftKerja.PSM).contains(j.get("idKode"))
								&& p.get("tr_date").equals(j.get("tanggal"))
								&& CommonUtil.isNotNullOrEmpty(p.get("tr_date"))) {
							mapReguler.put("count", 1);
							mapReguler.put("tanggal", j.get("tanggal"));
							detailReguler.add(mapReguler);

							mapOnSite.put("count", 1);
							mapOnSite.put("tanggal", j.get("tanggal"));
							detailOnSite.add(mapOnSite);

							break;
						} else if (p.get("tr_date").equals(j.get("tanggal"))
								&& CommonUtil.isNotNullOrEmpty(p.get("tr_date"))) {
							mapReguler.put("count", 1);
							mapReguler.put("tanggal", j.get("tanggal"));
							detailReguler.add(mapReguler);

							break;
						}
					}
				}
			}

			for (int i = 0; i < 2; i++) {
				Map<String, Object> map = new HashMap<>();
				if (i == 0) {
					map.put("hariKerja", "Reguler");
					map.put("detail", detailReguler);
				} else if (i == 1) {
					map.put("hariKerja", "On Site");
					map.put("detail", detailOnSite);
				}
				if (CommonUtil.isNotNullOrEmpty(map.get("detail"))) {
					data.add(map);
				}
			}

			result.put("kuantitas", data);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}

}
