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.ParameterDao;
import com.jasamedika.medifirst2000.entities.Parameter;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ParameterService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.ParameterVO;

@Service("ParameterService")
public class ParameterServiceImpl extends BaseVoServiceImpl implements ParameterService{
	
	@Autowired
	private ParameterDao parameterDao;
	
	@Autowired
	private BaseConverterImpl<ParameterVO, Parameter> parameterConverter;
	
	@Override
	public ParameterVO add(ParameterVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

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

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		return null;
	}
	
	
	// -----------------------------------------------------------//
	
	@Override
	public Map<String, Object> addParameter(ParameterVO vo) {
		Parameter parameter = new Parameter();
		if(CommonUtil.isNotNullOrEmpty(vo)){
			parameter = parameterConverter.transferVOToModel(vo, new Parameter());
		}
		
		
		parameter.setKode(StringUtil.formatNumber(String.valueOf(parameterDao.getCount()+1), 3));
		
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		Parameter resultModel = parameterDao.save(parameter);
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("id", resultModel.getId());
			result.put("name", resultModel.getNama());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> updateParameter(ParameterVO vo) {
		Parameter parameter = parameterDao.findOne(vo.getId());
		if(CommonUtil.isNotNullOrEmpty(vo)){
			parameter = parameterConverter.transferVOToModel(vo, new Parameter());
		}
		
		Map<String, Object> result = new HashMap<String,Object>();
		
		Parameter resultModel = parameterDao.save(parameter);
		
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("id", resultModel.getId());
			result.put("name", resultModel.getNama());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> findAllParameter() {
		List<Parameter> list = new ArrayList<>();
		Map<String, Object> result = new HashMap<String,Object>();
		
		list = (List<Parameter>) parameterDao.findAll();
		
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("data", list);
		}
		
		return result;
	}

}
