package de.jottyfan.camporganizer.module.ical.impl;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

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

import biweekly.Biweekly;
import biweekly.ICalendar;
import de.jottyfan.camporganizer.module.ical.IICalService;

/**
 *
 * @author jotty
 *
 */
@Service
public class ICalService implements IICalService {

	@Autowired
	private ICalGateway gateway;

	@Override
	public Boolean generate(HttpServletResponse response) throws IOException {
		ICalendar ical = gateway.getIcal();
		String content = Biweekly.write(ical).go();

		response.setHeader("charset", "UTF-8");
		response.setHeader("Content-Type", "text/calendar");
		response.setHeader("Content-Disposition", "attachment; filename=\"onkelwernerfreizeiten.de.ics\"");

		response.getWriter().write(content);
		response.getWriter().flush();
		response.flushBuffer();

		return true;
	}

}
