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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.ProgramDao;
import com.jasamedika.medifirst2000.entities.MapObjekModulToKelompokUser;
import com.jasamedika.medifirst2000.entities.Program;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.ProgramService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.ProgramVO;

@Service("ProgramService")
public class ProgramServiceImpl extends BaseVoServiceImpl implements ProgramService<ProgramVO>{
	
	@Autowired
	private ProgramDao programDao;
	
	@Autowired
	private BaseConverterImpl<ProgramVO,Program> programConverter;
	
	@Override
	public Map<String,Object> addProgram(ProgramVO vo) {

		Program program=new Program();
				
		if(CommonUtil.isNotNullOrEmpty(vo)){
			program=programConverter.transferVOToModel(vo, new Program());
		}
		
		program.setKodeProgram(StringUtil.formatNumber(String.valueOf(programDao.getCount()+1), 3));
		
		Program programResult=programDao.save(program);
		
		Map<String,Object> result=new HashMap<String,Object>();
		
		if (CommonUtil.isNotNullOrEmpty(programResult)) {
			result.put("noRec", programResult.getNoRec());
		}
		
		return result;
		
	}
	
	@Override
	public Map<String,Object> updateProgram(ProgramVO vo) {

		Program program=new Program();
				
		if(CommonUtil.isNotNullOrEmpty(vo)){
			program=programConverter.transferVOToModel(vo, new Program());
		}

		
		Program programResult=programDao.save(program);
		
		Map<String,Object> result=new HashMap<String,Object>();
		
		if (CommonUtil.isNotNullOrEmpty(programResult)) {
			result.put("noRec", programResult.getNoRec());
		}
		
		return result;
		
	}
	
	@Override
	public Map<String,Object> findAllProgram() {
		Map<String,Object> result=new HashMap<String,Object>();
		List<Program> resultModel=(List<Program>) programDao.findAll();
		//List<Program> resultModel=mapObjekModulToKelompokUserDao.findByKelompokUser(kelompokUser);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("program", resultModel);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		return result;
	}
	
	@Override
	public Map<String,Object> findOneProgram() {
		Map<String,Object> result=new HashMap<String,Object>();
		Program resultModel= programDao.findOneProgram("07");

		if(CommonUtil.isNotNullOrEmpty(resultModel)){
			result.put("program", resultModel);
			result.put("dataFound", true);
		}else{
			result.put("dataFound", false);
		}
		return result;
	}

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

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

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

	@Override
	public ProgramVO findById(String key) throws JpaSystemException {
		return null;
	}

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

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

}
