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.stereotype.Service;

import com.jasamedika.medifirst2000.dao.JenisDietDao;
import com.jasamedika.medifirst2000.dao.JenisWaktuDao;
import com.jasamedika.medifirst2000.dao.MapJenisDietToProdukDao;
import com.jasamedika.medifirst2000.dao.ProdukDao;
import com.jasamedika.medifirst2000.dto.MapJenisDietToProdukDto;
import com.jasamedika.medifirst2000.entities.JenisDiet;
import com.jasamedika.medifirst2000.entities.JenisWaktu;
import com.jasamedika.medifirst2000.entities.MapJenisDietToProduk;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.service.MapJenisDietToProdukService;
import com.jasamedika.medifirst2000.util.CommonUtil;

@Service("MapJenisDietToProdukService")
public class MapJenisDietToProdukServiceImpl implements MapJenisDietToProdukService {
	@Autowired
	private MapJenisDietToProdukDao mapJenisDietToProdukDao;
	
	@Autowired
	private JenisWaktuDao jenisWaktuDao;
	
	@Autowired
	private JenisDietDao jenisDietDao;
	
	@Autowired
	private ProdukDao produkDao;

	@Override
	public List<Map<String, Object>> saveMapJenisDietToProduk(List<MapJenisDietToProdukDto> dtos) {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listMap = new ArrayList<>();
		for(MapJenisDietToProdukDto dto:dtos){
			MapJenisDietToProduk mapJenisDietToProduk = new MapJenisDietToProduk();
			mapJenisDietToProduk.setId(dto.getId());
			JenisDiet jenisDiet = this.jenisDietDao.findOne(dto.getJenisDietId());
			mapJenisDietToProduk.setJenisDiet(jenisDiet);
			Produk produk = this.produkDao.findById(dto.getProdukId());
			mapJenisDietToProduk.setProduk(produk);
			JenisWaktu jenisWaktu = this.jenisWaktuDao.findOne(dto.getJenisWaktuId());
			mapJenisDietToProduk.setJenisWaktu(jenisWaktu);
			mapJenisDietToProduk.setBerat(dto.getBerat());
			mapJenisDietToProduk.setStatusEnabled(dto.getStatusEnabled());
			MapJenisDietToProduk mapJenisDietToProdukResult = this.mapJenisDietToProdukDao.save(mapJenisDietToProduk);
			if(CommonUtil.isNotNullOrEmpty(mapJenisDietToProdukResult.getId() != null)) {
				result.put("id", mapJenisDietToProdukResult.getId());
				listMap.add(result);
			}
		}
		
		return listMap;
	}

	@Override
	public Map<String, Object> getMapJenisDietToProduk() {
		Map<String, Object> result = new HashMap<>();
		List<Map<String, Object>> listData = new ArrayList<>();
		
		List<Map<String, Object>> jenisDiet = this.mapJenisDietToProdukDao.getMapJenisDietToProduk();
		for(Map<String, Object> item:jenisDiet){
			item.put("MapJenisDietToProduk", this.mapJenisDietToProdukDao.getProdukToJenisDiet((Integer)item.get("id")));
			listData.add(item);
		}
		result.put("data", listData);
		return result;
	}

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

}
