package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.dao.KartuStokDao;
import com.jasamedika.medifirst2000.dao.custom.KartuStokDaoCustom;
import com.jasamedika.medifirst2000.entities.KartuStok;
import com.jasamedika.medifirst2000.entities.Produk;
import com.jasamedika.medifirst2000.entities.Ruangan;
import com.jasamedika.medifirst2000.service.KartuStokService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

@Service("kartuStokService")
public class KartuStokServiceImpl extends BaseVoServiceImpl implements KartuStokService{
	
	@Autowired
	private KartuStokDao kartuStokDao;
	
	
	@Autowired
	private KartuStokDaoCustom kartuStokDaoCustom;
	
	
	@Override
	@Transactional
	public void saveKartuStok(Double qty,Produk produk,Ruangan ruangan,String keterangan,Boolean status,Date tanggalKejadian){
		KartuStok kartuStok = new KartuStok();
		kartuStok.setProduk(produk);
		kartuStok.setRuangan(ruangan);
		kartuStok.setKeterangan(keterangan);
		kartuStok.setJumlah(qty);
		kartuStok.setTglInput(new Date());
		if (CommonUtil.isNotNullOrEmpty(tanggalKejadian)) {
			kartuStok.setTglKejadian(tanggalKejadian);
		} else {
			kartuStok.setTglKejadian(new Date());
		}
		KartuStok kartuStokLast = new KartuStok();
		List<KartuStok> listKartuStok = kartuStokDaoCustom.lastKartuStok(ruangan.getId(), produk.getId());
		if (CommonUtil.isNotNullOrEmpty(listKartuStok)) {
			kartuStokLast = listKartuStok.get(0);
			Double saldoAwal = 0.0;
			if (CommonUtil.isNotNullOrEmpty(kartuStokLast.getSaldoAwal())) {
				saldoAwal = kartuStokLast.getSaldoAwal();
			}
			if (status == true) {
				kartuStok.setSaldoAwal(saldoAwal + qty);
				kartuStok.setStatus(true);
			} else if (status == false) {
				kartuStok.setSaldoAwal(saldoAwal - qty);
				kartuStok.setStatus(false);
			}
		} else {
			Double saldoAwal = 0.0;
			if (status == true) {
				kartuStok.setSaldoAwal(saldoAwal + qty);
				kartuStok.setStatus(true);
			} else if (status == false) {
				kartuStok.setSaldoAwal(0.0);
				kartuStok.setStatus(false);
			}
		}
		kartuStokDao.save(kartuStok);
		
	}

	@Override
	public List<Map<String, Object>> getKartuStok(Integer ruanganId, Integer produkId,String dateStart, String dateEnd) {
		Date awal=DateUtil.toDate(dateStart);
		Date ahir=DateUtil.toDate(dateEnd);
		List<Map<String, Object>> result=new ArrayList<>();
		for(Map<String,Object> data:kartuStokDao.kartuStokList(produkId, ruanganId,awal,ahir)){
			if(CommonUtil.isNotNullOrEmpty(data.get("status"))){
				data.put("stokAhir", (Double) data.get("saldoAhir"));
				if((boolean) data.get("status")){
					data.put("stokMasuk",(Double) data.get("jumlah"));
					data.put("stokKeluar", 0.0);
					if((Double) data.get("saldoAhir")==0.0){
						data.put("stokAwal", (Double) data.get("saldoAhir")-0.0);
					}else{
						data.put("stokAwal", (Double) data.get("saldoAhir")-(Double) data.get("jumlah"));
					}
					
				}else{
					data.put("stokMasuk", 0.0);
					data.put("stokKeluar", (Double) data.get("jumlah"));
					data.put("stokAwal", (Double) data.get("saldoAhir")+(Double) data.get("jumlah"));
				}
			}
			result.add(data);
		}
		return result ;
	}
	
}
