package com.jasamedika.medifirst2000.service.impl;

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

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

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.SatuanStandarDao;
import com.jasamedika.medifirst2000.entities.SatuanStandar;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.SatuanStandarService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.SatuanStandarVO;

@Service("SatuanStandarService")
public class SatuanStandarServiceImpl extends BaseVoServiceImpl implements SatuanStandarService {

	@Autowired
	private SatuanStandarDao satuanStandarDao;

	@Autowired
	private BaseConverterImpl<SatuanStandarVO, SatuanStandar> satuanStandarConverter;

	@Override
	public Map<String, Object> addSatuanStandar(SatuanStandarVO vo) {
		SatuanStandar satuanStandar = new SatuanStandar();
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			satuanStandar = satuanStandarConverter.transferVOToModel(vo, new SatuanStandar());
		}
		int countKd = satuanStandarDao.getCount() + 1;
		// satuanStandar.setKdSatuanStandar((byte)countKd);
		// satuanStandar.setqSatuanStandar((byte)0);
		Map<String, Object> result = new HashMap<String, Object>();

		SatuanStandar resultModel = satuanStandarDao.save(satuanStandar);

		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("id", resultModel.getId());
			result.put("name", resultModel.getSatuanStandar());
		}

		return result;
	}

	@Override
	public Map<String, Object> updateSatuanStandar(SatuanStandarVO vo) {
		SatuanStandar satuanStandar = satuanStandarDao.findOne(vo.getId());
		if (CommonUtil.isNotNullOrEmpty(vo)) {
			satuanStandar = satuanStandarConverter.transferVOToModel(vo, new SatuanStandar());
		}

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

		SatuanStandar resultModel = satuanStandarDao.save(satuanStandar);

		if (CommonUtil.isNotNullOrEmpty(resultModel)) {
			result.put("id", resultModel.getId());
			result.put("name", resultModel.getSatuanStandar());
		}

		return result;
	}

	@Override
	public Map<String, Object> findAllSatuanStandar() {
		List<SatuanStandar> list = new ArrayList<>();
		Map<String, Object> result = new HashMap<String, Object>();

		list = (List<SatuanStandar>) satuanStandarDao.findAll();

		if (CommonUtil.isNotNullOrEmpty(list)) {
			result.put("data", list);
		}

		return result;
	}

	@Override
	public SatuanStandarVO add(SatuanStandarVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public SatuanStandarVO update(SatuanStandarVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

	@Override
	public Boolean delete(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public SatuanStandarVO findById(Integer key) throws JpaSystemException {
		return null;
	}

	@Override
	public List<SatuanStandarVO> findAll() throws JpaSystemException {
		return null;
	}

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}

	@Override
	public List<Map<String, Object>> getAll() {
		List<Map<String, Object>> result = new ArrayList<>();
		
		List<Object[]> objectsData = satuanStandarDao.getAll();
		for (Object[] objects : objectsData) {
			Map<String, Object> data = new HashMap<>();
			int i = 0;
			for (Object object : objects) {
				i = i + 1;
				if (i == 1) {
					data.put("id", object);
				} else if (i == 2) {
					data.put("satuanStandar", object);
				}
			}
			result.add(data);
		}
		
		return result;
	}

}
