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.MapProdukMenuToBahanDao;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dao.SatuanStandarDao;
import com.jasamedika.medifirst2000.entities.MapProdukMenuToBahan;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.MapProdukMenuToBahanService;
import com.jasamedika.medifirst2000.vo.MapProdukMenuToBahanVO;

@Service("MapProdukMenuToBahanService")
public class MapProdukMenuToBahanServiceImpl extends BaseVoServiceImpl implements MapProdukMenuToBahanService{

	@Autowired
	private MapProdukMenuToBahanDao mapDao;
	
	@Autowired
	private ProdukDao produkDao;
	
	@Autowired
	private SatuanStandarDao satuanStandarDao;

	@Autowired
	private BaseVoConverter<MapProdukMenuToBahanVO, MapProdukMenuToBahan> mapConverter;
	
	@Override
	public MapProdukMenuToBahanVO add(MapProdukMenuToBahanVO vo) throws JpaSystemException, ServiceVOException {
		return null;
	}

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

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

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

	@Override
	public List<MapProdukMenuToBahanVO> 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<MapProdukMenuToBahan> 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<MapProdukMenuToBahan> resultPage = mapDao.findAll(spec, pageable);
		List<MapProdukMenuToBahan> resultData = resultPage.getContent();
		return constructMapReturn(resultData, resultPage.getTotalElements(), resultPage.getTotalPages());
	}

	@Override
	public Map<String, Object> save(List<MapProdukMenuToBahanVO> vos) {
		Map<String,Object> result = new HashMap<String,Object>();
		
		for(MapProdukMenuToBahanVO vo : vos){
		MapProdukMenuToBahan model = mapConverter.transferVOToModel(vo, new MapProdukMenuToBahan());
		
		mapDao.save(model);
		}
		
		return result;
	}

	@Override
	public Map<String, Object> findMenuGizi() {
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("data", produkDao.findMenuGizi());
		return result;
	}
	
	@Override
	public Map<String, Object> findBahanGizi() {
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("data", produkDao.findBahanGizi());
		return result;
	}

	@Override
	public Map<String, Object> findAll(Integer page, Integer take, String sort, String dir, Integer menuId) {
		Integer count = mapDao.count(menuId);
		
		Integer pages = count/take;
		Integer start = take * page;
		List<Map<String,Object>> resultListModel = mapDao.findAll(menuId, sort);
		
		for(Map<String,Object> model : resultListModel){
			model.put("bahanProduk", produkDao.findById((Integer)model.get("bahanId")));
			model.put("satuanStandar", satuanStandarDao.findById((Integer)model.get("satuanId")));
		}
		return constructMapReturn(resultListModel, count, pages);
//		return null;
	}

}
