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 org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.converter.RefackingConverter;
import com.jasamedika.medifirst2000.dao.RefackingDao;
import com.jasamedika.medifirst2000.dao.RunningNumberDao;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.ProduksiDetail;
import com.jasamedika.medifirst2000.entities.Refacking;
import com.jasamedika.medifirst2000.entities.RunningNumber;
import com.jasamedika.medifirst2000.service.RefackingService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.StringUtil;
import com.jasamedika.medifirst2000.vo.ProduksiDetailVO;
import com.jasamedika.medifirst2000.vo.RefackingVO;

@Service("refackingService")
public class RefackingServiceImpl implements RefackingService{
	
	@Autowired
	private RefackingDao refackingDao;
	
	@Autowired
	private RunningNumberDao runningNumberDao;
	
	@Autowired
	private RefackingConverter refackingConverter;
	
	@Autowired
	private BaseConverterImpl<ProduksiDetailVO, ProduksiDetail> produksiDetailConverter;
	
	@Override
	@Transactional
	public Map<String, Object> addRefacking(RefackingVO vo) {
		Map<String,Object> result=new HashMap<String,Object>();
		Refacking refacking = refackingConverter.transferVOToModel(vo, new Refacking());
		
		RunningNumber runningNumber = runningNumberDao.findByResetAndKegunaan("0", "Produksi");
		if(runningNumber==null)
		{
			runningNumber = new RunningNumber();
			runningNumber.setKegunaan("Produksi");
			runningNumber.setReset("0");
			runningNumber.setNomerTerbaru((Integer)0);
		}
		Integer number=(runningNumber.getNomerTerbaru()+(Integer)1);
		runningNumber.setNomerTerbaru(runningNumber.getNomerTerbaru()+(Integer)1);
		String noProduksi = StringUtil.formatNumber(number.toString(), 10); 
		
		refacking.setNoProduksi(noProduksi);
		
		List<ProduksiDetail> listProduksiDetail=new ArrayList<ProduksiDetail>();
		for(ProduksiDetailVO vos : vo.getProduksiDetailSet()){
			ProduksiDetail produksiDetail = produksiDetailConverter.transferVOToModel(vos, new ProduksiDetail());
			produksiDetail.setRefacking(refacking);
			produksiDetail.setSubTotal(produksiDetail.getHarga()*produksiDetail.getQty());
			listProduksiDetail.add(produksiDetail);
		}
		
		refacking.getProduksiDetailSet().clear();
		refacking.getProduksiDetailSet().addAll(listProduksiDetail);
		
		Refacking resultModel = refackingDao.save(refacking);
		if(CommonUtil.isNotNullOrEmpty(resultModel)){
//			result.put("refacking", resultModel);
			result.put("noProduksi", resultModel.getNoProduksi());
		}
		return result;
	}

	@Override
	public Map<String, Object> findProdukRefacking() {
		List<Produk> list = refackingDao.findProduk();		
		Map<String, Object> result = new HashMap<String, Object>();
		if(CommonUtil.isNotNullOrEmpty(list)){
			result.put("list", list);
			result.put("dataFound", true);
		}
		else{
			result.put("dataFound", true);
		}
		return result;
	}
	

}
