package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

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

import com.jasamedika.medifirst2000.vo.RegistrasiKeanggotaanVO;
import com.jasamedika.medifirst2000.vo.ReservasiDetailVO;
import com.jasamedika.medifirst2000.vo.BibliographyVO;
import com.jasamedika.medifirst2000.entities.RegistrasiKeanggotaan;
import com.jasamedika.medifirst2000.entities.Bibliography;

import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ReservasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.JsonUtil;
import com.jasamedika.medifirst2000.entities.Reservasi;
import com.jasamedika.medifirst2000.vo.ReservasiVO;
import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.ReservasiDao;

/**
 * Implement class for ReservasiService
 * 
 * @author Generator
 * @param <T>
 */
@Service("reservasiService")
public class ReservasiServiceImpl extends BaseVoServiceImpl implements ReservasiService {
	@Autowired
	private BaseConverterImpl<ReservasiVO, Reservasi> reservasiConverter;
	@Autowired
	private BaseConverterImpl<RegistrasiKeanggotaanVO, RegistrasiKeanggotaan> registrasiKeanggotaanConverter;

	@Autowired
	private BaseConverterImpl<BibliographyVO, Bibliography> bibliographyConverter;

	@Autowired
	private ReservasiDao reservasiDao;

	@Override
	public Map<String, Object> saveReservasi(ReservasiVO vo) {
		Map<String, Object> result = new HashMap<String, Object>();
		for (ReservasiDetailVO item : vo.getBuku()) {
			Reservasi model = new Reservasi();
			model = reservasiConverter.transferVOToModel(vo, model);

			if (CommonUtil.isNotNullOrEmpty(vo.getAnggota())) {
				model.setAnggota(
						registrasiKeanggotaanConverter.transferVOToModel(vo.getAnggota(), new RegistrasiKeanggotaan()));
			}
			if (CommonUtil.isNotNullOrEmpty(vo.getBuku())) {
				model.setBuku(bibliographyConverter.transferVOToModel(item.getBuku(), new Bibliography()));
			}
			model.setTanggalPinjam(item.getTanggalPinjam());
			Reservasi resultDomain = reservasiDao.save(model);
			if (CommonUtil.isNotNullOrEmpty(resultDomain)) {
				result.put("noRec", resultDomain.getNoRec());
			}
		}

		return result;
	}

	@Override
	public Map<String, Object> findReservasiByAnggota(String norec) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("items",JsonUtil.ToMaps(reservasiDao.findByAnggotaNoRec(norec)));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return result;
	}
}