package de.ivu.elect.presentation.charts;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;

import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ViewAccessScoped;
import org.primefaces.model.DefaultStreamedContent;

/**
 *  on 22.04.2015.
 */
@Named(SvgDownloader.COMPONENT_NAME)
@ViewAccessScoped
public class SvgDownloader implements Serializable {

  public static final String COMPONENT_NAME = "svgDownloader";

  private String filename;

  private String svg;

  public void prePareDownload() {
    svg = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("svg");
    filename = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("filename");
  }

  public DefaultStreamedContent getStreamedContent() {
    return new DefaultStreamedContent(new ByteArrayInputStream(svg.getBytes(StandardCharsets.UTF_8)), "image/svg+xml", filename+".svg", StandardCharsets.UTF_8.name());
  }

}
