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.KegiatanDao;
import com.jasamedika.medifirst2000.entities.Kegiatan;
import com.jasamedika.medifirst2000.entities.KemenLemb;
import com.jasamedika.medifirst2000.service.KegiatanService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.KegiatanVO;

@Service("kegiatanService")
public class KegiatanServiceImpl implements KegiatanService {

	@Autowired
	private KegiatanDao kegiatanDao;

	@Autowired
	private BaseConverterImpl<KegiatanVO, Kegiatan> kegiatanConverter;

	@Override
	public Map<String, Object> updateKegiatan(KegiatanVO vo) {

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

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

			Kegiatan kegiatan = kegiatanConverter.transferVOToModel(vo, new Kegiatan());

			Kegiatan resultModel = kegiatanDao.save(kegiatan);
			result.put("id", resultModel.getId());
			System.out.println("==================END UPDATE KEGIATAN======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public Map<String, Object> saveKegiatan(KegiatanVO vo) {

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

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

			Kegiatan kegiatan = kegiatanConverter.transferVOToModel(vo, new Kegiatan());

			Kegiatan resultModel = kegiatanDao.save(kegiatan);
			result.put("id", resultModel.getId());
			System.out.println("==================END SAVE KEGIATAN======================");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

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

}
