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.KartuPengendaliDao;
import com.jasamedika.medifirst2000.dao.RekananDao;
import com.jasamedika.medifirst2000.dao.StrukRekapPemenangDao;
import com.jasamedika.medifirst2000.dao.custom.KartuPengendaliDaoCustom;
import com.jasamedika.medifirst2000.service.RekapPemenangService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.IndonesianNumberToWords;

@Service("rekapPememenangService")
public class RekapPemenangServiceImpl extends BaseAngaranServiceImpl implements RekapPemenangService {
	
	@Autowired
	private StrukRekapPemenangDao strukRekapPemenangDao;
	
	@Autowired
	private KartuPengendaliDaoCustom kartuPengendaliDaoCustom;
	
	@Autowired
	private RekananDao rekananDao;
	
	// Detail Kartu Pengendali By No Rc dan penawaraan Harga supplier
	@Override
	public Map<String, Object> rekapPemenang(String noRec) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("kartuPengendali", kartuPengendaliDaoCustom.getKartuPengendaliHeaderByNoRec(noRec));
		List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
		for(Map<String,Object> strukRekap:strukRekapPemenangDao.findSupplierByKartuPengendali(noRec)){
			if(CommonUtil.isNotNullOrEmpty(strukRekap.get("totalhargaSupplier"))){
				strukRekap.put("hargaString", terbilang((Double) strukRekap.get("totalhargaSupplier")));
				data.add(strukRekap);
			}
		}
		result.put("supplier", data);
		return result;
	}
	
	@Override
	public Map<String, Object> detailRekapPemenang(Integer supplierId,String noRecKartuPengendaliId) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("supplier",rekananDao.findById(supplierId));
		result.put("detailProduk", strukRekapPemenangDao.findPrdukBySupplier(supplierId, noRecKartuPengendaliId));
		result.put("total", strukRekapPemenangDao.totalProdukBySupplier(supplierId, noRecKartuPengendaliId));
		return result;
	}
	
	public String terbilang(Double value){
		long intValue = value.intValue();
		String result = IndonesianNumberToWords.convert(intValue);
		return result;
	}

	


}
