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.KegiatanDetailDao;
import com.jasamedika.medifirst2000.entities.Kegiatan;
import com.jasamedika.medifirst2000.entities.KegiatanDetail;
import com.jasamedika.medifirst2000.service.KegiatanDetailService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KegiatanDetailVO;

@Service("KegiatanDetailService")
public class KegiatanDetailServiceImpl implements KegiatanDetailService {

	@Autowired
	private KegiatanDetailDao kegiatanDetailDao;

	@Autowired
	private BaseConverterImpl<KegiatanDetailVO, KegiatanDetail> kegiatanDetailConverter;

	@Override
	public Map<String, Object> saveKegiatanDetail(KegiatanDetailVO vo) {

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

		try {
			System.out.println("==================START SAVE KEGIATAN DETAIL======================");

			KegiatanDetail kegiatanDetail = kegiatanDetailConverter.transferVOToModel(vo, new KegiatanDetail());

			KegiatanDetail resultModel = kegiatanDetailDao.save(kegiatanDetail);
			result.put("id", resultModel.getId());
			System.out.println("==================END SAVE KEGIATAN DETAIL======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> updateKegiatanDetail(KegiatanDetailVO vo) {

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

		try {
			System.out.println("==================START UPDATE KEGIATAN DETAIL======================");

			KegiatanDetail kegiatanDetail = kegiatanDetailConverter.transferVOToModel(vo, new KegiatanDetail());

			KegiatanDetail resultModel = kegiatanDetailDao.save(kegiatanDetail);
			result.put("id", resultModel.getId());
			System.out.println("==================END UPDATE KEGIATAN DETAIL======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	
	@Override
	public Map<String, Object> findAllKgiatanDetail() {
		Map<String, Object> result = new HashMap<String, Object>();
		List<KegiatanDetail> resultModel = (List<KegiatanDetail>) kegiatanDetailDao.findAll();
		
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

}
