package com.jasamedika.medifirst2000.service.impl;

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.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.PengirimanBarangDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
import com.jasamedika.medifirst2000.entities.PengirimanBarang;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.PengirimanBarangService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;
import com.jasamedika.medifirst2000.vo.PengirimanBarangVO;
import com.jasamedika.medifirst2000.vo.RuanganVO;

@Service("pengirimanBarangService")
public class PengirimanBarangServiceImpl implements PengirimanBarangService {

	@Autowired
	private PengirimanBarangDao pengirimanBarangDao;

	@Autowired
	private RuanganDao ruanganDao;

	@Autowired
	private BaseConverterImpl<PengirimanBarangVO, PengirimanBarang> pengirimanBarangConverter;

	@Autowired
	private BaseConverterImpl<RuanganVO, Ruangan> ruanganConverter;

	@Override
	public Map<String, Object> savePengirimanBarang(PengirimanBarangVO vo) {

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

		try {
			System.out.println("==================START SAVE PENGIRIMAN BARANG======================");
			PengirimanBarang pengirimanBarang = pengirimanBarangConverter.transferVOToModel(vo, new PengirimanBarang());
			// set tanggal hari ini
			pengirimanBarang.setTgl(DateUtil.getShortDate());

			// get ruangan loundry
			if (CommonUtil.isNotNullOrEmpty(vo.getRuangan())) {
				pengirimanBarang.setRuangan(ruanganConverter.transferVOToModel(vo.getRuangan(), new Ruangan()));
			}

			PengirimanBarang resultModel = pengirimanBarangDao.save(pengirimanBarang);
			result.put("noRec Pengiriman Barang (Loundry)", resultModel.noRec);
			System.out.println("==================END SAVE PENGIRIMAN BARANG======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

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

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

		try {
			System.out.println("==================START FIND RUANG LOUNDRY======================");
			List<Ruangan> resultModel = ruanganDao.findRuangLoundry();
			if (CommonUtil.isNotNullOrEmpty(resultModel)) {
				result.put("data", resultModel);
				result.put("Ruang Loundry Ada", true);
			} else {
				result.put("Ruang Loundry Tidak Ada", false);
			}
			System.out.println("==================END FIND RUANG LOUNDRY======================");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
