package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.Map;

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

import com.jasamedika.medifirst2000.dao.BiologicalSudahDisterileDao;
import com.jasamedika.medifirst2000.dao.BowidickDao;
import com.jasamedika.medifirst2000.dao.MesinDao;
import com.jasamedika.medifirst2000.dao.PegawaiDao;
import com.jasamedika.medifirst2000.dao.PencatatanSuhuMesinDao;
import com.jasamedika.medifirst2000.dto.BowidickDto;
import com.jasamedika.medifirst2000.entities.BiologicalSudahDisterile;
import com.jasamedika.medifirst2000.entities.Bowidick;
import com.jasamedika.medifirst2000.entities.Mesin;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PencatatanSuhuMesin;
import com.jasamedika.medifirst2000.service.BowidickService;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Service("BowidickService")
public class BowidickServiceImpl implements BowidickService {
	@Autowired
	private BowidickDao bowidickDao;
	
	@Autowired
	private MesinDao mesinDao;
	
	@Autowired
	private PegawaiDao pegawaiDao;
	
	@Autowired
	private PencatatanSuhuMesinDao pencatatanSuhuMesinDao;
	
	@Autowired
	private BiologicalSudahDisterileDao biologicalSudahDisterileDao;
	

	@Override
	public Map<String, Object> saveBowidick(BowidickDto dto) {
		Map<String, Object> result = new HashMap<>();
		
		PencatatanSuhuMesin pencatatanSuhuMesin = new PencatatanSuhuMesin();
		pencatatanSuhuMesin.setNoRec(dto.getPencatatanMesinNoRec());
		Mesin mesin = this.mesinDao.findOne(dto.getMesinId());
		pencatatanSuhuMesin.setMesin(mesin);
		Pegawai petugas = this.pegawaiDao.findById(dto.getPetugasId());
		pencatatanSuhuMesin.setPetugas(petugas);
		pencatatanSuhuMesin.setTanggal(dto.getTanggal());
		pencatatanSuhuMesin.setProgramMesin(dto.getProgramMesin());
		pencatatanSuhuMesin.setSuhu(dto.getSuhu());
		pencatatanSuhuMesin.setTekanan(dto.getTekanan());
		pencatatanSuhuMesin.setWaktuMulai(dto.getWaktuMulai());
		pencatatanSuhuMesin.setWaktuSelesai(dto.getWaktuSelesai());
		pencatatanSuhuMesin.setLamaProses(dto.getLamaProses());
		pencatatanSuhuMesin.setKeterangan("Bowidick");
		pencatatanSuhuMesin.setStatusEnabled(true);
		PencatatanSuhuMesin PencatatanSuhuMesinResult = this.pencatatanSuhuMesinDao.save(pencatatanSuhuMesin);
		if(CommonUtil.isNotNullOrEmpty(PencatatanSuhuMesinResult)) {
			Bowidick bowidick = new Bowidick();
			bowidick.setNoRec(dto.getBowidickNoRec());
			bowidick.setPencatatanSuhuMesin(pencatatanSuhuMesin);
			bowidick.setMesin(mesin);
			bowidick.setProgramMesin(dto.getProgramMesin());
			bowidick.setHasil(dto.getHasil());
			bowidick.setSuhu(dto.getSuhu());
			bowidick.setTanggal(dto.getTanggalBowidick());
			Pegawai operator = this.pegawaiDao.findById(dto.getOperatorId());
			bowidick.setOperator(operator);
			Pegawai supervisor = this.pegawaiDao.findById(dto.getSupervisorId());
			bowidick.setSupervisor(supervisor);
			bowidick = this.bowidickDao.save(bowidick);
			
			BiologicalSudahDisterile biologicalSudahDisterile = new BiologicalSudahDisterile();
			biologicalSudahDisterile.setNoRec(dto.getBiologicalNoRec());
			biologicalSudahDisterile.setPencatatanSuhuMesin(pencatatanSuhuMesin);
			biologicalSudahDisterile.setTanggal(dto.getTanggalBiological());
			Pegawai operatorBiological = this.pegawaiDao.findById(dto.getOperatorBiologicalId());
			biologicalSudahDisterile.setOperator(operatorBiological);
			biologicalSudahDisterile.setSuhuPensterilan(dto.getSuhuPensterilan());
			biologicalSudahDisterile.setSuhuIncubator(dto.getSuhuIncubator());
			biologicalSudahDisterile.setHasil(dto.getHasilBiological());
			biologicalSudahDisterile.setJamMasuk(dto.getJamMasukBiological());
			biologicalSudahDisterile.setJamKeluar(dto.getJamKeluarBiological());
			biologicalSudahDisterile.setKeterangan("Biological");
			biologicalSudahDisterile = this.biologicalSudahDisterileDao.save(biologicalSudahDisterile);
			
			BiologicalSudahDisterile attestPembanding = new BiologicalSudahDisterile();
			attestPembanding.setNoRec(dto.getAttestPembandingNoRec());
			attestPembanding.setPencatatanSuhuMesin(pencatatanSuhuMesin);
			attestPembanding.setTanggal(dto.getTanggalAttest());
			Pegawai operatorAttest = this.pegawaiDao.findById(dto.getOperatorAttestId());
			attestPembanding.setOperator(operatorAttest);
			attestPembanding.setSuhuPensterilan(dto.getSuhuPensterilanAttest());
			attestPembanding.setSuhuIncubator(dto.getSuhuIncubatorAttest());
			attestPembanding.setHasil(dto.getHasilAttest());
			attestPembanding.setJamMasuk(dto.getJamMasukAttest());
			attestPembanding.setJamKeluar(dto.getJamKeluarAttest());
			attestPembanding.setKeterangan("Attest");
			attestPembanding = this.biologicalSudahDisterileDao.save(attestPembanding);
			
			result.put("data", PencatatanSuhuMesinResult.getNoRec());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getBowidick(String startDate, String endDate) {
		Map<String, Object> result = new HashMap<>();
		result.put("data", this.pencatatanSuhuMesinDao.getPencatatanSuhuMesinBowidick(startDate, endDate));
		return result;
	}

	@Override
	public Map<String, Object> getBowidickbyNorec(String noRec) {
		Map<String, Object> result = new HashMap<>();
		result.put("pencatatanSuhuMesin", this.pencatatanSuhuMesinDao.getPencatatanShuhuMesinBowidickByNoRec(noRec));
		result.put("bowidick", this.pencatatanSuhuMesinDao.getBowidickByNoRec(noRec));
		result.put("biologicalSudahDisterile", this.pencatatanSuhuMesinDao.getBiologicalSudahDisterileByNoRec(noRec));
		result.put("attestPembanding", this.pencatatanSuhuMesinDao.getAttestByNoRec(noRec));
		return result;
	}
	
	

}
