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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.dao.JenisWaktuDao;
import com.jasamedika.medifirst2000.entities.Departemen;
import com.jasamedika.medifirst2000.entities.JenisWaktu;
import com.jasamedika.medifirst2000.entities.KelompokProduk;
import com.jasamedika.medifirst2000.entities.KelompokUser;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.JenisWaktuService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.DepartemenVO;
import com.jasamedika.medifirst2000.vo.JenisWaktuVO;
import com.jasamedika.medifirst2000.vo.KelompokProdukVO;
import com.jasamedika.medifirst2000.vo.KelompokUserVO;

@Service("JenisWaktuService")
public class JenisWaktuServiceImpl extends BaseVoServiceImpl implements JenisWaktuService{

	@Autowired
	private JenisWaktuDao jenisWaktuDao;
	
	@Autowired
	private BaseVoConverter<JenisWaktuVO, JenisWaktu> jenisWaktuConverter;
	
	@Autowired
	private BaseVoConverter<DepartemenVO, Departemen> departementConverter;
	
	@Autowired BaseVoConverter<KelompokProdukVO, KelompokProduk> kelompokProdukConverter;
	
	@Override
	public JenisWaktuVO add(JenisWaktuVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<JenisWaktuVO> 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> findAll(Integer page, Integer take, String sort, String dir,
			Specification<JenisWaktu> spec) {
		Direction direction=null;
		
		if(dir.equalsIgnoreCase("asc")){
			direction=Sort.Direction.ASC;
		}else{
			direction=Sort.Direction.DESC;
		}
		
		Pageable pageable = new PageRequest(page, take, direction, sort);
		Page<JenisWaktu> resultPage = jenisWaktuDao.findAll(spec, pageable);
		List<JenisWaktu> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> save(JenisWaktuVO vo) {
		Map<String,Object> result = new HashMap<String,Object>();

		JenisWaktu model = jenisWaktuConverter.transferVOToModel(vo, new JenisWaktu());

		Byte no = this.jenisWaktuDao.getNo().byteValue();
		model.setKdJenisWaktu(no);
		model.setNoUrut(no);
		model.setqJenisWaktu(no);
		
		model.setDepartemen(departementConverter.transferVOToModel(vo.getDepartemen(), new Departemen()));
		model.setKelompokProduk(kelompokProdukConverter.transferVOToModel(vo.getKelompokProduk(), new KelompokProduk()));
		JenisWaktu jenisWaktuResult = this.jenisWaktuDao.save(model);
		
		if(jenisWaktuResult.getId() != null){
			result.put("data", jenisWaktuResult.getId());
		}
		
		return result;
	}

	@Override
	public Map<String, Object> getJenisWaktu() {
		Map<String, Object> result = new HashMap<>();
		result.put("data", this.jenisWaktuDao.getJenisWaktu());
		return result;
	}
	
	

}
