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.ProsesCuciDao;
import com.jasamedika.medifirst2000.entities.ProsesCuci;
import com.jasamedika.medifirst2000.service.ProsesCuciService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.ProsesCuciVO;

@Service("prosesCuciService")
public class ProsesCuciServiceImpl implements ProsesCuciService {

	@Autowired
	private ProsesCuciDao prosesCuciDao;

	@Autowired
	private BaseConverterImpl<ProsesCuciVO, ProsesCuci> prosesCuciConverter;

	@Override
	public Map<String, Object> saveProsesCuci(ProsesCuciVO vo) {

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

		try {
			System.out.println("===============START SAVE PROSES CUCI===============");
			ProsesCuci prosesCuci = prosesCuciConverter.transferVOToModel(vo, new ProsesCuci());
			prosesCuci.setKdProsesCuci(generateKodeProsesCuci());
			prosesCuci.setKodeExternal(vo.getKodeExternal());
			prosesCuci.setNamaExternal(vo.getNamaExternal());
			prosesCuci.setStatusEnabled(vo.getStatusEnabled());
			ProsesCuci resultModel = prosesCuciDao.save(prosesCuci);
			result.put("id Proses Cuci", resultModel.getId());
			System.out.println("===============END SAVE PROSES CUCI===============");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public String generateKodeProsesCuci() {

		Integer countByKodeProsesCuci = 0;
		String kodeProsesCuci = null;

		countByKodeProsesCuci = prosesCuciDao.getCountByKodeProsesCuci();
		countByKodeProsesCuci++;

		if (CommonUtil.isNotNullOrEmpty(countByKodeProsesCuci)) {
			kodeProsesCuci = "000" + countByKodeProsesCuci.toString();

		}

		return kodeProsesCuci;
	}

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

		Map<String, Object> result = new HashMap<String, Object>();
		//List<ProsesCuci> resultModel = (List<ProsesCuci>) prosesCuciDao.findAll();
		List<Map<String, Object>> resultModel = this.prosesCuciDao.getDataProsesCuci();
		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("data", resultModel);
			result.put("dataFound", true);
		} else {
			result.put("dataFound", false);
		}
		return result;
	}

	@Override
	public boolean deleteProsesCuci(Integer id) {

		if (CommonUtil.isNotNullOrEmpty(id)) {
			ProsesCuci prosesCuci = prosesCuciDao.findOne(id);
			prosesCuciDao.delete(prosesCuci);
			return true;
		} else {
			return false;
		}
	}

}
