package com.pingunaut;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.apache.commons.io.IOUtils;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.DownloadLink;
import org.apache.wicket.markup.html.link.ResourceLink;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.ByteArrayResource;

public class HomePageTmpFile extends WebPage {
	private static final long serialVersionUID = 1L;

	public HomePageTmpFile(final PageParameters parameters) throws IOException {
		super(parameters);
		setVersioned(false);

		add(new DownloadLink("download", new LoadableDetachableModel<File>(){

			@Override
			protected File load() {
				Path p;
				try {
					p = Files.createTempFile("google", "html");
					Files.copy(WicketApplication.getInMemoryStream(), p, StandardCopyOption.REPLACE_EXISTING);
					return p.toFile();
				} catch (IOException e) {
					return null;
				}
				
			}
			
		}, Model.of("google.html")).setDeleteAfterDownload(true));
		
	}
}