package com.jasamedika.medifirst2000.service.impl;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.LogbookKinerjaDetailDao;
import com.jasamedika.medifirst2000.dao.LogbookKinerjaDetailLogDao;
import com.jasamedika.medifirst2000.dto.LogbookKinerjaDetailLogDto;
import com.jasamedika.medifirst2000.dto.LogbookKinerjaDetailLogReqDto;
import com.jasamedika.medifirst2000.dto.PegawaiLogDto;
import com.jasamedika.medifirst2000.entities.LogbookKinerjaDetail;
import com.jasamedika.medifirst2000.entities.LogbookKinerjaDetailLog;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.service.LogbookKinerjaDetailLogService;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.LogbookKinerjaDetailVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author salmanoe
 * @version 1.0.0
 * @since 31/10/2024
 */
@Service
@Transactional
public class LogbookKinerjaDetailLogServiceImpl implements LogbookKinerjaDetailLogService {

	@Autowired
	private LoginUserService loginUserService;

	@Autowired
	private LogbookKinerjaDetailDao logbookKinerjaDetailDao;

	@Autowired
	private LogbookKinerjaDetailLogDao logbookKinerjaDetailLogDao;

	@Autowired
	private BaseConverterImpl<LogbookKinerjaDetailVO, LogbookKinerjaDetail> catatanKinerjaConverter;

	@Override
	public void save(LogbookKinerjaDetailLogReqDto dto) {
		LogbookKinerjaDetailLog.LogbookKinerjaDetailLogBuilder logBuilder = LogbookKinerjaDetailLog.builder();
		{
			Optional<LogbookKinerjaDetail> byNoRec = logbookKinerjaDetailDao.findByNoRec(dto.getCatatanKinerjaId());
			byNoRec.ifPresent(logBuilder::catatanKinerja);
		}
		{
			Pegawai subject = loginUserService.getLoginUser().getPegawai();
			logBuilder.pegawai(subject);
		}
		LogbookKinerjaDetailLog log = logBuilder.jenisLog(dto.getJenisLog()).keterangan(dto.getKeterangan())
				.tanggalDibuat(new Date()).build();
		log.setKdProfile((short) 0);
		log.setStatusEnabled(true);
		logbookKinerjaDetailLogDao.save(log);
	}

	@Override
	public Page<LogbookKinerjaDetailLogDto> search(String indikator, String kegiatan, Long tglAwalKegiatan,
			Long tglAkhirKegiatan, String pegawai, Pageable pageable) {
		Page<LogbookKinerjaDetailLog> logPage;
		if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(kegiatan)
				&& CommonUtil.isNotNullOrEmpty(tglAwalKegiatan) && CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan)
				&& CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorKegiatanTanggalPegawai(indikator.toLowerCase(),
					kegiatan.toLowerCase(), new Date(tglAwalKegiatan), new Date(tglAkhirKegiatan),
					pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(kegiatan) && CommonUtil.isNotNullOrEmpty(tglAwalKegiatan)
				&& CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan) && CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchKegiatanTanggalPegawai(kegiatan.toLowerCase(),
					new Date(tglAwalKegiatan), new Date(tglAkhirKegiatan), pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(kegiatan)
				&& CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorKegiatanPegawai(indikator.toLowerCase(),
					kegiatan.toLowerCase(), pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(tglAwalKegiatan)
				&& CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan) && CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorTanggalPegawai(indikator.toLowerCase(),
					new Date(tglAwalKegiatan), new Date(tglAkhirKegiatan), pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(kegiatan)
				&& CommonUtil.isNotNullOrEmpty(tglAwalKegiatan) && CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorKegiatanTanggal(indikator.toLowerCase(),
					kegiatan.toLowerCase(), new Date(tglAwalKegiatan), new Date(tglAkhirKegiatan), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(tglAwalKegiatan) && CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan)
				&& CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchTanggalPegawai(new Date(tglAwalKegiatan),
					new Date(tglAkhirKegiatan), pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(kegiatan) && CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchKegiatanPegawai(kegiatan.toLowerCase(),
					pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(kegiatan) && CommonUtil.isNotNullOrEmpty(tglAwalKegiatan)
				&& CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchKegiatanTanggal(kegiatan.toLowerCase(),
					new Date(tglAwalKegiatan), new Date(tglAkhirKegiatan), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorPegawai(indikator.toLowerCase(),
					pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(tglAwalKegiatan)
				&& CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorTanggal(indikator.toLowerCase(),
					new Date(tglAwalKegiatan), new Date(tglAkhirKegiatan), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator) && CommonUtil.isNotNullOrEmpty(kegiatan)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikatorKegiatan(indikator.toLowerCase(),
					kegiatan.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(pegawai)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchPegawai(pegawai.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(tglAwalKegiatan) && CommonUtil.isNotNullOrEmpty(tglAkhirKegiatan)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchTanggal(new Date(tglAwalKegiatan),
					new Date(tglAkhirKegiatan), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(kegiatan)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchKegiatan(kegiatan.toLowerCase(), pageable);
		} else if (CommonUtil.isNotNullOrEmpty(indikator)) {
			logPage = logbookKinerjaDetailLogDao.findBySearchIndikator(indikator.toLowerCase(), pageable);
		} else {
			logPage = logbookKinerjaDetailLogDao.findAll(pageable);
		}
		List<LogbookKinerjaDetailLogDto> dtoList = new ArrayList<>();
		logPage.getContent().forEach(log -> {
			LogbookKinerjaDetailLogDto.LogbookKinerjaDetailLogDtoBuilder dtoBuilder = LogbookKinerjaDetailLogDto
					.builder();
			{
				LogbookKinerjaDetailVO catatanKinerjaVO = catatanKinerjaConverter
						.transferModelToVO(log.getCatatanKinerja(), new LogbookKinerjaDetailVO());
				dtoBuilder.catatanKinerja(catatanKinerjaVO);
			}
			{
				PegawaiLogDto.PegawaiLogDtoBuilder builder = PegawaiLogDto.builder();
				builder.nipPns(log.getPegawai().getNipPns());
				builder.namaLengkap(log.getPegawai().getNamaLengkap());
				dtoBuilder.pegawai(builder.build());
			}
			LogbookKinerjaDetailLogDto dto = dtoBuilder.jenisLog(log.getJenisLog()).keterangan(log.getKeterangan())
					.tanggalDibuat(log.getTanggalDibuat()).build();
			dtoList.add(dto);
		});
		return new PageImpl<>(dtoList, pageable, logPage.getTotalElements());
	}

}
