package de.jottyfan.camporganizer.module.business.outlay;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import de.jottyfan.camporganizer.module.business.outlay.model.CampBean;
import de.jottyfan.camporganizer.module.business.outlay.model.OutlayBean;
import jakarta.validation.Valid;

/**
 *
 * @author jotty
 *
 */
@Service
public class OutlayService {

	@Autowired
	private OutlayRepository repository;

	public List<OutlayBean> getListOf(String username, Integer campId) {
		return repository.getListOf(username, campId);
	}

	public List<CampBean> getAllCamps() {
		return repository.getAllCamps();
	}

	public void save(@Valid OutlayBean bean) {
		if (bean.getId() != null) {
			repository.updateBean(bean);
		} else {
			repository.addBean(bean);
		}
	}

	public OutlayBean getBeanIfAllowedFor(String username, Integer id) {
		return repository.getBeanIfAllowedFor(username, id);
	}

	public void deleteIfAllowedFor(String username, Integer id) {
		repository.deleteBeanIfAllowedFor(username, id);
	}

	public InputStream getCsv(Integer campId) {
		List<OutlayBean> list = repository.getListOf(null, campId);
		StringBuilder buf = new StringBuilder("Beleg-Nummer;Einkauf bei;Freizeit;bezahlt von;Betrag;Bestandteile;Rechnungsdatum\n");
		for (OutlayBean bean : list) {
			buf.append(bean.toCsvLine());
		}
		return new ByteArrayInputStream(buf.toString().getBytes());
	}
}
