package com.jasamedika.medifirst2000.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PemakaianRuangRapatDao;
import com.jasamedika.medifirst2000.dao.PemakaianRuangRapatService;
import com.jasamedika.medifirst2000.dao.StrukOrderDao;
import com.jasamedika.medifirst2000.entities.MappingPegawaiPemakaianRuangRapat;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.entities.PemakaianRuangRapat;
import com.jasamedika.medifirst2000.entities.StrukOrder;
import com.jasamedika.medifirst2000.enums.Status;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.MappingPegawaiPemakaianRuangRapatVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;
import com.jasamedika.medifirst2000.vo.PemakaianRuangRapatVO;
import com.jasamedika.medifirst2000.vo.StrukOrderVO;

@Service("pemakaianRuangRapatService")
public class PemakaianRuangRapatServiceImpl implements PemakaianRuangRapatService {

	@Autowired
	private PemakaianRuangRapatDao pemakaianRuangRapatDao;

	@Autowired
	private StrukOrderDao strukOrderDao;

	@Autowired
	private BaseConverterImpl<PemakaianRuangRapatVO, PemakaianRuangRapat> pemakaianRuangRapatConverter;

	@Autowired
	private BaseConverterImpl<StrukOrderVO, StrukOrder> strukOrderConverter;

	@Autowired
	private BaseConverterImpl<PegawaiVO, Pegawai> pegawaiConverter;

	@Autowired
	private BaseConverterImpl<MappingPegawaiPemakaianRuangRapatVO, MappingPegawaiPemakaianRuangRapat> mappingPegawaiRuangRapatConverter;

	@Override
	public Map<String, Object> savePemakaianRuangRapat(PemakaianRuangRapatVO vo, String noRec)
			throws JpaSystemException, ServiceVOException {

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

		try {
			System.out.println("==================START SAVE PEMAKAIAN RUANG RAPAT======================");
			if (CommonUtil.isNotNullOrEmpty(vo.getStrukOrder())) {
				StrukOrder strukOrder = strukOrderDao.findByNoRec(noRec);
				strukOrderConverter.transferVOToModel(vo.getStrukOrder(), new StrukOrder());
				PemakaianRuangRapat pemakaianRuangRapat = pemakaianRuangRapatConverter.transferVOToModel(vo,
						new PemakaianRuangRapat());
				strukOrder.setStatus(Status.DIPAKAI.getVal());
				// get nama petugas
				if (CommonUtil.isNotNullOrEmpty(vo.getMapPegawaiRuangRapat())) {
					Set<MappingPegawaiPemakaianRuangRapat> listMapPegawaiRuangRapat = new HashSet<MappingPegawaiPemakaianRuangRapat>();
					for (MappingPegawaiPemakaianRuangRapatVO modelMapPegawaiRuangRapat : vo.getMapPegawaiRuangRapat()) {
						MappingPegawaiPemakaianRuangRapat mappingPegawaiRuangRapat = mappingPegawaiRuangRapatConverter
								.transferVOToModel(modelMapPegawaiRuangRapat, new MappingPegawaiPemakaianRuangRapat());
						if (CommonUtil.isNotNullOrEmpty(modelMapPegawaiRuangRapat.getPetugas())) {
							mappingPegawaiRuangRapat.setPetugas(pegawaiConverter
									.transferVOToModel(modelMapPegawaiRuangRapat.getPetugas(), new Pegawai()));
						}
						mappingPegawaiRuangRapat.setPemakaianRuangRapat(pemakaianRuangRapat);
						listMapPegawaiRuangRapat.add(mappingPegawaiRuangRapat);
					}
					pemakaianRuangRapat.setMapPegawaiRuangRapat(listMapPegawaiRuangRapat);
				}
				pemakaianRuangRapat.setStrukOrder(strukOrder);
				StrukOrder resultStrukOrder = strukOrderDao.save(strukOrder);
				result.put("noRecStrukOrder", resultStrukOrder.getNoRec());
				PemakaianRuangRapat resultPemakaianRuangRapat = pemakaianRuangRapatDao.save(pemakaianRuangRapat);
				result.put("noRecPemakaianRuangRapat", resultPemakaianRuangRapat.getNoRec());
			}
			System.out.println("==================END SAVE PEMAKAIAN RUANG RAPAT======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

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

		Map<String, Object> result = new HashMap<String, Object>();
		try {
			System.out.println("============START FIND PEMAKAIAN RUANG RAPAT BY NO ORDER============");
			StrukOrder resultModel = strukOrderDao.findByNoOrder(noOrder);
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("dataFound", true);
			} else {
				result.put("dataFound", false);
			}
			System.out.println("============END FIND PEMAKAIAN RUANG RAPAT BY NO ORDER============");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
